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