Sprite-Lit-Default.shader 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. Shader "Universal Render Pipeline/2D/Sprite-Lit-Default"
  2. {
  3. Properties
  4. {
  5. _MainTex("Diffuse", 2D) = "white" {}
  6. _MaskTex("Mask", 2D) = "white" {}
  7. _NormalMap("Normal Map", 2D) = "bump" {}
  8. // Legacy properties. They're here so that materials using this shader can gracefully fallback to the legacy sprite shader.
  9. [HideInInspector] _Color("Tint", Color) = (1,1,1,1)
  10. [HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
  11. [HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
  12. [HideInInspector] _AlphaTex("External Alpha", 2D) = "white" {}
  13. [HideInInspector] _EnableExternalAlpha("Enable External Alpha", Float) = 0
  14. }
  15. SubShader
  16. {
  17. Tags {"Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
  18. Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
  19. Cull Off
  20. ZWrite Off
  21. Pass
  22. {
  23. Tags { "LightMode" = "Universal2D" }
  24. HLSLPROGRAM
  25. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  26. #pragma vertex CombinedShapeLightVertex
  27. #pragma fragment CombinedShapeLightFragment
  28. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
  29. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
  30. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
  31. #pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
  32. #pragma multi_compile _ DEBUG_DISPLAY
  33. struct Attributes
  34. {
  35. float3 positionOS : POSITION;
  36. float4 color : COLOR;
  37. float2 uv : TEXCOORD0;
  38. UNITY_VERTEX_INPUT_INSTANCE_ID
  39. };
  40. struct Varyings
  41. {
  42. float4 positionCS : SV_POSITION;
  43. half4 color : COLOR;
  44. float2 uv : TEXCOORD0;
  45. half2 lightingUV : TEXCOORD1;
  46. #if defined(DEBUG_DISPLAY)
  47. float3 positionWS : TEXCOORD2;
  48. #endif
  49. UNITY_VERTEX_OUTPUT_STEREO
  50. };
  51. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
  52. TEXTURE2D(_MainTex);
  53. SAMPLER(sampler_MainTex);
  54. TEXTURE2D(_MaskTex);
  55. SAMPLER(sampler_MaskTex);
  56. half4 _MainTex_ST;
  57. #if USE_SHAPE_LIGHT_TYPE_0
  58. SHAPE_LIGHT(0)
  59. #endif
  60. #if USE_SHAPE_LIGHT_TYPE_1
  61. SHAPE_LIGHT(1)
  62. #endif
  63. #if USE_SHAPE_LIGHT_TYPE_2
  64. SHAPE_LIGHT(2)
  65. #endif
  66. #if USE_SHAPE_LIGHT_TYPE_3
  67. SHAPE_LIGHT(3)
  68. #endif
  69. Varyings CombinedShapeLightVertex(Attributes v)
  70. {
  71. Varyings o = (Varyings)0;
  72. UNITY_SETUP_INSTANCE_ID(v);
  73. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  74. o.positionCS = TransformObjectToHClip(v.positionOS);
  75. #if defined(DEBUG_DISPLAY)
  76. o.positionWS = TransformObjectToWorld(v.positionOS);
  77. #endif
  78. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  79. o.lightingUV = half2(ComputeScreenPos(o.positionCS / o.positionCS.w).xy);
  80. o.color = v.color;
  81. return o;
  82. }
  83. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"
  84. half4 CombinedShapeLightFragment(Varyings i) : SV_Target
  85. {
  86. const half4 main = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
  87. const half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);
  88. SurfaceData2D surfaceData;
  89. InputData2D inputData;
  90. InitializeSurfaceData(main.rgb, main.a, mask, surfaceData);
  91. InitializeInputData(i.uv, i.lightingUV, inputData);
  92. return CombinedShapeLightShared(surfaceData, inputData);
  93. }
  94. ENDHLSL
  95. }
  96. Pass
  97. {
  98. Tags { "LightMode" = "NormalsRendering"}
  99. HLSLPROGRAM
  100. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  101. #pragma vertex NormalsRenderingVertex
  102. #pragma fragment NormalsRenderingFragment
  103. struct Attributes
  104. {
  105. float3 positionOS : POSITION;
  106. float4 color : COLOR;
  107. float2 uv : TEXCOORD0;
  108. float4 tangent : TANGENT;
  109. UNITY_VERTEX_INPUT_INSTANCE_ID
  110. };
  111. struct Varyings
  112. {
  113. float4 positionCS : SV_POSITION;
  114. half4 color : COLOR;
  115. float2 uv : TEXCOORD0;
  116. half3 normalWS : TEXCOORD1;
  117. half3 tangentWS : TEXCOORD2;
  118. half3 bitangentWS : TEXCOORD3;
  119. UNITY_VERTEX_OUTPUT_STEREO
  120. };
  121. TEXTURE2D(_MainTex);
  122. SAMPLER(sampler_MainTex);
  123. TEXTURE2D(_NormalMap);
  124. SAMPLER(sampler_NormalMap);
  125. half4 _NormalMap_ST; // Is this the right way to do this?
  126. Varyings NormalsRenderingVertex(Attributes attributes)
  127. {
  128. Varyings o = (Varyings)0;
  129. UNITY_SETUP_INSTANCE_ID(attributes);
  130. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  131. o.positionCS = TransformObjectToHClip(attributes.positionOS);
  132. o.uv = TRANSFORM_TEX(attributes.uv, _NormalMap);
  133. o.color = attributes.color;
  134. o.normalWS = -GetViewForwardDir();
  135. o.tangentWS = TransformObjectToWorldDir(attributes.tangent.xyz);
  136. o.bitangentWS = cross(o.normalWS, o.tangentWS) * attributes.tangent.w;
  137. return o;
  138. }
  139. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl"
  140. half4 NormalsRenderingFragment(Varyings i) : SV_Target
  141. {
  142. const half4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
  143. const half3 normalTS = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uv));
  144. return NormalsRenderingShared(mainTex, normalTS, i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz);
  145. }
  146. ENDHLSL
  147. }
  148. Pass
  149. {
  150. Tags { "LightMode" = "UniversalForward" "Queue"="Transparent" "RenderType"="Transparent"}
  151. HLSLPROGRAM
  152. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  153. #pragma vertex UnlitVertex
  154. #pragma fragment UnlitFragment
  155. struct Attributes
  156. {
  157. float3 positionOS : POSITION;
  158. float4 color : COLOR;
  159. float2 uv : TEXCOORD0;
  160. UNITY_VERTEX_INPUT_INSTANCE_ID
  161. };
  162. struct Varyings
  163. {
  164. float4 positionCS : SV_POSITION;
  165. float4 color : COLOR;
  166. float2 uv : TEXCOORD0;
  167. #if defined(DEBUG_DISPLAY)
  168. float3 positionWS : TEXCOORD2;
  169. #endif
  170. UNITY_VERTEX_OUTPUT_STEREO
  171. };
  172. TEXTURE2D(_MainTex);
  173. SAMPLER(sampler_MainTex);
  174. float4 _MainTex_ST;
  175. Varyings UnlitVertex(Attributes attributes)
  176. {
  177. Varyings o = (Varyings)0;
  178. UNITY_SETUP_INSTANCE_ID(attributes);
  179. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  180. o.positionCS = TransformObjectToHClip(attributes.positionOS);
  181. #if defined(DEBUG_DISPLAY)
  182. o.positionWS = TransformObjectToWorld(v.positionOS);
  183. #endif
  184. o.uv = TRANSFORM_TEX(attributes.uv, _MainTex);
  185. o.color = attributes.color;
  186. return o;
  187. }
  188. float4 UnlitFragment(Varyings i) : SV_Target
  189. {
  190. float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
  191. #if defined(DEBUG_DISPLAY)
  192. SurfaceData2D surfaceData;
  193. InputData2D inputData;
  194. half4 debugColor = 0;
  195. InitializeSurfaceData(mainTex.rgb, mainTex.a, surfaceData);
  196. InitializeInputData(i.uv, inputData);
  197. SETUP_DEBUG_DATA_2D(inputData, i.positionWS);
  198. if(CanDebugOverrideOutputColor(surfaceData, inputData, debugColor))
  199. {
  200. return debugColor;
  201. }
  202. #endif
  203. return mainTex;
  204. }
  205. ENDHLSL
  206. }
  207. }
  208. Fallback "Sprites/Default"
  209. }