VolumetricFog2DURP.shader 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. Shader "VolumetricFog2/VolumetricFog2DURP"
  2. {
  3. Properties
  4. {
  5. [HideInInspector] _MainTex("Noise Texture", 2D) = "white" {}
  6. [HideInInspector] _DetailTex("Detail Texture", 3D) = "white" {}
  7. [HideInInspector] _NoiseScale("Noise Scale", Float) = 0.025
  8. [HideInInspector] _NoiseFinalMultiplier("Noise Scale", Float) = 1.0
  9. [HideInInspector] _NoiseStrength("Noise Strength", Float) = 1.0
  10. [HideInInspector] _Color("Color", Color) = (1,1,1)
  11. [HideInInspector] _Density("Density", Float) = 1.0
  12. [HideInInspector] _DeepObscurance("Deep Obscurance", Range(0, 2)) = 0.7
  13. [HideInInspector] _LightColor("Light Color", Color) = (1,1,1)
  14. [HideInInspector] _LightDiffusionPower("Sun Diffusion Power", Range(1, 64)) = 32
  15. [HideInInspector] _LightDiffusionIntensity("Sun Diffusion Intensity", Range(0, 1)) = 0.4
  16. [HideInInspector] _SunDir("Sun Direction", Vector) = (1,0,0)
  17. [HideInInspector] _ShadowData("Shadow Data", Vector) = (0.5, 0, 62500)
  18. [HideInInspector] _WindDirection("Wind Direction", Vector) = (1, 0, 0)
  19. [HideInInspector] _DetailWindDirection("Detail Wind Direction", Vector) = (1, 0, 0)
  20. [HideInInspector] _RayMarchSettings("Raymarch Settings", Vector) = (2, 0.01, 1.0, 0.1)
  21. [HideInInspector] _BoundsCenter("Bounds Center", Vector) = (0,0,0)
  22. [HideInInspector] _BoundsExtents("Bounds Size", Vector) = (0,0,0)
  23. [HideInInspector] _BoundsBorder("Bounds Border", Vector) = (0,1,0)
  24. [HideInInspector] _BoundsData("Bounds Data", Vector) = (0,0,1)
  25. [HideInInspector] _DetailData("Detail Data", Vector) = (0.5, 4, -0.5, 0)
  26. [HideInInspector] _DetailColor("Detail Color", Color) = (0.5,0.5,0.5,0)
  27. [HideInInspector] _DetailOffset("Detail Offset", Float) = -0.5
  28. [HideInInspector] _DistanceData("Distance Data", Vector) = (0, 5, 1, 1)
  29. [HideInInspector] _DepthGradientTex("Depth Gradient Texture", 2D) = "white" {}
  30. [HideInInspector] _HeightGradientTex("Height Gradient Texture", 2D) = "white" {}
  31. [HideInInspector] _SpecularThreshold("Specular Threshold", Float) = 0.5
  32. [HideInInspector] _SpecularIntensity("Specular Intensity", Float) = 0
  33. [HideInInspector] _SpecularColor("Specular Color", Color) = (0.5,0.5,0.5,0)
  34. [HideInInspector] _FogOfWarCenterAdjusted("FoW Center Adjusted", Vector) = (0,0,0)
  35. [HideInInspector] _FogOfWarSize("FoW Size", Vector) = (0,0,0)
  36. [HideInInspector] _FogOfWarCenter("FoW Center", Vector) = (0,0,0)
  37. [HideInInspector] _FogOfWar("FoW Texture", 2D) = "white" {}
  38. [HideInInspector] _BlueNoise("_Blue Noise Texture", 2D) = "white" {}
  39. [HideInInspector] _MaxDistanceData("Max Lengh Data", Vector) = (100000, 0.00001, 0)
  40. }
  41. SubShader
  42. {
  43. Tags { "RenderType" = "Transparent" "Queue" = "Transparent+100" "DisableBatching" = "True" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
  44. Blend One OneMinusSrcAlpha
  45. ZTest Always
  46. Cull Front
  47. ZWrite Off
  48. Pass
  49. {
  50. Tags { "LightMode" = "UniversalForward" }
  51. HLSLPROGRAM
  52. #pragma prefer_hlslcc gles
  53. #pragma exclude_renderers d3d11_9x
  54. #pragma target 3.0
  55. #pragma vertex vert
  56. #pragma fragment frag
  57. #if UNITY_VERSION < 202100
  58. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  59. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  60. #else
  61. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE
  62. #endif
  63. #pragma multi_compile _ _ADDITIONAL_LIGHTS
  64. #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
  65. #pragma multi_compile _ VF2_DEPTH_PREPASS
  66. #pragma multi_compile_local_fragment _ VF2_POINT_LIGHTS VF2_NATIVE_LIGHTS
  67. #pragma multi_compile_local_fragment _ VF2_RECEIVE_SHADOWS
  68. #pragma multi_compile_local_fragment VF2_SHAPE_BOX VF2_SHAPE_SPHERE
  69. #pragma multi_compile_local_fragment _ VF2_DETAIL_NOISE VF2_CONSTANT_DENSITY
  70. #pragma shader_feature_local_fragment VF2_DISTANCE
  71. #pragma shader_feature_local_fragment VF2_VOIDS
  72. #pragma shader_feature_local_fragment VF2_FOW
  73. #pragma shader_feature_local_fragment VF2_SURFACE
  74. #pragma shader_feature_local_fragment VF2_DEPTH_GRADIENT
  75. #pragma shader_feature_local_fragment VF2_HEIGHT_GRADIENT
  76. #pragma shader_feature_local_fragment VF2_LIGHT_COOKIE
  77. #if UNITY_VERSION >= 202200
  78. #pragma multi_compile _ _FORWARD_PLUS
  79. #endif
  80. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  81. #undef SAMPLE_TEXTURE2D
  82. #define SAMPLE_TEXTURE2D(textureName, samplerName, coord2) SAMPLE_TEXTURE2D_LOD(textureName, samplerName, coord2, 0)
  83. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  84. #include "CommonsURP.hlsl"
  85. #include "Primitives.cginc"
  86. #include "ShadowsURP.cginc"
  87. #include "Raymarch2D.cginc"
  88. #include "PointLights.cginc"
  89. struct appdata
  90. {
  91. float4 vertex : POSITION;
  92. UNITY_VERTEX_INPUT_INSTANCE_ID
  93. };
  94. struct v2f
  95. {
  96. float4 pos : SV_POSITION;
  97. float3 wpos : TEXCOORD0;
  98. float4 scrPos : TEXCOORD1;
  99. UNITY_VERTEX_INPUT_INSTANCE_ID
  100. UNITY_VERTEX_OUTPUT_STEREO
  101. };
  102. int _ForcedInvisible;
  103. v2f vert(appdata v)
  104. {
  105. v2f o;
  106. UNITY_SETUP_INSTANCE_ID(v);
  107. UNITY_TRANSFER_INSTANCE_ID(v, o);
  108. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  109. o.pos = TransformObjectToHClip(v.vertex.xyz);
  110. o.wpos = TransformObjectToWorld(v.vertex.xyz);
  111. o.scrPos = ComputeScreenPos(o.pos);
  112. #if defined(UNITY_REVERSED_Z)
  113. o.pos.z = o.pos.w * UNITY_NEAR_CLIP_VALUE * 0.99999; // 0.99999 avoids precision issues on some Android devices causing unexpected clipping of light mesh
  114. #else
  115. o.pos.z = o.pos.w - 1.0e-6f;
  116. #endif
  117. if (_ForcedInvisible == 1) {
  118. o.pos.xy = -10000;
  119. }
  120. return o;
  121. }
  122. half4 frag(v2f i) : SV_Target
  123. {
  124. UNITY_SETUP_INSTANCE_ID(i);
  125. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  126. float3 rayStart = GetRayStart(i.wpos);
  127. float3 ray = i.wpos - rayStart;
  128. float t1 = length(ray);
  129. #if defined(FOG_ROTATION)
  130. float3 rayStartNonRotated = rayStart;
  131. float3 rayDirNonRotated = ray / t1;
  132. rayStart = RotateInv(rayStart);
  133. ray = mul((float3x3)_InvRotMatrix, ray);
  134. float3 rayDir = ray / t1;
  135. #else
  136. float3 rayDir = ray / t1;
  137. float3 rayStartNonRotated = rayStart;
  138. float3 rayDirNonRotated = rayDir;
  139. #endif
  140. #if VF2_SHAPE_SPHERE
  141. float t0;
  142. SphereIntersection(rayStart, rayDir, t0, t1);
  143. #else
  144. float t0 = BoxIntersection(rayStart, rayDir);
  145. #endif
  146. CLAMP_RAY_DEPTH(rayStartNonRotated, i.scrPos, t1); // clamp to geometry
  147. #if defined(FOG_MAX_DISTANCE_XZ)
  148. float slope = 1.0001 - abs(rayDir.y);
  149. FOG_MAX_LENGTH /= slope;
  150. #endif
  151. t1 = min(t1, FOG_MAX_LENGTH); // max distance
  152. if (t0>=t1) return 0;
  153. SetJitter(i.scrPos);
  154. half4 fogColor = GetFogColor(rayStart, rayDir, t0, t1);
  155. // dither
  156. fogColor.rgb = max(0, fogColor.rgb - jitter * DITHERING);
  157. // alpha
  158. fogColor *= _LightColor.a;
  159. #if VF2_POINT_LIGHTS
  160. AddPointLights(rayStartNonRotated, rayDirNonRotated, fogColor, t0, t1 - t0);
  161. #endif
  162. #if defined(FOG_MAX_DISTANCE_XZ)
  163. float fallOffFactor = FOG_MAX_LENGTH * FOG_MAX_LENGTH_FALLOFF + 1.0;
  164. half maxDistanceFallOff = (FOG_MAX_LENGTH - t0) / fallOffFactor;
  165. #else
  166. half maxDistanceFallOff = (FOG_MAX_LENGTH - t0) / FOG_MAX_LENGTH_FALLOFF_PRECOMPUTED;
  167. #endif
  168. fogColor *= saturate(maxDistanceFallOff * maxDistanceFallOff * maxDistanceFallOff * maxDistanceFallOff);
  169. return fogColor;
  170. }
  171. ENDHLSL
  172. }
  173. }
  174. }