summaryrefslogtreecommitdiff
path: root/DeepSky.Haze/DS_HazeCore.cs
blob: 30b34866aeed9c116d631a38202355309aa75175 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System.Collections.Generic;
using UnityEngine;

namespace DeepSky.Haze;

[ExecuteInEditMode]
[AddComponentMenu("DeepSky Haze/Controller", 51)]
public class DS_HazeCore : MonoBehaviour
{
	public enum HeightFalloffType
	{
		Exponential,
		None
	}

	public enum NoiseTextureSize
	{
		x8 = 8,
		x16 = 0x10,
		x32 = 0x20
	}

	public enum DebugGUIPosition
	{
		TopLeft,
		TopCenter,
		TopRight,
		CenterLeft,
		Center,
		CenterRight,
		BottomLeft,
		BottomCenter,
		BottomRight
	}

	public static string kVersionStr = "DeepSky Haze v1.4.0";

	private static int kGUIHeight = 180;

	private static DS_HazeCore instance;

	[SerializeField]
	[Range(0f, 1f)]
	[Tooltip("The time at which Zones will evaluate their settings. Animate this or set in code to create time-of-day transitions.")]
	private float m_Time;

	[SerializeField]
	[Tooltip("The height falloff method to use globally (default Exponential).")]
	private HeightFalloffType m_HeightFalloff;

	[SerializeField]
	private List<DS_HazeZone> m_Zones = new List<DS_HazeZone>();

	[SerializeField]
	private DebugGUIPosition m_DebugGUIPosition;

	private HashSet<DS_HazeLightVolume> m_LightVolumes = new HashSet<DS_HazeLightVolume>();

	[SerializeField]
	private Texture3D m_NoiseLUT;

	[SerializeField]
	private bool m_ShowDebugGUI;

	private Vector2 m_GUIScrollPosition;

	private int m_GUISelectedView = -1;

	private bool m_GUISelectionPopup;

	private DS_HazeView m_GUIDisplayedView;

	public static DS_HazeCore Instance
	{
		get
		{
			if (instance == null)
			{
				instance = Object.FindObjectOfType<DS_HazeCore>();
			}
			return instance;
		}
	}

	public float Time
	{
		get
		{
			return m_Time;
		}
		set
		{
			m_Time = Mathf.Clamp01(value);
		}
	}

	public Texture3D NoiseLUT => m_NoiseLUT;

	public HeightFalloffType HeightFalloff
	{
		get
		{
			return m_HeightFalloff;
		}
		set
		{
			m_HeightFalloff = value;
			SetGlobalHeightFalloff();
		}
	}

	private void SetGlobalHeightFalloff()
	{
		switch (m_HeightFalloff)
		{
		case HeightFalloffType.Exponential:
			Shader.DisableKeyword("DS_HAZE_HEIGHT_FALLOFF_NONE");
			break;
		case HeightFalloffType.None:
			Shader.EnableKeyword("DS_HAZE_HEIGHT_FALLOFF_NONE");
			break;
		}
	}

	private void OnTransformChildrenChanged()
	{
		m_Zones.Clear();
		DS_HazeZone[] componentsInChildren = GetComponentsInChildren<DS_HazeZone>(includeInactive: true);
		m_Zones.AddRange(componentsInChildren);
	}

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		else if (instance != this)
		{
			Debug.LogError("DeepSky::DS_HazeCore:Awake - There is more than one Haze Controller in this scene! Disabling " + base.name);
			base.enabled = false;
		}
	}

	private void OnEnable()
	{
		SetGlobalHeightFalloff();
		Shader.SetGlobalTexture("_SamplingOffsets", m_NoiseLUT);
	}

	private void Reset()
	{
		OnTransformChildrenChanged();
	}

	public void SetGlobalNoiseLUT()
	{
		Shader.SetGlobalTexture("_SamplingOffsets", m_NoiseLUT);
	}

	public void AddLightVolume(DS_HazeLightVolume lightVolume)
	{
		RemoveLightVolume(lightVolume);
		m_LightVolumes.Add(lightVolume);
	}

	public void RemoveLightVolume(DS_HazeLightVolume lightVolume)
	{
		m_LightVolumes.Remove(lightVolume);
	}

	public void GetRenderLightVolumes(Vector3 cameraPosition, List<DS_HazeLightVolume> lightVolumes, List<DS_HazeLightVolume> shadowVolumes)
	{
		foreach (DS_HazeLightVolume lightVolume in m_LightVolumes)
		{
			if (lightVolume.WillRender(cameraPosition))
			{
				if (lightVolume.CastShadows)
				{
					shadowVolumes.Add(lightVolume);
				}
				else
				{
					lightVolumes.Add(lightVolume);
				}
			}
		}
	}

	public DS_HazeContextItem GetRenderContextAtPosition(Vector3 position)
	{
		List<DS_HazeZone> list = new List<DS_HazeZone>();
		for (int i = 0; i < m_Zones.Count; i++)
		{
			if (m_Zones[i].Contains(position) && m_Zones[i].enabled)
			{
				list.Add(m_Zones[i]);
			}
		}
		if (list.Count == 0)
		{
			return null;
		}
		if (list.Count == 1)
		{
			return list[0].Context.GetContextItemBlended(m_Time);
		}
		list.Sort((DS_HazeZone z1, DS_HazeZone z2) => (!(z1 < z2)) ? 1 : (-1));
		DS_HazeContextItem contextItemBlended = list[0].Context.GetContextItemBlended(m_Time);
		float num = 0f;
		for (int j = 1; j < list.Count; j++)
		{
			num = list[j].GetBlendWeight(position);
			contextItemBlended.Lerp(list[j].Context.GetContextItemBlended(m_Time), num);
		}
		return contextItemBlended;
	}
}