Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShadowCasterPass.hlsl 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef UNIVERSAL_SHADOW_CASTER_PASS_INCLUDED
  2. #define UNIVERSAL_SHADOW_CASTER_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  5. #if defined(LOD_FADE_CROSSFADE)
  6. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl"
  7. #endif
  8. // Shadow Casting Light geometric parameters. These variables are used when applying the shadow Normal Bias and are set by UnityEngine.Rendering.Universal.ShadowUtils.SetupShadowCasterConstantBuffer in com.unity.render-pipelines.universal/Runtime/ShadowUtils.cs
  9. // For Directional lights, _LightDirection is used when applying shadow Normal Bias.
  10. // For Spot lights and Point lights, _LightPosition is used to compute the actual light direction because it is different at each shadow caster geometry vertex.
  11. float3 _LightDirection;
  12. float3 _LightPosition;
  13. struct Attributes
  14. {
  15. float4 positionOS : POSITION;
  16. float3 normalOS : NORMAL;
  17. float2 texcoord : TEXCOORD0;
  18. UNITY_VERTEX_INPUT_INSTANCE_ID
  19. };
  20. struct Varyings
  21. {
  22. #if defined(_ALPHATEST_ON)
  23. float2 uv : TEXCOORD0;
  24. #endif
  25. float4 positionCS : SV_POSITION;
  26. UNITY_VERTEX_INPUT_INSTANCE_ID
  27. };
  28. #define EPSILON 0.001
  29. float4 GetShadowPositionHClip(Attributes input)
  30. {
  31. float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  32. float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
  33. #if _CASTING_PUNCTUAL_LIGHT_SHADOW
  34. float3 lightDirectionWS = normalize(_LightPosition - positionWS);
  35. #else
  36. float3 lightDirectionWS = _LightDirection;
  37. #endif
  38. float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS));
  39. #if UNITY_REVERSED_Z
  40. float clamped = min(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
  41. #else
  42. float clamped = max(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
  43. #endif
  44. positionCS.z = lerp(positionCS.z, clamped, saturate(_ShadowBias.y + EPSILON));
  45. return positionCS;
  46. }
  47. Varyings ShadowPassVertex(Attributes input)
  48. {
  49. Varyings output;
  50. UNITY_SETUP_INSTANCE_ID(input);
  51. UNITY_TRANSFER_INSTANCE_ID(input, output);
  52. #if defined(_ALPHATEST_ON)
  53. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  54. #endif
  55. output.positionCS = GetShadowPositionHClip(input);
  56. return output;
  57. }
  58. half4 ShadowPassFragment(Varyings input) : SV_TARGET
  59. {
  60. UNITY_SETUP_INSTANCE_ID(input);
  61. #if defined(_ALPHATEST_ON)
  62. Alpha(SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a, _BaseColor, _Cutoff);
  63. #endif
  64. #if defined(LOD_FADE_CROSSFADE)
  65. LODFadeCrossFade(input.positionCS);
  66. #endif
  67. return 0;
  68. }
  69. #endif