ScreenSpaceShadows.shader 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. Shader "Hidden/Universal Render Pipeline/ScreenSpaceShadows"
  2. {
  3. SubShader
  4. {
  5. Tags{ "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True"}
  6. HLSLINCLUDE
  7. // Note: Screenspace shadow resolve is only performed when shadow cascades are enabled
  8. // Shadow cascades require cascade index and shadowCoord to be computed on pixel.
  9. #define _MAIN_LIGHT_SHADOWS_CASCADE
  10. #pragma prefer_hlslcc gles
  11. #pragma exclude_renderers d3d11_9x
  12. //Keep compiler quiet about Shadows.hlsl.
  13. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  14. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl"
  15. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ImageBasedLighting.hlsl"
  16. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  17. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  18. #if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
  19. TEXTURE2D_ARRAY_FLOAT(_CameraDepthTexture);
  20. #else
  21. TEXTURE2D_FLOAT(_CameraDepthTexture);
  22. #endif
  23. SAMPLER(sampler_CameraDepthTexture);
  24. struct Attributes
  25. {
  26. float4 positionOS : POSITION;
  27. float2 texcoord : TEXCOORD0;
  28. UNITY_VERTEX_INPUT_INSTANCE_ID
  29. };
  30. struct Varyings
  31. {
  32. half4 positionCS : SV_POSITION;
  33. half4 uv : TEXCOORD0;
  34. UNITY_VERTEX_OUTPUT_STEREO
  35. };
  36. Varyings Vertex(Attributes input)
  37. {
  38. Varyings output;
  39. UNITY_SETUP_INSTANCE_ID(input);
  40. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  41. output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
  42. float4 projPos = output.positionCS * 0.5;
  43. projPos.xy = projPos.xy + projPos.w;
  44. output.uv.xy = UnityStereoTransformScreenSpaceTex(input.texcoord);
  45. output.uv.zw = projPos.xy;
  46. return output;
  47. }
  48. half4 Fragment(Varyings input) : SV_Target
  49. {
  50. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  51. float deviceDepth = SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture, input.uv.xy).r;
  52. #if UNITY_REVERSED_Z
  53. deviceDepth = 1 - deviceDepth;
  54. #endif
  55. deviceDepth = 2 * deviceDepth - 1; //NOTE: Currently must massage depth before computing CS position.
  56. float3 vpos = ComputeViewSpacePosition(input.uv.zw, deviceDepth, unity_CameraInvProjection);
  57. float3 wpos = mul(unity_CameraToWorld, float4(vpos, 1)).xyz;
  58. //Fetch shadow coordinates for cascade.
  59. float4 coords = TransformWorldToShadowCoord(wpos);
  60. // Screenspace shadowmap is only used for directional lights which use orthogonal projection.
  61. ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
  62. half4 shadowParams = GetMainLightShadowParams();
  63. return SampleShadowmap(TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), coords, shadowSamplingData, shadowParams, false);
  64. }
  65. ENDHLSL
  66. Pass
  67. {
  68. Name "ScreenSpaceShadows"
  69. ZTest Always
  70. ZWrite Off
  71. Cull Off
  72. HLSLPROGRAM
  73. #pragma multi_compile _MAIN_LIGHT_SHADOWS
  74. #pragma multi_compile _ _SHADOWS_SOFT
  75. #pragma vertex Vertex
  76. #pragma fragment Fragment
  77. ENDHLSL
  78. }
  79. }
  80. }