From 5b158af90739dcbb89c1538a6cb8c65a875dce80 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 26 Nov 2020 20:52:34 +0800 Subject: *misc --- .../Boxophobic.AtmosphericHeightFog.Runtime.asmdef | 14 + ...phobic.AtmosphericHeightFog.Runtime.asmdef.meta | 7 + .../Core/Runtime/HeightFogEnums.cs | 37 ++ .../Core/Runtime/HeightFogEnums.cs.meta | 12 + .../Core/Runtime/HeightFogGlobal.cs | 439 +++++++++++++++++++++ .../Core/Runtime/HeightFogGlobal.cs.meta | 11 + .../Core/Runtime/HeightFogOverride.cs | 392 ++++++++++++++++++ .../Core/Runtime/HeightFogOverride.cs.meta | 13 + .../Core/Runtime/HeightFogPerObject.cs | 186 +++++++++ .../Core/Runtime/HeightFogPerObject.cs.meta | 13 + 10 files changed, 1124 insertions(+) create mode 100644 Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/Boxophobic.AtmosphericHeightFog.Runtime.asmdef create mode 100644 Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/Boxophobic.AtmosphericHeightFog.Runtime.asmdef.meta create mode 100644 Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogEnums.cs create mode 100644 Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogEnums.cs.meta create mode 100644 Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogGlobal.cs create mode 100644 Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogGlobal.cs.meta create mode 100644 Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogOverride.cs create mode 100644 Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogOverride.cs.meta create mode 100644 Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogPerObject.cs create mode 100644 Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogPerObject.cs.meta (limited to 'Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime') diff --git a/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/Boxophobic.AtmosphericHeightFog.Runtime.asmdef b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/Boxophobic.AtmosphericHeightFog.Runtime.asmdef new file mode 100644 index 00000000..7ba3ccb0 --- /dev/null +++ b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/Boxophobic.AtmosphericHeightFog.Runtime.asmdef @@ -0,0 +1,14 @@ +{ + "name": "Boxophobic.AtmosphericHeightFog.Runtime", + "references": [ + "Boxophobic.Utils.Scripts" + ], + "optionalUnityReferences": [], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [] +} \ No newline at end of file diff --git a/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/Boxophobic.AtmosphericHeightFog.Runtime.asmdef.meta b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/Boxophobic.AtmosphericHeightFog.Runtime.asmdef.meta new file mode 100644 index 00000000..849ed431 --- /dev/null +++ b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/Boxophobic.AtmosphericHeightFog.Runtime.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 946ad27fa286e62409a42cca7d545b88 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogEnums.cs b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogEnums.cs new file mode 100644 index 00000000..ba3b8379 --- /dev/null +++ b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogEnums.cs @@ -0,0 +1,37 @@ +// Cristian Pop - https://boxophobic.com/ + +namespace AtmosphericHeightFog +{ + public enum FogMode + { + UseScriptSettings = 10, + UsePresetSettings = 15, + UseTimeOfDay = 20, + } + + public enum FogAxisMode + { + XAxis = 0, + YAxis = 1, + ZAxis = 2, + } + + public enum FogLayersMode + { + MultiplyDistanceAndHeight = 10, + AdditiveDistanceAndHeight = 20, + } + + public enum FogDirectionalMode + { + Off = 0, + On = 1 + } + + public enum FogNoiseMode + { + Off = 0, + Procedural3D = 3 + } +} + diff --git a/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogEnums.cs.meta b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogEnums.cs.meta new file mode 100644 index 00000000..ff110f08 --- /dev/null +++ b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogEnums.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 5472a508dc786f44eac5926a86dba7ff +timeCreated: 1554699905 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogGlobal.cs b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogGlobal.cs new file mode 100644 index 00000000..4850cbc8 --- /dev/null +++ b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogGlobal.cs @@ -0,0 +1,439 @@ +// Cristian Pop - https://boxophobic.com/ + +using UnityEngine; +using Boxophobic.StyledGUI; +using UnityEngine.Serialization; + +namespace AtmosphericHeightFog +{ + [RequireComponent(typeof(MeshRenderer))] + [RequireComponent(typeof(MeshFilter))] + [ExecuteInEditMode] + public class HeightFogGlobal : StyledMonoBehaviour + { + [StyledBanner(0.55f, 0.7f, 1f, "Height Fog Global", "", "https://docs.google.com/document/d/1pIzIHIZ-cSh2ykODSZCbAPtScJ4Jpuu7lS3rNEHCLbc/edit#heading=h.kfvqsi6kusw4")] + public bool styledBanner; + + [StyledCategory("Scene")] + public bool categoryScene; + + public Camera mainCamera; + public Light mainDirectional; + + [StyledCategory("Mode")] + public bool categoryMode; + + public FogMode fogMode = FogMode.UseScriptSettings; + + [StyledMessage("Info", "The Preset feature requires a material using the BOXOPHOBIC > Atmospherics > Fog Preset shader.", 10, 0)] + public bool messagePreset = false; + + [StyledMessage("Info", "The Time Of Day feature works by interpolating two Fog Preset materials using the BOXOPHOBIC > Atmospherics > Fog Preset shader. Please note that not all material properties can be interpolated properly!", 10, 0)] + public bool messageTimeOfDay = false; + + [Space(10)] + public Material presetMaterial; + + [Space(10)] + public Material presetDay; + public Material presetNight; + + [Space(10)] + [Range(0, 1)] + public float timeOfDay = 0; + + [StyledCategory("Fog")] + public bool categoryFog; + + [Range(0, 1)] + public float fogIntensity = 1; + + [Space(10)] + public FogAxisMode fogAxisMode = FogAxisMode.YAxis; + public FogLayersMode fogLayersMode = FogLayersMode.MultiplyDistanceAndHeight; + + [Space(10)] + [FormerlySerializedAs("fogColor")] + [ColorUsage(false, true)] + public Color fogColorStart = new Color(0.5f, 0.75f, 1.0f, 1.0f); + [ColorUsage(false, true)] + public Color fogColorEnd = new Color(0.75f, 1f, 1.25f, 1.0f); + [Range(0f, 1f)] + public float fogColorDuo = 0; + + [Space(10)] + public float fogDistanceStart = -100; + public float fogDistanceEnd = 100; + [Range(1, 8)] + public float fogDistanceFalloff = 1; + + [Space(10)] + public float fogHeightStart = 0; + public float fogHeightEnd = 100; + [Range(1f, 8f)] + public float fogHeightFalloff = 1; + + [StyledCategory("Skybox")] + public bool categorySkybox; + + [Range(0, 1)] + public float skyboxFogIntensity = 1; + [Range(0, 1)] + public float skyboxFogHeight = 1; + [Range(1, 8)] + public float skyboxFogFalloff = 1; + [Range(0, 1)] + public float skyboxFogFill = 0; + + [StyledCategory("Directional")] + public bool categoryDirectional; + + [Range(0, 1)] + public float directionalIntensity = 1; + [Range(1, 8)] + public float directionalFalloff = 1; + [ColorUsage(false, true)] + public Color directionalColor = new Color(1f, 0.75f, 0.5f, 1f); + + [StyledCategory("Noise")] + public bool categoryNoise; + + public FogNoiseMode noiseMode = FogNoiseMode.Procedural3D; + [Range(0, 1)] + public float noiseIntensity = 1; + public float noiseDistanceEnd = 50; + public float noiseScale = 30; + public Vector3 noiseSpeed = new Vector3(0.5f, 0f, 0.5f); + + [StyledCategory("Advanced")] + public bool categoryAdvanced; + + public bool manualPositionAndScale = false; + public int renderPriority = 1; + + [StyledSpace(5)] + public bool styledSpace0; + + Material localMaterial; + Material blendMaterial; + Material globalMaterial; + Material missingMaterial; + Material currentMaterial; + [HideInInspector] + public Material overrideMaterial; + [HideInInspector] + public float overrideCamToVolumeDistance = 1f; + [HideInInspector] + public float overrideVolumeDistanceFade = 0f; + + [HideInInspector] + public int version = 0; + + // Deprecated + [HideInInspector] + public FogDirectionalMode directionalMode = FogDirectionalMode.On; + + void Awake() + { + if (version < 180) + { + directionalIntensity = directionalIntensity * (int)directionalMode; + version = 180; + } + + gameObject.name = "Height Fog Global"; + + gameObject.transform.position = Vector3.zero; + gameObject.transform.rotation = Quaternion.identity; + + GetCamera(); + GetDirectional(); + + if (mainCamera != null) + { + if (mainCamera.depthTextureMode != DepthTextureMode.Depth || mainCamera.depthTextureMode != DepthTextureMode.DepthNormals) + { + mainCamera.depthTextureMode = DepthTextureMode.Depth; + } + } + else + { + Debug.Log("[Atmospheric Height Fog] Camera not found! Make sure you have a camera in the scene or your camera has the MainCamera tag!"); + } + + var sphereMeshGO = GameObject.CreatePrimitive(PrimitiveType.Sphere); + var sphereMesh = sphereMeshGO.GetComponent().sharedMesh; + DestroyImmediate(sphereMeshGO); + + gameObject.GetComponent().sharedMesh = sphereMesh; + + localMaterial = new Material(Shader.Find("BOXOPHOBIC/Atmospherics/Height Fog Preset")); + localMaterial.name = "Local"; + + overrideMaterial = new Material(localMaterial); + overrideMaterial.name = "Override"; + + blendMaterial = new Material(localMaterial); + blendMaterial.name = "Blend"; + + globalMaterial = new Material(Shader.Find("Hidden/BOXOPHOBIC/Atmospherics/Height Fog Global")); + globalMaterial.name = "Height Fog Global"; + + missingMaterial = Resources.Load("Height Fog Preset"); + + gameObject.GetComponent().sharedMaterial = globalMaterial; + } + + void OnEnable() + { + gameObject.GetComponent().enabled = true; + } + + void OnDisable() + { + gameObject.GetComponent().enabled = false; + Shader.SetGlobalFloat("AHF_FogIntensity", 0); + } + + void OnDestroy() + { + Shader.SetGlobalFloat("AHF_FogIntensity", 0); + } + + void Update() + { + if (mainCamera == null) + { + Debug.Log("[Atmospheric Height Fog] " + "Make sure you set scene camera tag to Main Camera for the fog to work!"); + return; + } + + if (!manualPositionAndScale) + { + SetFogSphereSize(); + SetFogSpherePosition(); + } + + currentMaterial = localMaterial; + + if (fogMode == FogMode.UseScriptSettings) + { + SetLocalMaterial(); + + messageTimeOfDay = false; + messagePreset = false; + } + else if (fogMode == FogMode.UsePresetSettings) + { + if (presetMaterial != null && presetMaterial.HasProperty("_IsHeightFogPreset")) + { + currentMaterial = presetMaterial; + messagePreset = false; + } + else + { + currentMaterial = missingMaterial; + messagePreset = true; + } + + messageTimeOfDay = false; + } + else if (fogMode == FogMode.UseTimeOfDay) + { + if (presetDay != null && presetDay.HasProperty("_IsHeightFogPreset") && presetNight != null && presetNight.HasProperty("_IsHeightFogPreset")) + { + currentMaterial.Lerp(presetDay, presetNight, timeOfDay); + messageTimeOfDay = false; + } + else + { + currentMaterial = missingMaterial; + messageTimeOfDay = true; + } + + messagePreset = false; + } + + if (mainDirectional != null) + { + currentMaterial.SetVector("_DirectionalDir", -mainDirectional.transform.forward); + } + else + { + currentMaterial.SetVector("_DirectionalDir", Vector4.zero); + } + + if (overrideCamToVolumeDistance > overrideVolumeDistanceFade) + { + blendMaterial.CopyPropertiesFromMaterial(currentMaterial); + } + else if (overrideCamToVolumeDistance < overrideVolumeDistanceFade) + { + var lerp = 1 - (overrideCamToVolumeDistance / overrideVolumeDistanceFade); + blendMaterial.Lerp(currentMaterial, overrideMaterial, lerp); + } + + SetGlobalMaterials(); + SetRenderQueue(); + } + + void GetCamera() + { + if (mainCamera == null) + { + mainCamera = Camera.main; + } + } + + void GetDirectional() + { + if (mainDirectional == null) + { + var allLights = FindObjectsOfType(); + var intensity = 0.0f; + + for (int i = 0; i < allLights.Length; i++) + { + if (allLights[i].type == LightType.Directional) + { + if (allLights[i].intensity > intensity) + { + mainDirectional = allLights[i]; + } + } + } + } + } + + void SetLocalMaterial() + { + localMaterial.SetFloat("_FogIntensity", fogIntensity); + + localMaterial.SetColor("_FogColorStart", fogColorStart); + localMaterial.SetColor("_FogColorEnd", fogColorEnd); + localMaterial.SetFloat("_FogColorDuo", fogColorDuo); + + localMaterial.SetFloat("_FogDistanceStart", fogDistanceStart); + localMaterial.SetFloat("_FogDistanceEnd", fogDistanceEnd); + localMaterial.SetFloat("_FogDistanceFalloff", fogDistanceFalloff); + + localMaterial.SetFloat("_FogHeightStart", fogHeightStart); + localMaterial.SetFloat("_FogHeightEnd", fogHeightEnd); + localMaterial.SetFloat("_FogHeightFalloff", fogHeightFalloff); + + localMaterial.SetFloat("_SkyboxFogIntensity", skyboxFogIntensity); + localMaterial.SetFloat("_SkyboxFogHeight", skyboxFogHeight); + localMaterial.SetFloat("_SkyboxFogFalloff", skyboxFogFalloff); + localMaterial.SetFloat("_SkyboxFogFill", skyboxFogFill); + + localMaterial.SetFloat("_DirectionalIntensity", directionalIntensity); + localMaterial.SetFloat("_DirectionalFalloff", directionalFalloff); + localMaterial.SetColor("_DirectionalColor", directionalColor); + + localMaterial.SetFloat("_NoiseIntensity", noiseIntensity); + localMaterial.SetFloat("_NoiseDistanceEnd", noiseDistanceEnd); + localMaterial.SetFloat("_NoiseScale", noiseScale); + localMaterial.SetVector("_NoiseSpeed", noiseSpeed); + + if (fogAxisMode == FogAxisMode.XAxis) + { + localMaterial.SetVector("_FogAxisOption", new Vector4(1, 0, 0, 0)); + } + else if (fogAxisMode == FogAxisMode.YAxis) + { + localMaterial.SetVector("_FogAxisOption", new Vector4(0, 1, 0, 0)); + } + else if (fogAxisMode == FogAxisMode.ZAxis) + { + localMaterial.SetVector("_FogAxisOption", new Vector4(0, 0, 1, 0)); + } + + if (fogLayersMode == FogLayersMode.MultiplyDistanceAndHeight) + { + localMaterial.SetFloat("_FogLayersMode", 0.0f); + } + else + { + localMaterial.SetFloat("_FogLayersMode", 1.0f); + } + + if (noiseMode == FogNoiseMode.Procedural3D) + { + localMaterial.SetFloat("_NoiseModeBlend", 1.0f); + } + else + { + localMaterial.SetFloat("_NoiseModeBlend", 0.0f); + } + } + + void SetGlobalMaterials() + { + if (blendMaterial.HasProperty("_IsHeightFogPreset") == false) + { + return; + } + + Shader.SetGlobalFloat("AHF_FogIntensity", blendMaterial.GetFloat("_FogIntensity")); + + Shader.SetGlobalVector("AHF_FogAxisOption", blendMaterial.GetVector("_FogAxisOption")); + Shader.SetGlobalFloat("AHF_FogLayersMode", blendMaterial.GetFloat("_FogLayersMode")); + + Shader.SetGlobalColor("AHF_FogColorStart", blendMaterial.GetColor("_FogColorStart")); + Shader.SetGlobalColor("AHF_FogColorEnd", blendMaterial.GetColor("_FogColorEnd")); + Shader.SetGlobalFloat("AHF_FogColorDuo", blendMaterial.GetFloat("_FogColorDuo")); + + Shader.SetGlobalFloat("AHF_FogDistanceStart", blendMaterial.GetFloat("_FogDistanceStart")); + Shader.SetGlobalFloat("AHF_FogDistanceEnd", blendMaterial.GetFloat("_FogDistanceEnd")); + Shader.SetGlobalFloat("AHF_FogDistanceFalloff", blendMaterial.GetFloat("_FogDistanceFalloff")); + + Shader.SetGlobalFloat("AHF_FogHeightStart", blendMaterial.GetFloat("_FogHeightStart")); + Shader.SetGlobalFloat("AHF_FogHeightEnd", blendMaterial.GetFloat("_FogHeightEnd")); + Shader.SetGlobalFloat("AHF_FogHeightFalloff", blendMaterial.GetFloat("_FogHeightFalloff")); + + Shader.SetGlobalFloat("AHF_SkyboxFogIntensity", blendMaterial.GetFloat("_SkyboxFogIntensity")); + Shader.SetGlobalFloat("AHF_SkyboxFogHeight", blendMaterial.GetFloat("_SkyboxFogHeight")); + Shader.SetGlobalFloat("AHF_SkyboxFogFalloff", blendMaterial.GetFloat("_SkyboxFogFalloff")); + Shader.SetGlobalFloat("AHF_SkyboxFogFill", blendMaterial.GetFloat("_SkyboxFogFill")); + + Shader.SetGlobalVector("AHF_DirectionalDir", blendMaterial.GetVector("_DirectionalDir")); + Shader.SetGlobalFloat("AHF_DirectionalIntensity", blendMaterial.GetFloat("_DirectionalIntensity")); + Shader.SetGlobalFloat("AHF_DirectionalFalloff", blendMaterial.GetFloat("_DirectionalFalloff")); + Shader.SetGlobalColor("AHF_DirectionalColor", blendMaterial.GetColor("_DirectionalColor")); + + Shader.SetGlobalFloat("AHF_NoiseModeBlend", blendMaterial.GetFloat("_NoiseModeBlend")); + Shader.SetGlobalFloat("AHF_NoiseIntensity", blendMaterial.GetFloat("_NoiseIntensity")); + Shader.SetGlobalFloat("AHF_NoiseDistanceEnd", blendMaterial.GetFloat("_NoiseDistanceEnd")); + Shader.SetGlobalFloat("AHF_NoiseScale", blendMaterial.GetFloat("_NoiseScale")); + Shader.SetGlobalVector("AHF_NoiseSpeed", blendMaterial.GetVector("_NoiseSpeed")); + + if (blendMaterial.GetFloat("_NoiseModeBlend") > 0) + { + Shader.DisableKeyword("AHF_NOISEMODE_OFF"); + Shader.EnableKeyword("AHF_NOISEMODE_PROCEDURAL3D"); + } + else + { + Shader.DisableKeyword("AHF_NOISEMODE_PROCEDURAL3D"); + Shader.EnableKeyword("AHF_NOISEMODE_OFF"); + } + } + + void SetFogSphereSize() + { + var cameraFar = mainCamera.farClipPlane - 1; + gameObject.transform.localScale = new Vector3(cameraFar, cameraFar, cameraFar); + } + + void SetFogSpherePosition() + { + transform.position = mainCamera.transform.position; + } + + void SetRenderQueue() + { + globalMaterial.renderQueue = 3000 + renderPriority; + } + } +} + diff --git a/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogGlobal.cs.meta b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogGlobal.cs.meta new file mode 100644 index 00000000..f03d0673 --- /dev/null +++ b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogGlobal.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d8023d2ae1fcb2948a39527720c2087b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 100 + icon: {fileID: 2800000, guid: 1ed6c69382334dd4e94337c8860e7116, type: 3} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogOverride.cs b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogOverride.cs new file mode 100644 index 00000000..abbbefe4 --- /dev/null +++ b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogOverride.cs @@ -0,0 +1,392 @@ +// Cristian Pop - https://boxophobic.com/ + +using UnityEngine; +using Boxophobic.StyledGUI; +using UnityEngine.Serialization; + +namespace AtmosphericHeightFog +{ + [ExecuteInEditMode] + [HelpURL("https://docs.google.com/document/d/1pIzIHIZ-cSh2ykODSZCbAPtScJ4Jpuu7lS3rNEHCLbc/edit#heading=h.hd5jt8lucuqq")] + public class HeightFogOverride : StyledMonoBehaviour + { + [StyledBanner(0.55f, 0.7f, 1f, "Height Fog Override", "", "https://docs.google.com/document/d/1pIzIHIZ-cSh2ykODSZCbAPtScJ4Jpuu7lS3rNEHCLbc/edit#heading=h.hd5jt8lucuqq")] + public bool styledBanner; + + [StyledMessage("Info", "The Height Fog Global object is missing from your scene! Please add it before using the Height Fog Override component!", 5, 0)] + public bool messageNoHeightFogGlobal = false; + + [StyledCategory("Volume")] + public bool categoryVolume; + + public float volumeDistanceFade = 3; + public bool volumeGizmoVisibility = true; + + [StyledCategory("Scene")] + public bool categoryScene; + + public Camera mainCamera; + public Light mainDirectional; + + [StyledCategory("Mode")] + public bool categoryMode; + + public FogMode fogMode = FogMode.UseScriptSettings; + + [StyledMessage("Info", "The Preset feature requires a material using the BOXOPHOBIC > Atmospherics > Fog Preset shader.", 10, 0)] + public bool messagePreset = false; + + [StyledMessage("Info", "The Time Of Day feature works by interpolating two Fog Preset materials using the BOXOPHOBIC > Atmospherics > Fog Preset shader. Please note that not all material properties can be interpolated properly!", 10, 0)] + public bool messageTimeOfDay = false; + + [Space(10)] + public Material presetMaterial; + + [Space(10)] + public Material presetDay; + public Material presetNight; + + [Space(10)] + [Range(0, 1)] + public float timeOfDay = 0; + + [StyledCategory("Fog")] + public bool categoryFog; + + [Range(0, 1)] + public float fogIntensity = 1; + + [Space(10)] + public FogAxisMode fogAxisMode = FogAxisMode.YAxis; + public FogLayersMode fogLayersMode = FogLayersMode.MultiplyDistanceAndHeight; + + [Space(10)] + [FormerlySerializedAs("fogColor")] + [ColorUsage(false, true)] + public Color fogColorStart = new Color(0.5f, 0.75f, 0.0f, 1.0f); + [ColorUsage(false, true)] + public Color fogColorEnd = new Color(0.75f, 1f, 0.0f, 1.0f); + [Range(0, 1)] + public float fogColorDuo = 0; + + [Space(10)] + public float fogDistanceStart = -100; + public float fogDistanceEnd = 100; + [Range(1, 8)] + public float fogDistanceFalloff = 1; + + [Space(10)] + public float fogHeightStart = 0; + public float fogHeightEnd = 100; + [Range(1f, 8f)] + public float fogHeightFalloff = 1; + + [StyledCategory("Skybox")] + public bool categorySkybox; + + [Range(0, 1)] + public float skyboxFogIntensity = 1; + [Range(0, 1)] + public float skyboxFogHeight = 1; + [Range(1, 8)] + public float skyboxFogFalloff = 1; + [Range(0, 1)] + public float skyboxFogFill = 0; + + [StyledCategory("Directional")] + public bool categoryDirectional; + + [Range(0, 1)] + public float directionalIntensity = 1; + [Range(1, 8)] + public float directionalFalloff = 1; + [ColorUsage(false, true)] + public Color directionalColor = new Color(1f, 0.75f, 0.5f, 1f); + + [StyledCategory("Noise")] + public bool categoryNoise; + + public FogNoiseMode noiseMode = FogNoiseMode.Procedural3D; + [Range(0, 1)] + public float noiseIntensity = 1; + public float noiseDistanceEnd = 50; + public float noiseScale = 30; + public Vector3 noiseSpeed = new Vector3(0.5f, 0f, 0.5f); + + [StyledSpace(5)] + public bool styledSpace0; + + Material localMaterial; + Material missingMaterial; + Material currentMaterial; + Collider volumeCollider; + HeightFogGlobal globalFog = null; + bool distanceSent = false; + + [HideInInspector] + public int version = 0; + + // Deprecated + [HideInInspector] + public FogDirectionalMode directionalMode = FogDirectionalMode.On; + + void Start() + { + if (version < 180) + { + directionalIntensity = directionalIntensity * (int)directionalMode; + version = 180; + } + + volumeCollider = GetComponent(); + + if (volumeCollider == null) + { + Debug.Log("[Atmospheric Height Fog] Please create override volumes from the GameObject menu > BOXOPHOBIC > Atmospheric Height Fog > Override!"); + DestroyImmediate(this); + } + + if (GameObject.Find("Height Fog Global") != null) + { + GameObject globalFogGO = GameObject.Find("Height Fog Global"); + globalFog = globalFogGO.GetComponent(); + + messageNoHeightFogGlobal = false; + } + else + { + messageNoHeightFogGlobal = true; + } + + GetDirectional(); + + localMaterial = new Material(Shader.Find("BOXOPHOBIC/Atmospherics/Height Fog Preset")); + localMaterial.name = "Local"; + + missingMaterial = Resources.Load("Height Fog Preset"); + + SetLocalMaterial(); + } + + void OnDisable() + { + if (globalFog != null) + { + globalFog.overrideCamToVolumeDistance = 1; + globalFog.overrideVolumeDistanceFade = 0; + } + } + + void OnDestroy() + { + if (globalFog != null) + { + globalFog.overrideCamToVolumeDistance = 1; + globalFog.overrideVolumeDistanceFade = 0; + } + } + + void Update() + { + GetCamera(); + + if (mainCamera == null || globalFog == null) + { + return; + } + + currentMaterial = localMaterial; + + if (fogMode == FogMode.UseScriptSettings) + { + SetLocalMaterial(); + + messageTimeOfDay = false; + messagePreset = false; + } + else if (fogMode == FogMode.UsePresetSettings) + { + if (presetMaterial != null && presetMaterial.HasProperty("_IsHeightFogPreset")) + { + currentMaterial = presetMaterial; + messagePreset = false; + } + else + { + currentMaterial = missingMaterial; + messagePreset = true; + } + + messageTimeOfDay = false; + } + else if (fogMode == FogMode.UseTimeOfDay) + { + if (presetDay != null && presetDay.HasProperty("_IsHeightFogPreset") && presetNight != null && presetNight.HasProperty("_IsHeightFogPreset")) + { + currentMaterial.Lerp(presetDay, presetNight, timeOfDay); + messageTimeOfDay = false; + } + else + { + currentMaterial = missingMaterial; + messageTimeOfDay = true; + } + + + messagePreset = false; + } + + if (mainDirectional != null) + { + currentMaterial.SetVector("_DirectionalDir", -mainDirectional.transform.forward); + } + else + { + currentMaterial.SetVector("_DirectionalDir", Vector4.zero); + } + + Vector3 camPos = mainCamera.transform.position; + Vector3 closestPos = volumeCollider.ClosestPoint(camPos); + + float dist = Vector3.Distance(camPos, closestPos); + + if (dist > volumeDistanceFade && distanceSent == false) + { + globalFog.overrideCamToVolumeDistance = Mathf.Infinity; + distanceSent = true; + } + else if (dist < volumeDistanceFade) + { + globalFog.overrideMaterial = currentMaterial; + globalFog.overrideCamToVolumeDistance = dist; + globalFog.overrideVolumeDistanceFade = volumeDistanceFade; + distanceSent = false; + } + } + + void OnDrawGizmos() + { + if (volumeCollider == null || !volumeGizmoVisibility) + { + return; + } + + var color = currentMaterial.GetColor("_FogColorStart"); + var mul = 2f; + + if (volumeCollider.GetType() == typeof(BoxCollider)) + { + var col = GetComponent(); + + Gizmos.color = new Color(color.r * mul, color.g * mul, color.b * mul, 1.0f); + Gizmos.DrawWireCube(transform.position, new Vector3(transform.lossyScale.x * col.size.x, transform.lossyScale.y * col.size.y, transform.lossyScale.z * col.size.z)); + + Gizmos.color = new Color(color.r * mul, color.g * mul, color.b * mul, 0.5f); + Gizmos.DrawWireCube(transform.position, new Vector3(transform.lossyScale.x * col.size.x + (volumeDistanceFade * 2), transform.lossyScale.y * col.size.y + (volumeDistanceFade * 2), transform.lossyScale.z * col.size.z + (volumeDistanceFade * 2))); + + } + else + { + var col = GetComponent(); + var scale = Mathf.Max(Mathf.Max(gameObject.transform.localScale.x, gameObject.transform.localScale.y), gameObject.transform.localScale.z); + + Gizmos.color = new Color(color.r * mul, color.g * mul, color.b * mul, 1.0f); + Gizmos.DrawWireSphere(transform.position, col.radius * scale); + + Gizmos.color = new Color(color.r * mul, color.g * mul, color.b * mul, 0.5f); + Gizmos.DrawWireSphere(transform.position, col.radius * scale + volumeDistanceFade); + } + } + + void GetCamera() + { + if (mainCamera == null) + { + mainCamera = Camera.main; + } + } + + void GetDirectional() + { + if (mainDirectional == null) + { + var allLights = FindObjectsOfType(); + var intensity = 0.0f; + + for (int i = 0; i < allLights.Length; i++) + { + if (allLights[i].type == LightType.Directional) + { + if (allLights[i].intensity > intensity) + { + mainDirectional = allLights[i]; + } + } + } + } + } + + void SetLocalMaterial() + { + localMaterial.SetFloat("_FogIntensity", fogIntensity); + + localMaterial.SetColor("_FogColorStart", fogColorStart); + localMaterial.SetColor("_FogColorEnd", fogColorEnd); + localMaterial.SetFloat("_FogColorDuo", fogColorDuo); + + localMaterial.SetFloat("_FogDistanceStart", fogDistanceStart); + localMaterial.SetFloat("_FogDistanceEnd", fogDistanceEnd); + localMaterial.SetFloat("_FogDistanceFalloff", fogDistanceFalloff); + + localMaterial.SetFloat("_FogHeightStart", fogHeightStart); + localMaterial.SetFloat("_FogHeightEnd", fogHeightEnd); + localMaterial.SetFloat("_FogHeightFalloff", fogHeightFalloff); + + localMaterial.SetFloat("_SkyboxFogIntensity", skyboxFogIntensity); + localMaterial.SetFloat("_SkyboxFogHeight", skyboxFogHeight); + localMaterial.SetFloat("_SkyboxFogFalloff", skyboxFogFalloff); + localMaterial.SetFloat("_SkyboxFogFill", skyboxFogFill); + + localMaterial.SetFloat("_DirectionalIntensity", directionalIntensity); + localMaterial.SetFloat("_DirectionalFalloff", directionalFalloff); + localMaterial.SetColor("_DirectionalColor", directionalColor); + + localMaterial.SetFloat("_NoiseIntensity", noiseIntensity); + localMaterial.SetFloat("_NoiseDistanceEnd", noiseDistanceEnd); + localMaterial.SetFloat("_NoiseScale", noiseScale); + localMaterial.SetVector("_NoiseSpeed", noiseSpeed); + + if (fogAxisMode == FogAxisMode.XAxis) + { + localMaterial.SetVector("_FogAxisOption", new Vector4(1, 0, 0, 0)); + } + else if (fogAxisMode == FogAxisMode.YAxis) + { + localMaterial.SetVector("_FogAxisOption", new Vector4(0, 1, 0, 0)); + } + else if (fogAxisMode == FogAxisMode.ZAxis) + { + localMaterial.SetVector("_FogAxisOption", new Vector4(0, 0, 1, 0)); + } + + if (fogLayersMode == FogLayersMode.MultiplyDistanceAndHeight) + { + localMaterial.SetFloat("_FogLayersMode", 0.0f); + } + else + { + localMaterial.SetFloat("_FogLayersMode", 1.0f); + } + + if (noiseMode == FogNoiseMode.Procedural3D) + { + localMaterial.SetFloat("_NoiseModeBlend", 1.0f); + } + else + { + localMaterial.SetFloat("_NoiseModeBlend", 0.0f); + } + } + } +} + diff --git a/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogOverride.cs.meta b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogOverride.cs.meta new file mode 100644 index 00000000..86ea5b0e --- /dev/null +++ b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogOverride.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: ac1c26670b7bd6a47ac695141473ab42 +timeCreated: 1568146209 +licenseType: Store +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {fileID: 2800000, guid: 500f3eaec95c62949b969478343b29d4, type: 3} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogPerObject.cs b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogPerObject.cs new file mode 100644 index 00000000..6a014d97 --- /dev/null +++ b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogPerObject.cs @@ -0,0 +1,186 @@ +// Cristian Pop - https://boxophobic.com/ + +using UnityEngine; +using Boxophobic; +using Boxophobic.StyledGUI; +#if UNITY_EDITOR +using UnityEditor; +#endif + +namespace AtmosphericHeightFog +{ + [ExecuteInEditMode] + [DisallowMultipleComponent] + [HelpURL("https://docs.google.com/document/d/1pIzIHIZ-cSh2ykODSZCbAPtScJ4Jpuu7lS3rNEHCLbc/edit#heading=h.pzat2b29j9a0")] + public class HeightFogPerObject : StyledMonoBehaviour + { + [StyledBanner(0.474f, 0.709f, 0.901f, "Height Fog Per Object", "", "https://docs.google.com/document/d/1pIzIHIZ-cSh2ykODSZCbAPtScJ4Jpuu7lS3rNEHCLbc/edit#heading=h.pzat2b29j9a0")] + public bool styledBanner; + + [StyledMessage("Info", "The Object does not have a Mesh Renderer!", 5, 5)] + public bool messageNoRenderer = false; + + [StyledMessage("Info", "Objects using multiple materials are not supported!", 5, 5)] + public bool messageMultiMaterials = false; + + [StyledMessage("Info", "The Object does not have a Material assigned!", 5, 5)] + public bool messageNoMaterial = false; + + [StyledMessage("Info", "Please note that the Height Fog Per Object option will not work for all transparent objects. Available in Play mode only. Please read the documentation for more!", 0, 0)] + public bool messageTransparencySupport = true; + + [StyledCategory("Settings")] + public bool categoryMaterial; + + public Material customFogMaterial = null; + + [StyledMessage("Info", "The is not a valid Height Fog material! Please assign the correct shader first!", 5, 0)] + public bool messageInvalidFogMaterial = false; + + [StyledSpace(5)] + public bool styledSpace0; + + int transparencyRenderQueue = 3002; + + Material originalMaterial; + Material instanceMaterial; + Material transparencyMaterial; + + GameObject transparencyGO; + + void Awake() + { + if (GameObjectIsInvalid()) + { + return; + } + +#if UNITY_EDITOR + if (Application.isPlaying == false) + { + GameObjectDisableBathingFlag(); + return; + } +#endif + + transparencyGO = new GameObject(gameObject.name + " (Height Fog Object)"); + + transparencyGO.transform.parent = gameObject.transform; + transparencyGO.transform.localPosition = Vector3.zero; + transparencyGO.transform.localRotation = Quaternion.identity; + transparencyGO.transform.localScale = Vector3.one; + + transparencyGO.AddComponent(); + transparencyGO.AddComponent(); + + transparencyGO.GetComponent().sharedMesh = gameObject.GetComponent().sharedMesh; + + Material originalMaterial = gameObject.GetComponent().sharedMaterial; + + instanceMaterial = new Material(originalMaterial); + instanceMaterial.name = originalMaterial.name + " (Instance)"; + //instanceMaterial.SetOverrideTag("DisableBatching", "True"); + + if (customFogMaterial == null) + { + transparencyMaterial = new Material(instanceMaterial); + transparencyMaterial.shader = Shader.Find("BOXOPHOBIC/Atmospherics/Height Fog Per Object"); + transparencyMaterial.name = originalMaterial.name + " (Generic Fog)"; + } + else if (customFogMaterial != null) + { + if (customFogMaterial.HasProperty("_IsHeightFogShader")) + { + transparencyMaterial = customFogMaterial; + transparencyMaterial.name = originalMaterial.name + " (Custom Fog)"; + } + else + { + transparencyMaterial = new Material(instanceMaterial); + transparencyMaterial.shader = Shader.Find("BOXOPHOBIC/Atmospherics/Height Fog Per Object"); + transparencyMaterial.name = originalMaterial.name + " (Generic Fog)"; + } + } + + if (transparencyMaterial.HasProperty("_IsStandardPipeline")) + { + transparencyRenderQueue = 3002; + } + else + { + transparencyRenderQueue = 3102; + } + + instanceMaterial.renderQueue = transparencyRenderQueue; + transparencyMaterial.renderQueue = transparencyRenderQueue + 1; + + gameObject.GetComponent().material = instanceMaterial; + transparencyGO.GetComponent().material = transparencyMaterial; + + } +#if UNITY_EDITOR + void Update() + { + if (Application.isPlaying == true) + { + return; + } + + if (gameObject.isStatic) + { + GameObjectDisableBathingFlag(); + } + + if (customFogMaterial == null) + { + messageInvalidFogMaterial = false; + } + else if (customFogMaterial != null) + { + if (customFogMaterial.HasProperty("_IsHeightFogShader") == false) + { + messageInvalidFogMaterial = true; + } + else + { + messageInvalidFogMaterial = false; + } + } + } +#endif + + bool GameObjectIsInvalid() + { + bool invalid = false; + + if (gameObject.GetComponent() == null) + { + messageNoRenderer = true; + invalid = true; + } + + else if (gameObject.GetComponent().sharedMaterials.Length > 1) + { + messageMultiMaterials = true; + invalid = true; + } + + else if (gameObject.GetComponent().sharedMaterial == null) + { + messageNoMaterial = true; + invalid = true; + } + + return invalid; + } + +#if UNITY_EDITOR + void GameObjectDisableBathingFlag() + { + StaticEditorFlags flags = GameObjectUtility.GetStaticEditorFlags(gameObject); + flags = flags & ~(StaticEditorFlags.BatchingStatic); + GameObjectUtility.SetStaticEditorFlags(gameObject, flags); + } +#endif + } +} diff --git a/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogPerObject.cs.meta b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogPerObject.cs.meta new file mode 100644 index 00000000..8e2a36c7 --- /dev/null +++ b/Assets/BOXOPHOBIC/Atmospheric Height Fog/Core/Runtime/HeightFogPerObject.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 002595e91f609a246845cbf8a32fd288 +timeCreated: 1568222395 +licenseType: Store +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: -333 + icon: {fileID: 2800000, guid: e5ce702f9a95f4f42a9ebd790113406a, type: 3} + userData: + assetBundleName: + assetBundleVariant: -- cgit v1.1-26-g67d0