EnviroVolumetricCloudsReprojectHDRP.shader 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. Shader "Hidden/EnviroVolumetricCloudsReprojectHDRP"
  2. {
  3. Properties
  4. {
  5. //_MainTex ("Texture", any) = "white" {}
  6. }
  7. SubShader
  8. {
  9. Tags { "RenderType"="Opaque" }
  10. LOD 100
  11. Pass
  12. {
  13. HLSLPROGRAM
  14. #pragma vertex vert
  15. #pragma fragment frag
  16. #pragma multi_compile _ ENVIRO_DEPTH_BLENDING
  17. #pragma target 4.5
  18. #pragma only_renderers d3d11 ps4 xboxone vulkan metal switch
  19. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  20. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  21. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
  22. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FXAA.hlsl"
  23. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RTUpscale.hlsl"
  24. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/AtmosphericScattering/AtmosphericScattering.hlsl"
  25. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Sky/SkyUtils.hlsl"
  26. TEXTURE2D(_MainTex);
  27. float4 _MainTex_TexelSize;
  28. TEXTURE2D(_UndersampleCloudTex);
  29. float4 _UndersampleCloudTex_TexelSize;
  30. TEXTURE2D(_DownsampledDepth);
  31. float4x4 _PrevVP;
  32. float4x4 _CamToWorld;
  33. float4 _ProjectionExtents;
  34. float4 _ProjectionExtentsRight;
  35. float2 _TexelSize;
  36. float _BlendTime;
  37. struct appdata
  38. {
  39. uint vertexID : SV_VertexID;
  40. float2 uv : TEXCOORD0;
  41. UNITY_VERTEX_INPUT_INSTANCE_ID
  42. };
  43. struct v2f
  44. {
  45. float4 vertex : SV_POSITION;
  46. float2 uv : TEXCOORD0;
  47. float2 ray : TEXCOORD1;
  48. UNITY_VERTEX_OUTPUT_STEREO
  49. };
  50. v2f vert(appdata v)
  51. {
  52. v2f o;
  53. UNITY_SETUP_INSTANCE_ID(v);
  54. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  55. o.vertex = GetFullScreenTriangleVertexPosition(v.vertexID);
  56. o.uv = GetFullScreenTriangleTexCoord(v.vertexID);
  57. if(unity_StereoEyeIndex == 0)
  58. o.ray = (2.0 * o.uv - 1.0) * _ProjectionExtents.xy + _ProjectionExtents.zw;
  59. else
  60. o.ray = (2.0 * o.uv - 1.0) * _ProjectionExtentsRight.xy + _ProjectionExtentsRight.zw;
  61. return o;
  62. }
  63. float2 PrevUV(float4 wspos, out half outOfBound)
  64. {
  65. float4x4 prev = mul(UNITY_MATRIX_P,_PrevVP);
  66. float4 prevUV = mul(prev, wspos);
  67. prevUV.xy = 0.5 * (prevUV.xy / prevUV.w) + 0.5;
  68. half oobmax = max(0.0 - prevUV.x, 0.0 - prevUV.y);
  69. half oobmin = max(prevUV.x - 1.0, prevUV.y - 1.0);
  70. outOfBound = step(0, max(oobmin, oobmax));
  71. return prevUV;
  72. }
  73. float4 ClipAABB(float4 aabbMin, float4 aabbMax, float4 prevSample)
  74. {
  75. float4 p_clip = 0.5 * (aabbMax + aabbMin);
  76. float4 e_clip = 0.5 * (aabbMax - aabbMin);
  77. float4 v_clip = prevSample - p_clip;
  78. float4 v_unit = v_clip / e_clip;
  79. float4 a_unit = abs(v_unit);
  80. float ma_unit = max(max(a_unit.x, max(a_unit.y, a_unit.z)), a_unit.w);
  81. if (ma_unit > 1.0)
  82. return p_clip + v_clip / ma_unit;
  83. else
  84. return prevSample;
  85. }
  86. float4 frag(v2f i) : SV_Target
  87. {
  88. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  89. float3 vspos = float3(i.ray, 1.0);
  90. float4 raymarchResult = LOAD_TEXTURE2D(_UndersampleCloudTex, i.uv * _ScreenSize.xy);
  91. float distance = raymarchResult.y;
  92. float intensity = raymarchResult.x;
  93. half outOfBound;
  94. float4 worldPos = mul(_CamToWorld, float4(normalize(vspos) * distance, 1.0));
  95. worldPos /= worldPos.w;
  96. float2 prevUV = PrevUV(worldPos, outOfBound);
  97. {
  98. float4 prevSample = LOAD_TEXTURE2D(_MainTex, prevUV * _ScreenSize.xy);
  99. float4 m1 = float4(0.0f,0.0f,0.0f,0.0f);
  100. float4 m2 = float4(0.0f,0.0f,0.0f,0.0f);
  101. float sampleCount = 1.0f;
  102. #if ENVIRO_DEPTH_BLENDING
  103. float originalPointDepth = LinearEyeDepth(LOAD_TEXTURE2D(_DownsampledDepth, i.uv * _ScreenSize.xy), _ZBufferParams);
  104. #endif
  105. [unroll]
  106. for (int x = -1; x <= 1; x ++)
  107. {
  108. [unroll]
  109. for (int y = -1; y <= 1; y ++ )
  110. {
  111. float4 val;
  112. if (x == 0 && y == 0)
  113. {
  114. val = raymarchResult;
  115. m1 += val;
  116. m2 += val * val;
  117. }
  118. else
  119. {
  120. float2 uv = i.uv + float2(x * _UndersampleCloudTex_TexelSize.x, y * _UndersampleCloudTex_TexelSize.y);
  121. val = LOAD_TEXTURE2D(_UndersampleCloudTex, uv * _ScreenSize.xy);
  122. #if ENVIRO_DEPTH_BLENDING
  123. float depth = LinearEyeDepth(LOAD_TEXTURE2D(_DownsampledDepth,uv * _ScreenSize.xy), _ZBufferParams);
  124. if (abs(originalPointDepth - depth < 1.5f))
  125. {
  126. m1 += val;
  127. m2 += val * val;
  128. sampleCount += 1.0f;
  129. }
  130. #else
  131. m1 += val;
  132. m2 += val * val;
  133. sampleCount += 1.0f;
  134. #endif
  135. }
  136. }
  137. }
  138. float gamma = _BlendTime;
  139. float4 mu = m1 / sampleCount;
  140. float4 sigma = sqrt(abs(m2 / sampleCount - mu * mu));
  141. float4 minc = mu - gamma * sigma;
  142. float4 maxc = mu + gamma * sigma;
  143. prevSample = ClipAABB(minc, maxc, prevSample);
  144. //Blend
  145. raymarchResult = lerp(prevSample, raymarchResult, max(0.01f, outOfBound));
  146. //raymarchResult = prevSample;
  147. }
  148. return raymarchResult;
  149. }
  150. ENDHLSL
  151. }
  152. }
  153. }