EnviroVolumetricCloudsBlendHDRP.shader 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. Shader "Hidden/EnviroVolumetricCloudsBlendHDRP"
  2. {
  3. Properties
  4. {
  5. //_MainTex ("Texture", any) = "white" {}
  6. }
  7. SubShader
  8. {
  9. Tags { "RenderType"="Opaque" }
  10. LOD 100
  11. Pass
  12. {
  13. Cull Off ZWrite Off ZTest Always
  14. HLSLPROGRAM
  15. #pragma target 4.5
  16. #pragma only_renderers d3d11 ps4 xboxone vulkan metal switch
  17. #pragma vertex vert
  18. #pragma fragment frag
  19. #pragma multi_compile __ ENVIRO_DEPTH_BLENDING
  20. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  21. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  22. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
  23. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FXAA.hlsl"
  24. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RTUpscale.hlsl"
  25. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/AtmosphericScattering/AtmosphericScattering.hlsl"
  26. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Sky/SkyUtils.hlsl"
  27. //#include "../Includes/SkyIncludeHLSL.hlsl"
  28. #include "../Includes/FogIncludeHLSL.hlsl"
  29. TEXTURE2D_X(_MainTex);
  30. TEXTURE2D(_DownsampledDepth);
  31. #ifdef STEREO_INSTANCING_ON
  32. TEXTURE2D_X (_CloudTex);
  33. SAMPLER(sampler_CloudTex);
  34. #else
  35. TEXTURE2D (_CloudTex);
  36. SAMPLER(sampler_CloudTex);
  37. #endif
  38. SamplerState Point_Clamp_Sampler;
  39. float4 _CloudTex_TexelSize;
  40. float4 _MainTex_TexelSize;
  41. float4 _HandleScales;
  42. float4 _DepthHandleScale;
  43. float4 _ProjectionExtents;
  44. float4 _ProjectionExtentsRight;
  45. float3 _AmbientColor;
  46. float3 _DirectLightColor;
  47. float _AtmosphereColorSaturateDistance;
  48. float4x4 _CamToWorld;
  49. float3 color;
  50. float3 opacity;
  51. float _EnviroSkyIntensity;
  52. struct appdata
  53. {
  54. uint vertexID : SV_VertexID;
  55. float2 uv : TEXCOORD0;
  56. UNITY_VERTEX_INPUT_INSTANCE_ID
  57. };
  58. struct v2f
  59. {
  60. float4 vertex : SV_POSITION;
  61. float2 uv : TEXCOORD0;
  62. float2 vsray : TEXCOORD1;
  63. #ifdef ENVIRO_DEPTH_BLENDING
  64. float2 uv00 : TEXCOORD2;
  65. float2 uv10 : TEXCOORD3;
  66. float2 uv01 : TEXCOORD4;
  67. float2 uv11 : TEXCOORD5;
  68. #endif
  69. UNITY_VERTEX_OUTPUT_STEREO
  70. };
  71. v2f vert(appdata v)
  72. {
  73. v2f o;
  74. UNITY_SETUP_INSTANCE_ID(v);
  75. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  76. o.vertex = GetFullScreenTriangleVertexPosition(v.vertexID);
  77. o.uv = GetFullScreenTriangleTexCoord(v.vertexID);
  78. if(unity_StereoEyeIndex == 0)
  79. o.vsray = (2.0 * o.uv * (1/_RTHandleScale.xy) - 1.0) * _ProjectionExtents.xy + _ProjectionExtents.zw;
  80. else
  81. o.vsray = (2.0 * o.uv * (1/_RTHandleScale.xy) - 1.0) * _ProjectionExtentsRight.xy + _ProjectionExtentsRight.zw;
  82. #ifdef ENVIRO_DEPTH_BLENDING
  83. o.uv00 = o.uv - 0.5 * _CloudTex_TexelSize.xy;
  84. o.uv10 = o.uv00 + float2(_CloudTex_TexelSize.x, 0.0);
  85. o.uv01 = o.uv00 + float2(0.0, _CloudTex_TexelSize.y);
  86. o.uv11 = o.uv00 + _CloudTex_TexelSize.xy;
  87. #endif
  88. return o;
  89. }
  90. #ifdef ENVIRO_DEPTH_BLENDING
  91. float4 Upsample(v2f i)
  92. {
  93. float4 lowResDepth = 0.0f;
  94. float highResDepth = LinearEyeDepth(LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, i.uv * _ScreenSize.xy * (1/_RTHandleScale.xy), 0), _ZBufferParams);
  95. lowResDepth.x = LinearEyeDepth(LOAD_TEXTURE2D(_DownsampledDepth,i.uv00 * _ScreenSize.xy * _DepthHandleScale.xy).r, _ZBufferParams);
  96. lowResDepth.y = LinearEyeDepth(LOAD_TEXTURE2D(_DownsampledDepth,i.uv10 * _ScreenSize.xy * _DepthHandleScale.xy).r, _ZBufferParams);
  97. lowResDepth.z = LinearEyeDepth(LOAD_TEXTURE2D(_DownsampledDepth,i.uv01 * _ScreenSize.xy * _DepthHandleScale.xy).r, _ZBufferParams);
  98. lowResDepth.w = LinearEyeDepth(LOAD_TEXTURE2D(_DownsampledDepth,i.uv11 * _ScreenSize.xy * _DepthHandleScale.xy).r, _ZBufferParams);
  99. float4 depthDiff = abs(lowResDepth - highResDepth);
  100. float accumDiff = dot(depthDiff, float4(1, 1, 1, 1));
  101. [branch]
  102. if (accumDiff < 1.5f)
  103. {
  104. #ifdef STEREO_INSTANCING_ON
  105. float3 uv = float3(i.uv,unity_StereoEyeIndex);
  106. return _CloudTex.Sample(sampler_CloudTex, uv * _HandleScales.xy);
  107. #else
  108. return _CloudTex.Sample(sampler_CloudTex, i.uv * _HandleScales.xy);
  109. #endif
  110. }
  111. else
  112. {
  113. float minDepthDiff = depthDiff[0];
  114. float2 nearestUv = i.uv00;
  115. if (depthDiff[1] < minDepthDiff)
  116. {
  117. nearestUv = i.uv10;
  118. minDepthDiff = depthDiff[1];
  119. }
  120. if (depthDiff[2] < minDepthDiff)
  121. {
  122. nearestUv = i.uv01;
  123. minDepthDiff = depthDiff[2];
  124. }
  125. if (depthDiff[3] < minDepthDiff)
  126. {
  127. nearestUv = i.uv11;
  128. minDepthDiff = depthDiff[3];
  129. }
  130. #ifdef STEREO_INSTANCING_ON
  131. float3 uv = float3(nearestUv,unity_StereoEyeIndex);
  132. return _CloudTex.Sample(Point_Clamp_Sampler, uv * _HandleScales.xy);
  133. #else
  134. return _CloudTex.Sample(Point_Clamp_Sampler, nearestUv * _HandleScales.xy);
  135. #endif
  136. }
  137. }
  138. #endif
  139. half4 frag(v2f i) : SV_Target
  140. {
  141. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  142. float4 vspos = float4(i.vsray, 1.0, 1.0);
  143. float4 worldPos = mul(_CamToWorld,vspos);
  144. float3 viewDir = normalize(worldPos.xyz - _WorldSpaceCameraPos);
  145. float4 sourceColor = SAMPLE_TEXTURE2D_X_LOD(_MainTex,s_trilinear_clamp_sampler, i.uv, 0);
  146. #ifdef ENVIRO_DEPTH_BLENDING
  147. float4 cloudsColor = Upsample(i);
  148. #else
  149. #ifdef STEREO_INSTANCING_ON
  150. float3 uv = float3(i.uv * _HandleScales.xy ,unity_StereoEyeIndex);
  151. float4 cloudsColor = _CloudTex.Sample(sampler_CloudTex, uv);
  152. #else
  153. float4 cloudsColor = _CloudTex.Sample(sampler_CloudTex, i.uv * _HandleScales.xy);
  154. #endif
  155. #endif
  156. float3 sunColor = pow(_DirectLightColor.rgb,2) * 2.0f;
  157. float3 skyColor = GetSkyColor(viewDir,0.005f) * _EnviroSkyIntensity * GetCurrentExposureMultiplier();
  158. float4 finalColor = float4(cloudsColor.r * sunColor + _AmbientColor, cloudsColor.a);
  159. float atmosphericBlendFactor = saturate(exp(-cloudsColor.g / _AtmosphereColorSaturateDistance));
  160. //if(_WorldSpaceCameraPos.y <= 2000)
  161. finalColor.rgb = lerp(skyColor, finalColor.rgb, atmosphericBlendFactor);
  162. float rawDepth = LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, i.uv * _ScreenSize.xy * (1/_RTHandleScale.xy), 0);
  163. float sceneDepth = Linear01Depth(rawDepth, _ZBufferParams);
  164. #if ENVIRO_DEPTH_BLENDING
  165. float4 final = float4(sourceColor.rgb * (1 - finalColor.a) + finalColor.rgb * finalColor.a, 1);
  166. #else
  167. float4 final = sourceColor;
  168. if (sceneDepth == 1.0f)
  169. final = half4(sourceColor.rgb * saturate(1 - finalColor.a) + finalColor.rgb * finalColor.a, 1);
  170. #endif
  171. // HDRP Fog
  172. if (sceneDepth == 1.0f)
  173. {
  174. PositionInputs posInput = GetPositionInput(i.vertex.xy, _ScreenSize.zw, rawDepth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V);
  175. float3 V = GetSkyViewDirWS(i.uv * _ScreenSize.xy * (1/_RTHandleScale.xy));
  176. posInput.positionWS = GetCurrentViewPosition() - V * _MaxFogDistance;
  177. EvaluateAtmosphericScattering(posInput, V, color, opacity);
  178. final.rgb = color + (1 - opacity) * final.rgb;
  179. }
  180. return final;
  181. }
  182. ENDHLSL
  183. }
  184. }
  185. }