暫無描述
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.

Sprite-Lit-Default.shader 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. _ZWrite("ZWrite", Float) = 0
  9. // Legacy properties. They're here so that materials using this shader can gracefully fallback to the legacy sprite shader.
  10. [HideInInspector] _Color("Tint", Color) = (1,1,1,1)
  11. [HideInInspector] _RendererColor("RendererColor", Color) = (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 [_ZWrite]
  21. ZTest Off
  22. Pass
  23. {
  24. Tags { "LightMode" = "Universal2D" }
  25. HLSLPROGRAM
  26. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  27. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/Core2D.hlsl"
  28. #pragma vertex CombinedShapeLightVertex
  29. #pragma fragment CombinedShapeLightFragment
  30. #include_with_pragmas "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/ShapeLightShared.hlsl"
  31. #pragma multi_compile _ DEBUG_DISPLAY SKINNED_SPRITE
  32. struct Attributes
  33. {
  34. float3 positionOS : POSITION;
  35. float4 color : COLOR;
  36. float2 uv : TEXCOORD0;
  37. UNITY_SKINNED_VERTEX_INPUTS
  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. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/DebugMipmapStreamingMacros.hlsl"
  53. TEXTURE2D(_MainTex);
  54. SAMPLER(sampler_MainTex);
  55. float4 _MainTex_TexelSize;
  56. UNITY_TEXTURE_STREAMING_DEBUG_VARS_FOR_TEX(_MainTex);
  57. TEXTURE2D(_MaskTex);
  58. SAMPLER(sampler_MaskTex);
  59. // NOTE: Do not ifdef the properties here as SRP batcher can not handle different layouts.
  60. CBUFFER_START(UnityPerMaterial)
  61. half4 _MainTex_ST;
  62. half4 _NormalMap_ST; // Is this the right way to do this?
  63. half4 _Color;
  64. CBUFFER_END
  65. #if USE_SHAPE_LIGHT_TYPE_0
  66. SHAPE_LIGHT(0)
  67. #endif
  68. #if USE_SHAPE_LIGHT_TYPE_1
  69. SHAPE_LIGHT(1)
  70. #endif
  71. #if USE_SHAPE_LIGHT_TYPE_2
  72. SHAPE_LIGHT(2)
  73. #endif
  74. #if USE_SHAPE_LIGHT_TYPE_3
  75. SHAPE_LIGHT(3)
  76. #endif
  77. Varyings CombinedShapeLightVertex(Attributes v)
  78. {
  79. Varyings o = (Varyings)0;
  80. UNITY_SETUP_INSTANCE_ID(v);
  81. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  82. UNITY_SKINNED_VERTEX_COMPUTE(v);
  83. v.positionOS = UnityFlipSprite(v.positionOS, unity_SpriteProps.xy);
  84. o.positionCS = TransformObjectToHClip(v.positionOS);
  85. #if defined(DEBUG_DISPLAY)
  86. o.positionWS = TransformObjectToWorld(v.positionOS);
  87. #endif
  88. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  89. o.lightingUV = half2(ComputeScreenPos(o.positionCS / o.positionCS.w).xy);
  90. o.color = v.color * _Color * unity_SpriteColor;
  91. return o;
  92. }
  93. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"
  94. half4 CombinedShapeLightFragment(Varyings i) : SV_Target
  95. {
  96. const half4 main = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
  97. const half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);
  98. SurfaceData2D surfaceData;
  99. InputData2D inputData;
  100. InitializeSurfaceData(main.rgb, main.a, mask, surfaceData);
  101. InitializeInputData(i.uv, i.lightingUV, inputData);
  102. SETUP_DEBUG_TEXTURE_DATA_2D(inputData, i.positionWS, i.positionCS, _MainTex);
  103. return CombinedShapeLightShared(surfaceData, inputData);
  104. }
  105. ENDHLSL
  106. }
  107. Pass
  108. {
  109. ZWrite Off
  110. Tags { "LightMode" = "NormalsRendering"}
  111. HLSLPROGRAM
  112. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  113. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/Core2D.hlsl"
  114. #pragma vertex NormalsRenderingVertex
  115. #pragma fragment NormalsRenderingFragment
  116. #pragma multi_compile _ SKINNED_SPRITE
  117. struct Attributes
  118. {
  119. float3 positionOS : POSITION;
  120. float4 color : COLOR;
  121. float2 uv : TEXCOORD0;
  122. float4 tangent : TANGENT;
  123. UNITY_SKINNED_VERTEX_INPUTS
  124. UNITY_VERTEX_INPUT_INSTANCE_ID
  125. };
  126. struct Varyings
  127. {
  128. float4 positionCS : SV_POSITION;
  129. half4 color : COLOR;
  130. float2 uv : TEXCOORD0;
  131. half3 normalWS : TEXCOORD1;
  132. half3 tangentWS : TEXCOORD2;
  133. half3 bitangentWS : TEXCOORD3;
  134. UNITY_VERTEX_OUTPUT_STEREO
  135. };
  136. TEXTURE2D(_MainTex);
  137. SAMPLER(sampler_MainTex);
  138. TEXTURE2D(_NormalMap);
  139. SAMPLER(sampler_NormalMap);
  140. // NOTE: Do not ifdef the properties here as SRP batcher can not handle different layouts.
  141. CBUFFER_START( UnityPerMaterial )
  142. half4 _MainTex_ST;
  143. half4 _NormalMap_ST; // Is this the right way to do this?
  144. half4 _Color;
  145. CBUFFER_END
  146. Varyings NormalsRenderingVertex(Attributes attributes)
  147. {
  148. Varyings o = (Varyings)0;
  149. UNITY_SETUP_INSTANCE_ID(attributes);
  150. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  151. UNITY_SKINNED_VERTEX_COMPUTE(attributes);
  152. attributes.positionOS = UnityFlipSprite(attributes.positionOS, unity_SpriteProps.xy);
  153. o.positionCS = TransformObjectToHClip(attributes.positionOS);
  154. o.uv = TRANSFORM_TEX(attributes.uv, _NormalMap);
  155. o.color = attributes.color;
  156. o.normalWS = -GetViewForwardDir();
  157. o.tangentWS = TransformObjectToWorldDir(attributes.tangent.xyz);
  158. o.bitangentWS = cross(o.normalWS, o.tangentWS) * attributes.tangent.w;
  159. return o;
  160. }
  161. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl"
  162. half4 NormalsRenderingFragment(Varyings i) : SV_Target
  163. {
  164. const half4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
  165. const half3 normalTS = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uv));
  166. return NormalsRenderingShared(mainTex, normalTS, i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz);
  167. }
  168. ENDHLSL
  169. }
  170. Pass
  171. {
  172. Tags { "LightMode" = "UniversalForward" "Queue"="Transparent" "RenderType"="Transparent"}
  173. HLSLPROGRAM
  174. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  175. #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/Core2D.hlsl"
  176. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/DebugMipmapStreamingMacros.hlsl"
  177. #if defined(DEBUG_DISPLAY)
  178. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Debug/Debugging2D.hlsl"
  179. #endif
  180. #pragma vertex UnlitVertex
  181. #pragma fragment UnlitFragment
  182. #pragma multi_compile _ DEBUG_DISPLAY SKINNED_SPRITE
  183. struct Attributes
  184. {
  185. float3 positionOS : POSITION;
  186. float4 color : COLOR;
  187. float2 uv : TEXCOORD0;
  188. UNITY_SKINNED_VERTEX_INPUTS
  189. UNITY_VERTEX_INPUT_INSTANCE_ID
  190. };
  191. struct Varyings
  192. {
  193. float4 positionCS : SV_POSITION;
  194. float4 color : COLOR;
  195. float2 uv : TEXCOORD0;
  196. #if defined(DEBUG_DISPLAY)
  197. float3 positionWS : TEXCOORD2;
  198. #endif
  199. UNITY_VERTEX_OUTPUT_STEREO
  200. };
  201. TEXTURE2D(_MainTex);
  202. SAMPLER(sampler_MainTex);
  203. float4 _MainTex_TexelSize;
  204. UNITY_TEXTURE_STREAMING_DEBUG_VARS_FOR_TEX(_MainTex);
  205. // NOTE: Do not ifdef the properties here as SRP batcher can not handle different layouts.
  206. CBUFFER_START( UnityPerMaterial )
  207. half4 _MainTex_ST;
  208. half4 _NormalMap_ST; // Is this the right way to do this?
  209. half4 _Color;
  210. CBUFFER_END
  211. Varyings UnlitVertex(Attributes attributes)
  212. {
  213. Varyings o = (Varyings)0;
  214. UNITY_SETUP_INSTANCE_ID(attributes);
  215. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  216. UNITY_SKINNED_VERTEX_COMPUTE(attributes);
  217. attributes.positionOS = UnityFlipSprite( attributes.positionOS, unity_SpriteProps.xy);
  218. o.positionCS = TransformObjectToHClip(attributes.positionOS);
  219. #if defined(DEBUG_DISPLAY)
  220. o.positionWS = TransformObjectToWorld(attributes.positionOS);
  221. #endif
  222. o.uv = TRANSFORM_TEX(attributes.uv, _MainTex);
  223. o.color = attributes.color * _Color * unity_SpriteColor;
  224. return o;
  225. }
  226. float4 UnlitFragment(Varyings i) : SV_Target
  227. {
  228. float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
  229. #if defined(DEBUG_DISPLAY)
  230. SurfaceData2D surfaceData;
  231. InputData2D inputData;
  232. half4 debugColor = 0;
  233. InitializeSurfaceData(mainTex.rgb, mainTex.a, surfaceData);
  234. InitializeInputData(i.uv, inputData);
  235. SETUP_DEBUG_TEXTURE_DATA_2D(inputData, i.positionWS, i.positionCS, _MainTex);
  236. if(CanDebugOverrideOutputColor(surfaceData, inputData, debugColor))
  237. {
  238. return debugColor;
  239. }
  240. #endif
  241. return mainTex;
  242. }
  243. ENDHLSL
  244. }
  245. }
  246. Fallback "Sprites/Default"
  247. }