CommonsURP.hlsl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef VOLUMETRIC_FOG_2_COMMONS_URP
  2. #define VOLUMETRIC_FOG_2_COMMONS_URP
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  4. // ***** Custom options ************
  5. //#define ORTHO_SUPPORT
  6. //#define USE_ALTERNATE_RECONSTRUCT_API
  7. #define MAX_ITERATIONS 500
  8. //#define FOG_BLUE_NOISE
  9. //#define WEBGL_COMPATIBILITY_MODE
  10. //#define FOG_VOID_ROTATION
  11. #define USE_WORLD_SPACE_NOISE
  12. //#define FOG_ROTATION
  13. //#define FOG_BORDER
  14. //#define FOG_MAX_DISTANCE_XZ
  15. //#define FOG_SHADOW_CANCELLATION
  16. //#define V2F_LIGHT_COOKIE_CANCELLATION
  17. //#define FOG_FORWARD_PLUS_IGNORE_CLUSTERING
  18. //#define FOG_FORWARD_PLUS_ADDITIONAL_DIRECTIONAL_LIGHTS
  19. // ***** Common URP code ***********
  20. #if defined(USE_ALTERNATE_RECONSTRUCT_API)
  21. #define CLAMP_RAY_DEPTH(rayStart, scrPos, t1) ClampRayDepthAlt(rayStart, scrPos, t1)
  22. #else
  23. #define CLAMP_RAY_DEPTH(rayStart, scrPos, t1) ClampRayDepth(rayStart, scrPos, t1)
  24. #endif
  25. TEXTURE2D_X_FLOAT(_CustomDepthTexture);
  26. SAMPLER(sampler_CustomDepthTexture);
  27. int VF2_FLIP_DEPTH_TEXTURE;
  28. inline float GetRawDepth(float2 uv) {
  29. float sceneDepth = SampleSceneDepth(VF2_FLIP_DEPTH_TEXTURE ? float2(uv.x, 1.0 - uv.y) : uv);
  30. #if VF2_DEPTH_PREPASS
  31. float customDepth = SAMPLE_TEXTURE2D_X(_CustomDepthTexture, sampler_CustomDepthTexture, uv ).r;
  32. #if UNITY_REVERSED_Z
  33. sceneDepth = max(sceneDepth, customDepth);
  34. #else
  35. sceneDepth = min(sceneDepth, customDepth);
  36. #endif
  37. #endif
  38. return sceneDepth;
  39. }
  40. void ClampRayDepth(float3 rayStart, float4 scrPos, inout float t1) {
  41. float2 uv = scrPos.xy / scrPos.w;
  42. // World position reconstruction
  43. float depth = GetRawDepth(uv);
  44. float4 raw = mul(UNITY_MATRIX_I_VP, float4(uv * 2.0 - 1.0, depth, 1.0));
  45. float3 worldPos = raw.xyz / raw.w;
  46. float z = distance(rayStart, worldPos.xyz);
  47. t1 = min(t1, z);
  48. }
  49. // Alternate reconstruct API; URP 7.4 doesn't set UNITY_MATRIX_I_VP correctly in VR, so we need to use this alternate method
  50. inline float GetLinearEyeDepth(float2 uv) {
  51. float rawDepth = SampleSceneDepth(VF2_FLIP_DEPTH_TEXTURE ? float2(uv.x, 1.0 - uv.y) : uv);
  52. float sceneLinearDepth = LinearEyeDepth(rawDepth, _ZBufferParams);
  53. #if defined(ORTHO_SUPPORT)
  54. #if UNITY_REVERSED_Z
  55. rawDepth = 1.0 - rawDepth;
  56. #endif
  57. float orthoDepth = lerp(_ProjectionParams.y, _ProjectionParams.z, rawDepth);
  58. sceneLinearDepth = lerp(sceneLinearDepth, orthoDepth, unity_OrthoParams.w);
  59. #endif
  60. #if VF2_DEPTH_PREPASS
  61. float customRawDepth = SAMPLE_TEXTURE2D_X(_CustomDepthTexture, sampler_CustomDepthTexture, uv).r;
  62. float customLinearDepth = LinearEyeDepth(customRawDepth, _ZBufferParams);
  63. #if defined(ORTHO_SUPPORT)
  64. #if UNITY_REVERSED_Z
  65. customRawDepth = 1.0 - customRawDepth;
  66. #endif
  67. float customOrthoDepth = lerp(_ProjectionParams.y, _ProjectionParams.z, customRawDepth);
  68. customLinearDepth = lerp(customLinearDepth, customOrthoDepth, unity_OrthoParams.w);
  69. #endif
  70. sceneLinearDepth = min(sceneLinearDepth, customLinearDepth);
  71. #endif
  72. return sceneLinearDepth;
  73. }
  74. void ClampRayDepthAlt(float3 rayStart, float4 scrPos, inout float t1) {
  75. float2 uv = scrPos.xy / scrPos.w;
  76. float vz = GetLinearEyeDepth(uv);
  77. #if defined(ORTHO_SUPPORT)
  78. if (unity_OrthoParams.w) {
  79. t1 = min(t1, vz);
  80. return;
  81. }
  82. #endif
  83. float2 p11_22 = float2(unity_CameraProjection._11, unity_CameraProjection._22);
  84. float2 suv = uv;
  85. #if UNITY_SINGLE_PASS_STEREO
  86. // If Single-Pass Stereo mode is active, transform the
  87. // coordinates to get the correct output UV for the current eye.
  88. float4 scaleOffset = unity_StereoScaleOffset[unity_StereoEyeIndex];
  89. suv = (suv - scaleOffset.zw) / scaleOffset.xy;
  90. #endif
  91. float3 vpos = float3((suv * 2 - 1) / p11_22, -1) * vz;
  92. float4 wpos = mul(unity_CameraToWorld, float4(vpos, 1));
  93. float z = distance(rayStart, wpos.xyz);
  94. t1 = min(t1, z);
  95. }
  96. #endif // VOLUMETRIC_FOG_2_COMMONS_URP