DynamicFogProfile.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace DynamicFogAndMist {
  6. [CreateAssetMenu (fileName = "DynamicFogProfile", menuName = "Dynamic Fog Profile", order = 100)]
  7. public class DynamicFogProfile : ScriptableObject {
  8. public FOG_TYPE effectType = FOG_TYPE.DesktopFogPlusWithSkyHaze;
  9. public bool enableDithering = false;
  10. [Range (0, 0.2f)]
  11. public float ditherStrength = 0.03f;
  12. [Range (0, 1)]
  13. public float
  14. alpha = 1.0f;
  15. [Range (0, 1)]
  16. public float
  17. noiseStrength = 0.5f;
  18. [Range (0.01f, 1)]
  19. public float
  20. noiseScale = 0.1f;
  21. [Range (0, 0.999f)]
  22. public float
  23. distance = 0.1f;
  24. [Range (0.0001f, 2f)]
  25. public float
  26. distanceFallOff = 0.01f;
  27. [Range (0, 1.2f)]
  28. public float
  29. maxDistance = 0.999f;
  30. [Range (0.0001f, 0.5f)]
  31. public float
  32. maxDistanceFallOff = 0f;
  33. [Range (0, 500)]
  34. public float
  35. height = 1f;
  36. [Range (0, 500)]
  37. public float
  38. maxHeight = 100f;
  39. // used in orthogonal fog
  40. [Range (0.0001f, 1)]
  41. public float
  42. heightFallOff = 0.1f;
  43. public float
  44. baselineHeight = 0;
  45. public bool clipUnderBaseline = false;
  46. [Range (0, 15)]
  47. public float
  48. turbulence = 0.1f;
  49. [Range (0, 5.0f)]
  50. public float
  51. speed = 0.1f;
  52. public Vector3 windDirection = new Vector3 (1, 0, 1);
  53. public Color color = Color.white;
  54. public Color color2 = Color.gray;
  55. [Range (0, 500)]
  56. public float
  57. skyHaze = 50f;
  58. [Range (0, 1)]
  59. public float
  60. skySpeed = 0.3f;
  61. [Range (0, 1)]
  62. public float
  63. skyNoiseStrength = 0.1f;
  64. [Range (0, 1)]
  65. public float
  66. skyAlpha = 1.0f;
  67. public bool useXZDistance = false;
  68. [Range (0, 1)]
  69. public float scattering = 0.7f;
  70. public Color scatteringColor = new Color (1, 1, 0.8f);
  71. /// <summary>
  72. /// Applies profile settings to a given fog instance
  73. /// </summary>
  74. public void Load (DynamicFog fog) {
  75. fog.preset = FOG_PRESET.Custom;
  76. fog.effectType = effectType;
  77. fog.enableDithering = enableDithering;
  78. fog.ditherStrength = ditherStrength;
  79. fog.alpha = alpha;
  80. fog.noiseStrength = noiseStrength;
  81. fog.noiseScale = noiseScale;
  82. fog.distance = distance;
  83. fog.distanceFallOff = distanceFallOff;
  84. fog.maxDistance = maxDistance;
  85. fog.maxDistanceFallOff = maxDistanceFallOff;
  86. fog.height = height;
  87. fog.maxHeight = maxHeight;
  88. fog.heightFallOff = heightFallOff;
  89. fog.baselineHeight = baselineHeight;
  90. fog.clipUnderBaseline = clipUnderBaseline;
  91. fog.turbulence = turbulence;
  92. fog.speed = speed;
  93. fog.windDirection = windDirection;
  94. fog.color = color;
  95. fog.color2 = color2;
  96. fog.skyHaze = skyHaze;
  97. fog.skySpeed = skySpeed;
  98. fog.skyNoiseStrength = skyNoiseStrength;
  99. fog.skyAlpha = skyAlpha;
  100. fog.useXZDistance = useXZDistance;
  101. fog.scattering = scattering;
  102. fog.scatteringColor = scatteringColor;
  103. }
  104. /// <summary>
  105. /// Replaces profile settings with current fog configuration
  106. /// </summary>
  107. public void Save (DynamicFog fog) {
  108. effectType = fog.effectType;
  109. enableDithering = fog.enableDithering;
  110. ditherStrength = fog.ditherStrength;
  111. alpha = fog.alpha;
  112. noiseStrength = fog.noiseStrength;
  113. noiseScale = fog.noiseScale;
  114. distance = fog.distance;
  115. distanceFallOff = fog.distanceFallOff;
  116. maxDistance = fog.maxDistance;
  117. maxDistanceFallOff = fog.maxDistanceFallOff;
  118. height = fog.height;
  119. maxHeight = fog.maxHeight;
  120. heightFallOff = fog.heightFallOff;
  121. baselineHeight = fog.baselineHeight;
  122. clipUnderBaseline = fog.clipUnderBaseline;
  123. turbulence = fog.turbulence;
  124. speed = fog.speed;
  125. windDirection = fog.windDirection;
  126. color = fog.color;
  127. color2 = fog.color2;
  128. skyHaze = fog.skyHaze;
  129. skySpeed = fog.skySpeed;
  130. skyNoiseStrength = fog.skyNoiseStrength;
  131. skyAlpha = fog.skyAlpha;
  132. useXZDistance = fog.useXZDistance;
  133. scattering = fog.scattering;
  134. scatteringColor = fog.scatteringColor;
  135. }
  136. /// <summary>
  137. /// Lerps between profile1 and profile2 using t as the transition amount (0..1) and assign the values to given fog
  138. /// </summary>
  139. public static void Lerp (DynamicFogProfile profile1, DynamicFogProfile profile2, float t, DynamicFog fog) {
  140. if (t < 0)
  141. t = 0;
  142. else if (t > 1f)
  143. t = 1f;
  144. fog.enableDithering = t < 0.5f ? profile1.enableDithering : profile2.enableDithering;
  145. fog.ditherStrength = profile1.ditherStrength * (1f - t) + profile2.ditherStrength * t;
  146. fog.alpha = profile1.alpha * (1f - t) + profile2.alpha * t;
  147. fog.noiseStrength = profile1.noiseStrength * (1f - t) + profile2.noiseStrength * t;
  148. fog.noiseScale = profile1.noiseScale * (1f - t) + profile2.noiseScale * t;
  149. fog.distance = profile1.distance * (1f - t) + profile2.distance * t;
  150. fog.distanceFallOff = profile1.distanceFallOff * (1f - t) + profile2.distanceFallOff * t;
  151. fog.maxDistance = profile1.maxDistance * (1f - t) + profile2.maxDistance * t;
  152. fog.maxDistanceFallOff = profile1.maxDistanceFallOff * (1f - t) + profile2.maxDistanceFallOff * t;
  153. fog.height = profile1.height * (1f - t) + profile2.height * t;
  154. fog.maxHeight = profile1.maxHeight * (1f - t) + profile2.maxHeight * t;
  155. fog.heightFallOff = profile1.heightFallOff * (1f - t) + profile2.heightFallOff * t;
  156. fog.baselineHeight = profile1.baselineHeight * (1f - t) + profile2.baselineHeight * t;
  157. fog.clipUnderBaseline = t < 0.5f ? profile1.clipUnderBaseline : profile2.clipUnderBaseline;
  158. fog.turbulence = profile1.turbulence * (1f - t) + profile2.turbulence * t;
  159. fog.speed = profile1.speed * (1f - t) + profile2.speed * t;
  160. fog.windDirection = profile1.windDirection * (1f - t) + profile2.windDirection * t;
  161. fog.color = profile1.color * (1f - t) + profile2.color * t;
  162. fog.color2 = profile1.color2 * (1f - t) + profile2.color * t;
  163. fog.skyHaze = profile1.skyHaze * (1f - t) + profile2.skyHaze * t;
  164. fog.skySpeed = profile1.skySpeed * (1f - t) + profile2.skySpeed * t;
  165. fog.skyNoiseStrength = profile1.skyNoiseStrength * (1f - t) + profile2.skyNoiseStrength * t;
  166. fog.skyAlpha = profile1.skyAlpha * (1f - t) + profile2.skyAlpha * t;
  167. fog.useXZDistance = t < 0.5f ? profile1.useXZDistance : profile2.useXZDistance;
  168. fog.scattering = profile1.scattering * (1f - t) + profile2.scattering * t;
  169. fog.scatteringColor = profile1.scatteringColor * (1f - t) + profile2.scatteringColor * t;
  170. }
  171. }
  172. }