EnviroHeightFogHDRP.shader 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. Shader "Hidden/EnviroHeightFogHDRP"
  2. {
  3. Properties
  4. {
  5. //_MainTex ("Texture", any) = "white" {}
  6. }
  7. SubShader
  8. {
  9. // No culling or depth
  10. Cull Off ZWrite Off ZTest Always
  11. Pass
  12. {
  13. HLSLPROGRAM
  14. #pragma vertex vert
  15. #pragma fragment frag
  16. #pragma target 4.5
  17. #pragma only_renderers d3d11 ps4 xboxone vulkan metal switch
  18. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  19. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  20. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
  21. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FXAA.hlsl"
  22. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RTUpscale.hlsl"
  23. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/AtmosphericScattering/AtmosphericScattering.hlsl"
  24. #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Sky/SkyUtils.hlsl"
  25. //#include "../Includes/SkyIncludeHLSL.hlsl"
  26. #include "../Includes/FogIncludeHLSL.hlsl"
  27. struct appdata
  28. {
  29. uint vertexID : SV_VertexID;
  30. float2 uv : TEXCOORD0;
  31. UNITY_VERTEX_INPUT_INSTANCE_ID
  32. };
  33. struct v2f
  34. {
  35. float2 uv : TEXCOORD0;
  36. float4 position : SV_POSITION;
  37. float3 ray : TEXCOORD1;
  38. UNITY_VERTEX_OUTPUT_STEREO
  39. };
  40. v2f vert (appdata v)
  41. {
  42. v2f o;
  43. UNITY_SETUP_INSTANCE_ID(v);
  44. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  45. o.position = GetFullScreenTriangleVertexPosition(v.vertexID);
  46. o.uv = GetFullScreenTriangleTexCoord(v.vertexID);
  47. return o;
  48. }
  49. float4x4 _LeftWorldFromView;
  50. float4x4 _RightWorldFromView;
  51. float4x4 _LeftViewFromScreen;
  52. float4x4 _RightViewFromScreen;
  53. float3 color, opacity;
  54. float _EnviroSkyIntensity;
  55. TEXTURE2D_X(_MainTex);
  56. void InverseProjectDepth (float depth, float2 texcoord, out float3 worldPos, out float dist, out float3 viewDir)
  57. {
  58. float4x4 proj, eyeToWorld;
  59. if (unity_StereoEyeIndex == 0)
  60. {
  61. proj = _LeftViewFromScreen;
  62. eyeToWorld = _LeftWorldFromView;
  63. }
  64. else
  65. {
  66. proj = _RightViewFromScreen;
  67. eyeToWorld = _RightWorldFromView;
  68. }
  69. #if !UNITY_UV_STARTS_AT_TOP
  70. texcoord.y = 1 - texcoord.y;
  71. #endif
  72. float2 uvClip = texcoord * 2.0 - 1.0;
  73. float clipDepth = depth; // Fix for OpenGl Core thanks to Lars Bertram
  74. clipDepth = (UNITY_NEAR_CLIP_VALUE < 0) ? clipDepth * 2 - 1 : clipDepth;
  75. float4 clipPos = float4(uvClip, clipDepth, 1.0);
  76. float4 viewPos = mul(proj, clipPos); // inverse projection by clip position
  77. viewPos /= viewPos.w; // perspective division
  78. worldPos = mul(eyeToWorld, viewPos).xyz;
  79. viewDir = worldPos - _WorldSpaceCameraPos.xyz;
  80. dist = length(viewDir);
  81. viewDir /= dist;
  82. }
  83. float4 frag (v2f i) : SV_Target
  84. {
  85. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
  86. float depth = LOAD_TEXTURE2D_X_LOD(_CameraDepthTexture, i.uv * _ScreenSize.xy * (1/_RTHandleScale.xy), 0).r;
  87. float linearDepth = Linear01Depth(depth, _ZBufferParams);
  88. float viewDistance;
  89. float3 worldPos, viewDir;
  90. InverseProjectDepth(depth, i.uv.xy * (1/_RTHandleScale.xy), worldPos, viewDistance, viewDir);
  91. float4 fog = GetExponentialHeightFog(worldPos,linearDepth);
  92. //HDRP Fog
  93. //float3 V = GetSkyViewDirWS(i.uv.xy * _ScreenSize.xy * (1/_RTHandleScale.xy));
  94. //PositionInputs posInput = GetPositionInput(i.position.xy, _ScreenSize.zw, depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V);
  95. //posInput.positionWS = GetCurrentViewPosition() - V * _MaxFogDistance;
  96. //EvaluateAtmosphericScattering(posInput, V, color, opacity);
  97. //fog.rgb = color + (1 - opacity) * fog.rgb;
  98. float4 col = SAMPLE_TEXTURE2D_X_LOD(_MainTex,s_trilinear_clamp_sampler, i.uv, 0);
  99. //float4 volumetrics = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_EnviroVolumetricFogTex, i.uv);
  100. //col.rgb = col.rgb * fog.a + fog.rgb * max(volumetrics.rgb,0.75);
  101. col.rgb = col.rgb * fog.a + fog.rgb;
  102. return col;
  103. }
  104. ENDHLSL
  105. }
  106. }
  107. }