VolumetricFogProfile.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //------------------------------------------------------------------------------------------------------------------
  2. // Volumetric Fog & Mist Scriptable Object
  3. // Created by Ramiro Oliva (Kronnect)
  4. //------------------------------------------------------------------------------------------------------------------
  5. using UnityEngine;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. namespace VolumetricFogAndMist {
  10. [CreateAssetMenu(fileName = "VolumetricFogProfile", menuName = "Volumetric Fog Profile", order = 100) ]
  11. public class VolumetricFogProfile: ScriptableObject {
  12. public LIGHTING_MODEL lightingModel = LIGHTING_MODEL.Classic;
  13. public bool sunCopyColor = true;
  14. [Range(0, 1.25f)]
  15. public float density = 1.0f;
  16. [Range(0, 1f)]
  17. public float noiseStrength = 0.8f;
  18. [Range(0, 500)]
  19. public float height = 4f;
  20. public float baselineHeight = 0f;
  21. [Range(0, 1000)]
  22. public float distance = 0f;
  23. [Range(0, 5f)]
  24. public float distanceFallOff = 0f;
  25. [Range(0, 2000)]
  26. public float maxFogLength = 1000f;
  27. [Range(0, 1f)]
  28. public float maxFogLengthFallOff = 0f;
  29. public bool baselineRelativeToCamera = false;
  30. [Range(0, 1f)]
  31. public float baselineRelativeToCameraDelay = 0;
  32. [Range(0.2f, 10f)]
  33. public float noiseScale = 1f;
  34. [Range(-0.3f, 1f)]
  35. public float noiseSparse = 0f;
  36. [Range(0, 1.05f)]
  37. public float alpha = 1f;
  38. public Color color = new Color (0.89f, 0.89f, 0.89f, 1);
  39. public Color specularColor = new Color (1, 1, 0.8f, 1);
  40. [Range(0, 1f)]
  41. public float specularThreshold = 0.6f;
  42. [Range(0, 1f)]
  43. public float specularIntensity = 0.2f;
  44. public Vector3 lightDirection = new Vector3 (1, 0, -1);
  45. [Range(-1f, 3f)]
  46. public float lightIntensity = 0.2f;
  47. public Color lightColor = Color.white;
  48. [Range(0, 1f)]
  49. public float speed = 0.01f;
  50. public bool useRealTime;
  51. public Vector3 windDirection = new Vector3 (-1, 0, 0);
  52. [Range(0, 10f)]
  53. public float turbulenceStrength = 0f;
  54. public bool useXYPlane = false;
  55. public Color skyColor = new Color (0.89f, 0.89f, 0.89f, 1);
  56. [Range(0f, 1000f)]
  57. public float skyHaze = 50f;
  58. [Range(0, 1f)]
  59. public float skySpeed = 0.3f;
  60. [Range(0, 1f)]
  61. public float skyNoiseStrength = 0.1f;
  62. [Range(0, 1f)]
  63. public float skyAlpha = 1f;
  64. public float stepping = 12f;
  65. public float steppingNear = 1f;
  66. public bool dithering = false;
  67. public float ditherStrength = 0.75f;
  68. /// <summary>
  69. /// Applies profile settings
  70. /// </summary>
  71. /// <param name="fog">Fog.</param>
  72. public void Load(VolumetricFog fog) {
  73. // Fog Geo
  74. fog.density = density;
  75. fog.noiseStrength = noiseStrength;
  76. fog.height = height;
  77. fog.baselineHeight = baselineHeight;
  78. fog.distance = distance;
  79. fog.distanceFallOff = distanceFallOff;
  80. fog.maxFogLength = maxFogLength;
  81. fog.maxFogLengthFallOff = maxFogLengthFallOff;
  82. fog.baselineRelativeToCamera = baselineRelativeToCamera;
  83. fog.baselineRelativeToCameraDelay = baselineRelativeToCameraDelay;
  84. fog.noiseScale = noiseScale;
  85. fog.noiseSparse = noiseSparse;
  86. fog.useXYPlane = useXYPlane;
  87. // Fog Colors
  88. fog.sunCopyColor = sunCopyColor;
  89. fog.alpha = alpha;
  90. fog.color = color;
  91. fog.specularColor = specularColor;
  92. fog.specularThreshold = specularThreshold;
  93. fog.specularIntensity = specularIntensity;
  94. fog.lightDirection = lightDirection;
  95. fog.lightIntensity = lightIntensity;
  96. fog.lightColor = lightColor;
  97. // Fog animation
  98. fog.speed = speed;
  99. fog.windDirection = windDirection;
  100. fog.turbulenceStrength = turbulenceStrength;
  101. fog.useRealTime = useRealTime;
  102. // Fog sky
  103. fog.skyColor = skyColor;
  104. fog.skyHaze = skyHaze;
  105. fog.skySpeed = skySpeed;
  106. fog.skyNoiseStrength = skyNoiseStrength;
  107. fog.skyAlpha = skyAlpha;
  108. // Optimization
  109. fog.stepping = stepping;
  110. fog.steppingNear = steppingNear;
  111. fog.dithering = dithering;
  112. fog.ditherStrength = ditherStrength;
  113. }
  114. /// <summary>
  115. /// Replaces profile settings with current fog configuration
  116. /// </summary>
  117. public void Save(VolumetricFog fog) {
  118. // Fog Geo
  119. density = fog.density;
  120. noiseStrength = fog.noiseStrength;
  121. height = fog.height;
  122. baselineHeight = fog.baselineHeight;
  123. distance = fog.distance;
  124. distanceFallOff = fog.distanceFallOff;
  125. maxFogLength = fog.maxFogLength;
  126. maxFogLengthFallOff = fog.maxFogLengthFallOff;
  127. baselineRelativeToCamera = fog.baselineRelativeToCamera;
  128. baselineRelativeToCameraDelay = fog.baselineRelativeToCameraDelay;
  129. noiseScale = fog.noiseScale;
  130. noiseSparse = fog.noiseSparse;
  131. useXYPlane = fog.useXYPlane;
  132. // Fog Colors
  133. sunCopyColor = fog.sunCopyColor;
  134. alpha = fog.alpha;
  135. color = fog.color;
  136. specularColor = fog.specularColor;
  137. specularThreshold = fog.specularThreshold;
  138. specularIntensity = fog.specularIntensity;
  139. lightDirection = fog.lightDirection;
  140. lightIntensity = fog.lightIntensity;
  141. lightColor = fog.lightColor;
  142. // Fog animation
  143. speed = fog.speed;
  144. windDirection = fog.windDirection;
  145. turbulenceStrength = fog.turbulenceStrength;
  146. useRealTime = fog.useRealTime;
  147. // Fog sky
  148. skyColor = fog.skyColor;
  149. skyHaze = fog.skyHaze;
  150. skySpeed = fog.skySpeed;
  151. skyNoiseStrength = fog.skyNoiseStrength;
  152. skyAlpha = fog.skyAlpha;
  153. // Optimization
  154. stepping = fog.stepping;
  155. steppingNear = fog.steppingNear;
  156. dithering = fog.dithering;
  157. ditherStrength = fog.ditherStrength;
  158. }
  159. /// <summary>
  160. /// Lerps between profile1 and profile2 using t as the transition amount (0..1) and assign the values to given fog
  161. /// </summary>
  162. public static void Lerp(VolumetricFogProfile profile1, VolumetricFogProfile profile2, float t, VolumetricFog fog) {
  163. if (t<0) t = 0; else if (t>1f) t = 1f;
  164. // Fog Geo
  165. fog.density = profile1.density * (1f-t) + profile2.density * t;
  166. fog.noiseStrength = profile1.noiseStrength * (1f-t) + profile2.noiseStrength * t;;
  167. fog.height = profile1.height * (1f-t) + profile2.height * t;;
  168. fog.baselineHeight = profile1.baselineHeight * (1f-t) + profile2.baselineHeight * t;
  169. fog.distance = profile1.baselineHeight * (1f-t) + profile2.distance * t;;
  170. fog.distanceFallOff = profile1.distanceFallOff * (1f-t) + profile2.distanceFallOff * t;
  171. fog.maxFogLength = profile1.maxFogLength * (1f-t) + profile2.maxFogLength * t;
  172. fog.maxFogLengthFallOff = profile1.maxFogLengthFallOff * (1f-t) + profile2.maxFogLengthFallOff * t;
  173. fog.baselineRelativeToCamera = t<0.5f ? profile1.baselineRelativeToCamera : profile2.baselineRelativeToCamera;
  174. fog.baselineRelativeToCameraDelay = profile1.baselineRelativeToCameraDelay * (1f-t) + profile2.baselineRelativeToCameraDelay * t;
  175. fog.noiseScale = profile1.noiseScale * (1f-t) + profile2.noiseScale * t;
  176. fog.noiseSparse = profile1.noiseSparse * (1f-t) + profile2.noiseSparse * t;
  177. // fog.useXYPlane = t<0.5f ? profile1.useXYPlane : profile2.useXYPlane;
  178. // Fog Colors
  179. fog.sunCopyColor = t<0.5f ? profile1.sunCopyColor : profile2.sunCopyColor;
  180. fog.alpha = profile1.alpha * (1f-t) + profile2.alpha * t;
  181. fog.color = profile1.color * (1f-t) + profile2.color * t;
  182. fog.specularColor = profile1.specularColor * (1f-t) + profile2.color * t;
  183. fog.specularThreshold = profile1.specularThreshold * (1f-t) + profile2.specularThreshold * t;
  184. fog.specularIntensity = profile1.specularIntensity * (1f-t) + profile2.specularIntensity * t;
  185. fog.lightDirection = profile1.lightDirection * (1f-t) + profile2.lightDirection * t;
  186. fog.lightIntensity = profile1.lightIntensity * (1f-t) + profile2.lightIntensity * t;
  187. fog.lightColor = profile1.lightColor * (1f-t) + profile2.lightColor * t;
  188. // Fog animation
  189. fog.speed = profile1.speed * (1f-t) + profile2.speed * t;
  190. fog.windDirection = profile1.windDirection * (1f-t) + profile2.windDirection * t;
  191. fog.turbulenceStrength = profile1.turbulenceStrength * (1f-t) + profile2.turbulenceStrength * t;
  192. // Fog sky
  193. fog.skyColor = profile1.skyColor * (1f-t) + profile2.skyColor * t;
  194. fog.skyHaze = profile1.skyHaze * (1f-t) + profile2.skyHaze * t;
  195. fog.skySpeed = profile1.skySpeed * (1f-t) + profile2.skySpeed * t;
  196. fog.skyNoiseStrength = profile1.skyNoiseStrength * (1f-t) + profile2.skyNoiseStrength * t;
  197. fog.skyAlpha = profile1.skyAlpha * (1f-t) + profile2.skyAlpha * t;
  198. // Optimization
  199. fog.stepping = profile1.stepping * (1f-t) + profile2.stepping * t;
  200. fog.steppingNear = profile1.steppingNear * (1f-t) + profile2.steppingNear * t;
  201. fog.dithering = t<0.5f ? profile1.dithering: profile2.dithering;
  202. fog.ditherStrength = profile1.ditherStrength * (1f-t) + profile2.ditherStrength * t;
  203. }
  204. }
  205. }