Spine-SkeletonLit-URP-2D.shader 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. Shader "Universal Render Pipeline/2D/Spine/Skeleton Lit" {
  2. Properties {
  3. [NoScaleOffset] _MainTex ("Main Texture", 2D) = "black" {}
  4. [NoScaleOffset] _MaskTex("Mask", 2D) = "white" {}
  5. [Toggle(_STRAIGHT_ALPHA_INPUT)] _StraightAlphaInput("Straight Alpha Texture", Int) = 0
  6. [MaterialToggle(_LIGHT_AFFECTS_ADDITIVE)] _LightAffectsAdditive("Light Affects Additive", Float) = 0
  7. [HideInInspector] _StencilRef("Stencil Reference", Float) = 1.0
  8. [Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Compare", Float) = 0.0 // Disabled stencil test by default
  9. }
  10. HLSLINCLUDE
  11. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  12. ENDHLSL
  13. SubShader {
  14. // UniversalPipeline tag is required. If Universal render pipeline is not set in the graphics settings
  15. // this Subshader will fail.
  16. Tags {"Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" }
  17. Cull Off
  18. ZWrite Off
  19. Stencil {
  20. Ref[_StencilRef]
  21. Comp[_StencilComp]
  22. Pass Keep
  23. }
  24. Pass {
  25. Tags { "LightMode" = "Universal2D" }
  26. ZWrite Off
  27. Cull Off
  28. Blend One OneMinusSrcAlpha
  29. HLSLPROGRAM
  30. // Required to compile gles 2.0 with standard srp library
  31. #pragma prefer_hlslcc gles
  32. #pragma exclude_renderers d3d11_9x
  33. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
  34. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
  35. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
  36. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
  37. #pragma multi_compile _ _LIGHT_AFFECTS_ADDITIVE
  38. struct Attributes {
  39. float3 positionOS : POSITION;
  40. half4 color : COLOR;
  41. float2 uv : TEXCOORD0;
  42. };
  43. struct Varyings {
  44. float4 positionCS : SV_POSITION;
  45. half4 color : COLOR0;
  46. float2 uv : TEXCOORD0;
  47. float2 lightingUV : TEXCOORD1;
  48. };
  49. // Spine related keywords
  50. #pragma shader_feature _ _STRAIGHT_ALPHA_INPUT
  51. #pragma vertex CombinedShapeLightVertex
  52. #pragma fragment CombinedShapeLightFragment
  53. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
  54. #define USE_URP
  55. #include "../Include/SpineCoreShaders/Spine-Common.cginc"
  56. TEXTURE2D(_MainTex);
  57. SAMPLER(sampler_MainTex);
  58. TEXTURE2D(_MaskTex);
  59. SAMPLER(sampler_MaskTex);
  60. #if USE_SHAPE_LIGHT_TYPE_0
  61. SHAPE_LIGHT(0)
  62. #endif
  63. #if USE_SHAPE_LIGHT_TYPE_1
  64. SHAPE_LIGHT(1)
  65. #endif
  66. #if USE_SHAPE_LIGHT_TYPE_2
  67. SHAPE_LIGHT(2)
  68. #endif
  69. #if USE_SHAPE_LIGHT_TYPE_3
  70. SHAPE_LIGHT(3)
  71. #endif
  72. Varyings CombinedShapeLightVertex(Attributes v)
  73. {
  74. Varyings o = (Varyings)0;
  75. o.positionCS = TransformObjectToHClip(v.positionOS);
  76. o.uv = v.uv;
  77. float4 clipVertex = o.positionCS / o.positionCS.w;
  78. o.lightingUV = ComputeScreenPos(clipVertex).xy;
  79. o.color = PMAGammaToTargetSpace(v.color);
  80. return o;
  81. }
  82. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"
  83. half4 CombinedShapeLightFragment(Varyings i) : SV_Target
  84. {
  85. half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
  86. #if defined(_STRAIGHT_ALPHA_INPUT)
  87. tex.rgb *= tex.a;
  88. #endif
  89. half4 main = tex * i.color;
  90. #if !defined(_LIGHT_AFFECTS_ADDITIVE)
  91. if (i.color.a == 0)
  92. return main;
  93. #endif
  94. half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);
  95. #if UNITY_VERSION < 202120
  96. return half4(CombinedShapeLightShared(half4(main.rgb, 1), mask, i.lightingUV).rgb, main.a);
  97. #else
  98. SurfaceData2D surfaceData;
  99. InputData2D inputData;
  100. surfaceData.albedo = main.rgb;
  101. surfaceData.alpha = 1;
  102. surfaceData.mask = mask;
  103. inputData.uv = i.uv;
  104. inputData.lightingUV = i.lightingUV;
  105. return half4(CombinedShapeLightShared(surfaceData, inputData).rgb, main.a);
  106. #endif
  107. }
  108. ENDHLSL
  109. }
  110. Pass
  111. {
  112. Tags { "LightMode" = "NormalsRendering"}
  113. Blend SrcAlpha OneMinusSrcAlpha
  114. ZWrite Off
  115. HLSLPROGRAM
  116. #pragma prefer_hlslcc gles
  117. #pragma vertex NormalsRenderingVertex
  118. #pragma fragment NormalsRenderingFragment
  119. struct Attributes
  120. {
  121. float3 positionOS : POSITION;
  122. float4 color : COLOR;
  123. float2 uv : TEXCOORD0;
  124. };
  125. struct Varyings
  126. {
  127. float4 positionCS : SV_POSITION;
  128. float4 color : COLOR;
  129. float2 uv : TEXCOORD0;
  130. float3 normalWS : TEXCOORD1;
  131. };
  132. TEXTURE2D(_MainTex);
  133. SAMPLER(sampler_MainTex);
  134. Varyings NormalsRenderingVertex(Attributes attributes)
  135. {
  136. Varyings o = (Varyings)0;
  137. o.positionCS = TransformObjectToHClip(attributes.positionOS);
  138. o.uv = attributes.uv;
  139. o.color = attributes.color;
  140. o.normalWS = TransformObjectToWorldDir(float3(0, 0, -1));
  141. return o;
  142. }
  143. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl"
  144. float4 NormalsRenderingFragment(Varyings i) : SV_Target
  145. {
  146. float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
  147. half3 normalTS = half3(0, 0, 1);
  148. half3 tangentWS = half3(0, 0, 0);
  149. half3 bitangentWS = half3(0, 0, 0);
  150. return NormalsRenderingShared(mainTex, normalTS, tangentWS, bitangentWS, i.normalWS);
  151. }
  152. ENDHLSL
  153. }
  154. Pass
  155. {
  156. Name "Unlit"
  157. Tags { "LightMode" = "UniversalForward" "Queue"="Transparent" "RenderType"="Transparent"}
  158. ZWrite Off
  159. Cull Off
  160. Blend One OneMinusSrcAlpha
  161. HLSLPROGRAM
  162. #pragma shader_feature _ _STRAIGHT_ALPHA_INPUT
  163. #pragma prefer_hlslcc gles
  164. #pragma vertex UnlitVertex
  165. #pragma fragment UnlitFragment
  166. #include "Include/Spine-SkeletonLit-UnlitPass-URP-2D.hlsl"
  167. ENDHLSL
  168. }
  169. }
  170. FallBack "Universal Render Pipeline/2D/Sprite-Lit-Default"
  171. }