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