BakedLit.shader 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. Shader "Universal Render Pipeline/Baked Lit"
  2. {
  3. Properties
  4. {
  5. _BaseMap("Texture", 2D) = "white" {}
  6. _BaseColor("Color", Color) = (1, 1, 1, 1)
  7. _Cutoff("AlphaCutout", Range(0.0, 1.0)) = 0.5
  8. _BumpMap("Normal Map", 2D) = "bump" {}
  9. // BlendMode
  10. [HideInInspector] _Surface("__surface", Float) = 0.0
  11. [HideInInspector] _Blend("__blend", Float) = 0.0
  12. [HideInInspector] _AlphaClip("__clip", Float) = 0.0
  13. [HideInInspector] _SrcBlend("Src", Float) = 1.0
  14. [HideInInspector] _DstBlend("Dst", Float) = 0.0
  15. [HideInInspector] _ZWrite("ZWrite", Float) = 1.0
  16. [HideInInspector] _Cull("__cull", Float) = 2.0
  17. // Editmode props
  18. [HideInInspector] _QueueOffset("Queue offset", Float) = 0.0
  19. }
  20. SubShader
  21. {
  22. Tags { "RenderType" = "Opaque" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
  23. LOD 100
  24. Blend [_SrcBlend][_DstBlend]
  25. ZWrite [_ZWrite]
  26. Cull [_Cull]
  27. Pass
  28. {
  29. Name "BakedLit"
  30. Tags{ "LightMode" = "UniversalForward" }
  31. HLSLPROGRAM
  32. // Required to compile gles 2.0 with standard srp library
  33. #pragma prefer_hlslcc gles
  34. #pragma exclude_renderers d3d11_9x
  35. #pragma vertex vert
  36. #pragma fragment frag
  37. #pragma shader_feature _ _NORMALMAP
  38. #pragma shader_feature _ALPHATEST_ON
  39. #pragma shader_feature _ALPHAPREMULTIPLY_ON
  40. // -------------------------------------
  41. // Unity defined keywords
  42. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  43. #pragma multi_compile _ LIGHTMAP_ON
  44. #pragma multi_compile_fog
  45. #pragma multi_compile_instancing
  46. // Lighting include is needed because of GI
  47. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  48. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  49. struct Attributes
  50. {
  51. float4 positionOS : POSITION;
  52. float2 uv : TEXCOORD0;
  53. float2 lightmapUV : TEXCOORD1;
  54. float3 normalOS : NORMAL;
  55. float4 tangentOS : TANGENT;
  56. UNITY_VERTEX_INPUT_INSTANCE_ID
  57. };
  58. struct Varyings
  59. {
  60. float3 uv0AndFogCoord : TEXCOORD0; // xy: uv0, z: fogCoord
  61. DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);
  62. half3 normal : TEXCOORD2;
  63. #if defined(_NORMALMAP)
  64. half3 tangent : TEXCOORD3;
  65. half3 bitangent : TEXCOORD4;
  66. #endif
  67. float4 vertex : SV_POSITION;
  68. UNITY_VERTEX_INPUT_INSTANCE_ID
  69. UNITY_VERTEX_OUTPUT_STEREO
  70. };
  71. Varyings vert(Attributes input)
  72. {
  73. Varyings output = (Varyings)0;
  74. UNITY_SETUP_INSTANCE_ID(input);
  75. UNITY_TRANSFER_INSTANCE_ID(input, output);
  76. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  77. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  78. output.vertex = vertexInput.positionCS;
  79. output.uv0AndFogCoord.xy = TRANSFORM_TEX(input.uv, _BaseMap);
  80. output.uv0AndFogCoord.z = ComputeFogFactor(vertexInput.positionCS.z);
  81. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  82. output.normal = normalInput.normalWS;
  83. #if defined(_NORMALMAP)
  84. output.tangent = normalInput.tangentWS;
  85. output.bitangent = normalInput.bitangentWS;
  86. #endif
  87. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  88. OUTPUT_SH(output.normal, output.vertexSH);
  89. return output;
  90. }
  91. half4 frag(Varyings input) : SV_Target
  92. {
  93. UNITY_SETUP_INSTANCE_ID(input);
  94. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  95. half2 uv = input.uv0AndFogCoord.xy;
  96. half4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
  97. half3 color = texColor.rgb * _BaseColor.rgb;
  98. half alpha = texColor.a * _BaseColor.a;
  99. AlphaDiscard(alpha, _Cutoff);
  100. #ifdef _ALPHAPREMULTIPLY_ON
  101. color *= alpha;
  102. #endif
  103. #if defined(_NORMALMAP)
  104. half3 normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap)).xyz;
  105. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangent, input.bitangent, input.normal));
  106. #else
  107. half3 normalWS = input.normal;
  108. #endif
  109. normalWS = NormalizeNormalPerPixel(normalWS);
  110. color *= SAMPLE_GI(input.lightmapUV, input.vertexSH, normalWS);
  111. color = MixFog(color, input.uv0AndFogCoord.z);
  112. return half4(color, alpha);
  113. }
  114. ENDHLSL
  115. }
  116. Pass
  117. {
  118. Tags{"LightMode" = "DepthOnly"}
  119. ZWrite On
  120. ColorMask 0
  121. HLSLPROGRAM
  122. // Required to compile gles 2.0 with standard srp library
  123. #pragma prefer_hlslcc gles
  124. #pragma exclude_renderers d3d11_9x
  125. #pragma target 2.0
  126. #pragma vertex DepthOnlyVertex
  127. #pragma fragment DepthOnlyFragment
  128. // -------------------------------------
  129. // Material Keywords
  130. #pragma shader_feature _ALPHATEST_ON
  131. //--------------------------------------
  132. // GPU Instancing
  133. #pragma multi_compile_instancing
  134. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  135. #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
  136. ENDHLSL
  137. }
  138. // This pass it not used during regular rendering, only for lightmap baking.
  139. Pass
  140. {
  141. Name "Meta"
  142. Tags{"LightMode" = "Meta"}
  143. Cull Off
  144. HLSLPROGRAM
  145. // Required to compile gles 2.0 with standard srp library
  146. #pragma prefer_hlslcc gles
  147. #pragma exclude_renderers d3d11_9x
  148. #pragma vertex UniversalVertexMeta
  149. #pragma fragment UniversalFragmentMetaBakedLit
  150. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  151. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitMetaPass.hlsl"
  152. ENDHLSL
  153. }
  154. Pass
  155. {
  156. Name "Universal2D"
  157. Tags{ "LightMode" = "Universal2D" }
  158. Blend[_SrcBlend][_DstBlend]
  159. ZWrite[_ZWrite]
  160. Cull[_Cull]
  161. HLSLPROGRAM
  162. // Required to compile gles 2.0 with standard srp library
  163. #pragma prefer_hlslcc gles
  164. #pragma exclude_renderers d3d11_9x
  165. #pragma vertex vert
  166. #pragma fragment frag
  167. #pragma shader_feature _ALPHATEST_ON
  168. #pragma shader_feature _ALPHAPREMULTIPLY_ON
  169. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  170. #include "Packages/com.unity.render-pipelines.universal/Shaders/Utils/Universal2D.hlsl"
  171. ENDHLSL
  172. }
  173. }
  174. FallBack "Universal Render Pipeline/Unlit"
  175. CustomEditor "UnityEditor.Rendering.Universal.ShaderGUI.BakedLitShader"
  176. }