ShadowGroup2D.shader 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. Shader "Hidden/ShadowGroup2D"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. _ShadowStencilGroup("__ShadowStencilGroup", Float) = 1.0
  7. }
  8. SubShader
  9. {
  10. Tags { "RenderType"="Opaque" }
  11. Cull Off
  12. BlendOp Add
  13. Blend One One
  14. ZWrite Off
  15. Pass
  16. {
  17. Stencil
  18. {
  19. Ref [_ShadowStencilGroup]
  20. Comp NotEqual
  21. Pass Replace
  22. Fail Keep
  23. }
  24. HLSLPROGRAM
  25. #pragma vertex vert
  26. #pragma fragment frag
  27. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  28. struct Attributes
  29. {
  30. float3 vertex : POSITION;
  31. float4 tangent: TANGENT;
  32. float2 uv : TEXCOORD0;
  33. float4 extrusion : COLOR;
  34. };
  35. struct Varyings
  36. {
  37. float4 vertex : SV_POSITION;
  38. float4 color : COLOR;
  39. float2 uv : TEXCOORD0;
  40. };
  41. sampler2D _MainTex;
  42. float4 _MainTex_ST;
  43. uniform float3 _LightPos;
  44. uniform float _ShadowRadius;
  45. Varyings vert (Attributes v)
  46. {
  47. Varyings o;
  48. float3 vertexWS = TransformObjectToWorld(v.vertex); // This should be in world space
  49. float3 lightDir = _LightPos - vertexWS;
  50. lightDir.z = 0;
  51. // Start of code to see if this point should be extruded
  52. float3 lightDirection = normalize(lightDir);
  53. float3 endpoint = vertexWS + (_ShadowRadius * -lightDirection);
  54. float3 worldTangent = TransformObjectToWorldDir(v.tangent.xyz);
  55. float sharedShadowTest = saturate(ceil(dot(lightDirection, worldTangent)));
  56. // Start of code to calculate offset
  57. float3 vertexWS0 = TransformObjectToWorld(float3(v.extrusion.xy, 0));
  58. float3 vertexWS1 = TransformObjectToWorld(float3(v.extrusion.zw, 0));
  59. float3 shadowDir0 = vertexWS0 - _LightPos;
  60. shadowDir0.z = 0;
  61. shadowDir0 = normalize(shadowDir0);
  62. float3 shadowDir1 = vertexWS1 -_LightPos;
  63. shadowDir1.z = 0;
  64. shadowDir1 = normalize(shadowDir1);
  65. float3 shadowDir = normalize(shadowDir0 + shadowDir1);
  66. float3 sharedShadowOffset = sharedShadowTest * _ShadowRadius * shadowDir;
  67. float3 position;
  68. position = vertexWS + sharedShadowOffset;
  69. o.vertex = TransformWorldToHClip(position);
  70. // RGB - R is shadow value (to support soft shadows), G is Self Shadow Mask, B is No Shadow Mask
  71. o.color = 1; // v.color;
  72. o.color.g = 0.5;
  73. o.color.b = 0;
  74. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  75. return o;
  76. }
  77. float4 frag (Varyings i) : SV_Target
  78. {
  79. float4 main = tex2D(_MainTex, i.uv);
  80. float4 col = i.color;
  81. col.g = main.a * col.g;
  82. return col;
  83. }
  84. ENDHLSL
  85. }
  86. Pass
  87. {
  88. Stencil
  89. {
  90. Ref [_ShadowStencilGroup]
  91. Comp NotEqual
  92. Pass Replace
  93. Fail Keep
  94. }
  95. HLSLPROGRAM
  96. #pragma vertex vert
  97. #pragma fragment frag
  98. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  99. struct Attributes
  100. {
  101. float3 vertex : POSITION;
  102. float2 uv : TEXCOORD0;
  103. };
  104. struct Varyings
  105. {
  106. float4 vertex : SV_POSITION;
  107. float4 color : COLOR;
  108. float2 uv : TEXCOORD0;
  109. };
  110. sampler2D _MainTex;
  111. float4 _MainTex_ST;
  112. Varyings vert (Attributes v)
  113. {
  114. Varyings o;
  115. o.vertex = TransformObjectToHClip(v.vertex);
  116. // RGB - R is shadow value (to support soft shadows), G is Self Shadow Mask, B is No Shadow Mask
  117. o.color = 1;
  118. o.color.g = 0.5;
  119. o.color.b = 1;
  120. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  121. return o;
  122. }
  123. half4 frag (Varyings i) : SV_Target
  124. {
  125. half4 main = tex2D(_MainTex, i.uv);
  126. half4 color = i.color;
  127. color.b = 1 - main.a;
  128. return color;
  129. }
  130. ENDHLSL
  131. }
  132. }
  133. }