Spine-Sprite-ShadowCasterPass-URP.hlsl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef SPRITES_SHADOW_CASTER_PASS_URP_INCLUDED
  2. #define SPRITES_SHADOW_CASTER_PASS_URP_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  5. float3 _LightDirection;
  6. struct AttributesSpine
  7. {
  8. float4 positionOS : POSITION;
  9. float3 normalOS : NORMAL;
  10. float4 vertexColor : COLOR;
  11. float2 texcoord : TEXCOORD0;
  12. UNITY_VERTEX_INPUT_INSTANCE_ID
  13. };
  14. struct VaryingsSpine
  15. {
  16. float4 positionCS : SV_POSITION;
  17. float4 texcoordAndAlpha: TEXCOORD0;
  18. };
  19. float4 GetShadowPositionHClip(float3 positionOS, half3 normalWS)
  20. {
  21. float3 positionWS = TransformObjectToWorld(positionOS);
  22. float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
  23. #if UNITY_REVERSED_Z
  24. positionCS.z = min(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
  25. #else
  26. positionCS.z = max(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
  27. #endif
  28. return positionCS;
  29. }
  30. VaryingsSpine ShadowPassVertexSprite(AttributesSpine input)
  31. {
  32. VaryingsSpine output;
  33. UNITY_SETUP_INSTANCE_ID(input);
  34. output.texcoordAndAlpha.xyz = float3(TRANSFORM_TEX(input.texcoord, _MainTex).xy, 0);
  35. half3 fixedNormalOS = half3(0, 0, -1);
  36. half3 normalWS = normalize(TransformObjectToWorldNormal(fixedNormalOS));
  37. // flip normal for shadow bias if necessary
  38. // unfortunately we have to compute the sign here in the vertex shader
  39. // instead of using VFACE in fragment shader stage.
  40. half3 viewDirWS = UNITY_MATRIX_V[2].xyz;
  41. half faceSign = sign(dot(viewDirWS, normalWS));
  42. normalWS *= faceSign;
  43. output.positionCS = GetShadowPositionHClip(input.positionOS.xyz, normalWS);
  44. output.texcoordAndAlpha.a = input.vertexColor.a * _Color.a;
  45. return output;
  46. }
  47. #include "SpineCoreShaders/ShaderShared.cginc"
  48. half4 ShadowPassFragmentSprite(VaryingsSpine input) : SV_TARGET
  49. {
  50. fixed4 texureColor = calculateTexturePixel(input.texcoordAndAlpha.xy);
  51. clip(texureColor.a * input.texcoordAndAlpha.a - _ShadowAlphaCutoff);
  52. return 0;
  53. }
  54. #endif