diff options
author | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
commit | 6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch) | |
tree | b38119d2acf0a982cb67e381f146924b9bfc3b3f /DeepSky.Haze/DS_HazeCore.cs |
+init
Diffstat (limited to 'DeepSky.Haze/DS_HazeCore.cs')
-rw-r--r-- | DeepSky.Haze/DS_HazeCore.cs | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/DeepSky.Haze/DS_HazeCore.cs b/DeepSky.Haze/DS_HazeCore.cs new file mode 100644 index 0000000..30b3486 --- /dev/null +++ b/DeepSky.Haze/DS_HazeCore.cs @@ -0,0 +1,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; + } +} |