You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.6 KiB

  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.Utility
  4. {
  5. public class DynamicShadowSettings : MonoBehaviour
  6. {
  7. public Light sunLight;
  8. public float minHeight = 10;
  9. public float minShadowDistance = 80;
  10. public float minShadowBias = 1;
  11. public float maxHeight = 1000;
  12. public float maxShadowDistance = 10000;
  13. public float maxShadowBias = 0.1f;
  14. public float adaptTime = 1;
  15. private float m_SmoothHeight;
  16. private float m_ChangeSpeed;
  17. private float m_OriginalStrength = 1;
  18. private void Start()
  19. {
  20. m_OriginalStrength = sunLight.shadowStrength;
  21. }
  22. // Update is called once per frame
  23. private void Update()
  24. {
  25. Ray ray = new Ray(Camera.main.transform.position, -Vector3.up);
  26. RaycastHit hit;
  27. float height = transform.position.y;
  28. if (Physics.Raycast(ray, out hit))
  29. {
  30. height = hit.distance;
  31. }
  32. if (Mathf.Abs(height - m_SmoothHeight) > 1)
  33. {
  34. m_SmoothHeight = Mathf.SmoothDamp(m_SmoothHeight, height, ref m_ChangeSpeed, adaptTime);
  35. }
  36. float i = Mathf.InverseLerp(minHeight, maxHeight, m_SmoothHeight);
  37. QualitySettings.shadowDistance = Mathf.Lerp(minShadowDistance, maxShadowDistance, i);
  38. sunLight.shadowBias = Mathf.Lerp(minShadowBias, maxShadowBias, 1 - ((1 - i)*(1 - i)));
  39. sunLight.shadowStrength = Mathf.Lerp(m_OriginalStrength, 0, i);
  40. }
  41. }
  42. }