BakedLit.shader 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. Shader "Universal Render Pipeline/Baked Lit"
  2. {
  3. Properties
  4. {
  5. [MainTexture] _BaseMap("Texture", 2D) = "white" {}
  6. [MainColor] _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. [HideInInspector][NoScaleOffset]unity_Lightmaps("unity_Lightmaps", 2DArray) = "" {}
  20. [HideInInspector][NoScaleOffset]unity_LightmapsInd("unity_LightmapsInd", 2DArray) = "" {}
  21. [HideInInspector][NoScaleOffset]unity_ShadowMasks("unity_ShadowMasks", 2DArray) = "" {}
  22. }
  23. SubShader
  24. {
  25. Tags { "RenderType" = "Opaque" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" "ShaderModel"="4.5"}
  26. LOD 100
  27. Blend [_SrcBlend][_DstBlend]
  28. ZWrite [_ZWrite]
  29. Cull [_Cull]
  30. Pass
  31. {
  32. Name "BakedLit"
  33. Tags{ "LightMode" = "UniversalForwardOnly" }
  34. HLSLPROGRAM
  35. #pragma exclude_renderers gles gles3 glcore
  36. #pragma target 4.5
  37. #pragma vertex vert
  38. #pragma fragment frag
  39. // -------------------------------------
  40. // Material Keywords
  41. #pragma shader_feature_local _NORMALMAP
  42. #pragma shader_feature_local_fragment _ALPHATEST_ON
  43. #pragma shader_feature_local_fragment _ALPHAPREMULTIPLY_ON
  44. // -------------------------------------
  45. // Universal Pipeline keywords
  46. #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
  47. // -------------------------------------
  48. // Unity defined keywords
  49. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  50. #pragma multi_compile _ LIGHTMAP_ON
  51. #pragma multi_compile_fog
  52. //--------------------------------------
  53. // GPU Instancing
  54. #pragma multi_compile_instancing
  55. #pragma multi_compile _ DOTS_INSTANCING_ON
  56. // Lighting include is needed because of GI
  57. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  58. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  59. struct Attributes
  60. {
  61. float4 positionOS : POSITION;
  62. float2 uv : TEXCOORD0;
  63. float2 lightmapUV : TEXCOORD1;
  64. float3 normalOS : NORMAL;
  65. float4 tangentOS : TANGENT;
  66. UNITY_VERTEX_INPUT_INSTANCE_ID
  67. };
  68. struct Varyings
  69. {
  70. float3 uv0AndFogCoord : TEXCOORD0; // xy: uv0, z: fogCoord
  71. DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);
  72. half3 normalWS : TEXCOORD2;
  73. #if defined(_NORMALMAP)
  74. half4 tangentWS : TEXCOORD3;
  75. #endif
  76. float4 vertex : SV_POSITION;
  77. UNITY_VERTEX_INPUT_INSTANCE_ID
  78. UNITY_VERTEX_OUTPUT_STEREO
  79. };
  80. Varyings vert(Attributes input)
  81. {
  82. Varyings output = (Varyings)0;
  83. UNITY_SETUP_INSTANCE_ID(input);
  84. UNITY_TRANSFER_INSTANCE_ID(input, output);
  85. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  86. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  87. output.vertex = vertexInput.positionCS;
  88. output.uv0AndFogCoord.xy = TRANSFORM_TEX(input.uv, _BaseMap);
  89. output.uv0AndFogCoord.z = ComputeFogFactor(vertexInput.positionCS.z);
  90. // normalWS and tangentWS already normalize.
  91. // this is required to avoid skewing the direction during interpolation
  92. // also required for per-vertex SH evaluation
  93. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  94. output.normalWS = normalInput.normalWS;
  95. #if defined(_NORMALMAP)
  96. real sign = input.tangentOS.w * GetOddNegativeScale();
  97. output.tangentWS = half4(normalInput.tangentWS.xyz, sign);
  98. #endif
  99. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  100. OUTPUT_SH(output.normalWS, output.vertexSH);
  101. return output;
  102. }
  103. half4 frag(Varyings input) : SV_Target
  104. {
  105. UNITY_SETUP_INSTANCE_ID(input);
  106. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  107. half2 uv = input.uv0AndFogCoord.xy;
  108. half4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
  109. half3 color = texColor.rgb * _BaseColor.rgb;
  110. half alpha = texColor.a * _BaseColor.a;
  111. AlphaDiscard(alpha, _Cutoff);
  112. #ifdef _ALPHAPREMULTIPLY_ON
  113. color *= alpha;
  114. #endif
  115. #if defined(_NORMALMAP)
  116. half3 normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap)).xyz;
  117. float sgn = input.tangentWS.w; // should be either +1 or -1
  118. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  119. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS));
  120. #else
  121. half3 normalWS = input.normalWS;
  122. #endif
  123. normalWS = NormalizeNormalPerPixel(normalWS);
  124. color *= SAMPLE_GI(input.lightmapUV, input.vertexSH, normalWS);
  125. #if defined(_SCREEN_SPACE_OCCLUSION)
  126. float2 normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.vertex);
  127. color *= SampleAmbientOcclusion(normalizedScreenSpaceUV);
  128. #endif
  129. color = MixFog(color, input.uv0AndFogCoord.z);
  130. alpha = OutputAlpha(alpha, _Surface);
  131. return half4(color, alpha);
  132. }
  133. ENDHLSL
  134. }
  135. Pass
  136. {
  137. Tags{"LightMode" = "DepthOnly"}
  138. ZWrite On
  139. ColorMask 0
  140. HLSLPROGRAM
  141. #pragma exclude_renderers gles gles3 glcore
  142. #pragma target 4.5
  143. #pragma vertex DepthOnlyVertex
  144. #pragma fragment DepthOnlyFragment
  145. // -------------------------------------
  146. // Material Keywords
  147. #pragma shader_feature_local_fragment _ALPHATEST_ON
  148. //--------------------------------------
  149. // GPU Instancing
  150. #pragma multi_compile_instancing
  151. #pragma multi_compile _ DOTS_INSTANCING_ON
  152. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  153. #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
  154. ENDHLSL
  155. }
  156. // This pass is used when drawing to a _CameraNormalsTexture texture
  157. Pass
  158. {
  159. Name "DepthNormals"
  160. Tags{"LightMode" = "DepthNormals"}
  161. ZWrite On
  162. Cull[_Cull]
  163. HLSLPROGRAM
  164. #pragma exclude_renderers gles gles3 glcore
  165. #pragma target 4.5
  166. #pragma vertex DepthNormalsVertex
  167. #pragma fragment DepthNormalsFragment
  168. // -------------------------------------
  169. // Material Keywords
  170. #pragma shader_feature_local _ _NORMALMAP
  171. #pragma shader_feature_local_fragment _ALPHATEST_ON
  172. //--------------------------------------
  173. // Defines
  174. #define BUMP_SCALE_NOT_SUPPORTED 1
  175. //--------------------------------------
  176. // GPU Instancing
  177. #pragma multi_compile_instancing
  178. #pragma multi_compile _ DOTS_INSTANCING_ON
  179. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  180. #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthNormalsPass.hlsl"
  181. ENDHLSL
  182. }
  183. // This pass it not used during regular rendering, only for lightmap baking.
  184. Pass
  185. {
  186. Name "Meta"
  187. Tags{"LightMode" = "Meta"}
  188. Cull Off
  189. HLSLPROGRAM
  190. #pragma exclude_renderers gles gles3 glcore
  191. #pragma target 4.5
  192. #pragma vertex UniversalVertexMeta
  193. #pragma fragment UniversalFragmentMetaBakedLit
  194. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  195. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitMetaPass.hlsl"
  196. ENDHLSL
  197. }
  198. Pass
  199. {
  200. Name "Universal2D"
  201. Tags{ "LightMode" = "Universal2D" }
  202. Blend[_SrcBlend][_DstBlend]
  203. ZWrite[_ZWrite]
  204. Cull[_Cull]
  205. HLSLPROGRAM
  206. #pragma exclude_renderers gles gles3 glcore
  207. #pragma target 4.5
  208. #pragma vertex vert
  209. #pragma fragment frag
  210. #pragma shader_feature_local_fragment _ALPHATEST_ON
  211. #pragma shader_feature_local_fragment _ALPHAPREMULTIPLY_ON
  212. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  213. #include "Packages/com.unity.render-pipelines.universal/Shaders/Utils/Universal2D.hlsl"
  214. ENDHLSL
  215. }
  216. }
  217. SubShader
  218. {
  219. Tags { "RenderType" = "Opaque" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" "ShaderModel"="2.0"}
  220. LOD 100
  221. Blend [_SrcBlend][_DstBlend]
  222. ZWrite [_ZWrite]
  223. Cull [_Cull]
  224. Pass
  225. {
  226. Name "BakedLit"
  227. Tags{ "LightMode" = "UniversalForwardOnly" }
  228. HLSLPROGRAM
  229. #pragma only_renderers gles gles3 glcore d3d11
  230. #pragma target 2.0
  231. #pragma vertex vert
  232. #pragma fragment frag
  233. // -------------------------------------
  234. // Material Keywords
  235. #pragma shader_feature_local _NORMALMAP
  236. #pragma shader_feature_local_fragment _ALPHATEST_ON
  237. #pragma shader_feature_local_fragment _ALPHAPREMULTIPLY_ON
  238. // -------------------------------------
  239. // Universal Pipeline keywords
  240. #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
  241. // -------------------------------------
  242. // Unity defined keywords
  243. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  244. #pragma multi_compile _ LIGHTMAP_ON
  245. #pragma multi_compile_fog
  246. //--------------------------------------
  247. // GPU Instancing
  248. #pragma multi_compile_instancing
  249. // Lighting include is needed because of GI
  250. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  251. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  252. struct Attributes
  253. {
  254. float4 positionOS : POSITION;
  255. float2 uv : TEXCOORD0;
  256. float2 lightmapUV : TEXCOORD1;
  257. float3 normalOS : NORMAL;
  258. float4 tangentOS : TANGENT;
  259. UNITY_VERTEX_INPUT_INSTANCE_ID
  260. };
  261. struct Varyings
  262. {
  263. float3 uv0AndFogCoord : TEXCOORD0; // xy: uv0, z: fogCoord
  264. DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);
  265. half3 normalWS : TEXCOORD2;
  266. #if defined(_NORMALMAP)
  267. half4 tangentWS : TEXCOORD3;
  268. #endif
  269. float4 vertex : SV_POSITION;
  270. UNITY_VERTEX_INPUT_INSTANCE_ID
  271. UNITY_VERTEX_OUTPUT_STEREO
  272. };
  273. Varyings vert(Attributes input)
  274. {
  275. Varyings output = (Varyings)0;
  276. UNITY_SETUP_INSTANCE_ID(input);
  277. UNITY_TRANSFER_INSTANCE_ID(input, output);
  278. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  279. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  280. output.vertex = vertexInput.positionCS;
  281. output.uv0AndFogCoord.xy = TRANSFORM_TEX(input.uv, _BaseMap);
  282. output.uv0AndFogCoord.z = ComputeFogFactor(vertexInput.positionCS.z);
  283. // normalWS and tangentWS already normalize.
  284. // this is required to avoid skewing the direction during interpolation
  285. // also required for per-vertex SH evaluation
  286. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  287. output.normalWS = normalInput.normalWS;
  288. #if defined(_NORMALMAP)
  289. real sign = input.tangentOS.w * GetOddNegativeScale();
  290. output.tangentWS = half4(normalInput.tangentWS.xyz, sign);
  291. #endif
  292. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  293. OUTPUT_SH(output.normalWS, output.vertexSH);
  294. return output;
  295. }
  296. half4 frag(Varyings input) : SV_Target
  297. {
  298. UNITY_SETUP_INSTANCE_ID(input);
  299. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  300. half2 uv = input.uv0AndFogCoord.xy;
  301. half4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
  302. half3 color = texColor.rgb * _BaseColor.rgb;
  303. half alpha = texColor.a * _BaseColor.a;
  304. AlphaDiscard(alpha, _Cutoff);
  305. #ifdef _ALPHAPREMULTIPLY_ON
  306. color *= alpha;
  307. #endif
  308. #if defined(_NORMALMAP)
  309. half3 normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap)).xyz;
  310. float sgn = input.tangentWS.w; // should be either +1 or -1
  311. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  312. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS));
  313. #else
  314. half3 normalWS = input.normalWS;
  315. #endif
  316. normalWS = NormalizeNormalPerPixel(normalWS);
  317. color *= SAMPLE_GI(input.lightmapUV, input.vertexSH, normalWS);
  318. #if defined(_SCREEN_SPACE_OCCLUSION)
  319. float2 normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.vertex);
  320. color *= SampleAmbientOcclusion(normalizedScreenSpaceUV);
  321. #endif
  322. color = MixFog(color, input.uv0AndFogCoord.z);
  323. alpha = OutputAlpha(alpha, _Surface);
  324. return half4(color, alpha);
  325. }
  326. ENDHLSL
  327. }
  328. Pass
  329. {
  330. Tags{"LightMode" = "DepthOnly"}
  331. ZWrite On
  332. ColorMask 0
  333. HLSLPROGRAM
  334. #pragma only_renderers gles gles3 glcore d3d11
  335. #pragma target 2.0
  336. //--------------------------------------
  337. // GPU Instancing
  338. #pragma multi_compile_instancing
  339. #pragma vertex DepthOnlyVertex
  340. #pragma fragment DepthOnlyFragment
  341. // -------------------------------------
  342. // Material Keywords
  343. #pragma shader_feature_local_fragment _ALPHATEST_ON
  344. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  345. #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
  346. ENDHLSL
  347. }
  348. // This pass is used when drawing to a _CameraNormalsTexture texture
  349. Pass
  350. {
  351. Name "DepthNormals"
  352. Tags{"LightMode" = "DepthNormals"}
  353. ZWrite On
  354. Cull[_Cull]
  355. HLSLPROGRAM
  356. #pragma only_renderers gles gles3 glcore d3d11
  357. #pragma target 2.0
  358. #pragma vertex DepthNormalsVertex
  359. #pragma fragment DepthNormalsFragment
  360. // -------------------------------------
  361. // Material Keywords
  362. #pragma shader_feature_local _ _NORMALMAP
  363. #pragma shader_feature_local_fragment _ALPHATEST_ON
  364. //--------------------------------------
  365. // Defines
  366. #define BUMP_SCALE_NOT_SUPPORTED 1
  367. //--------------------------------------
  368. // GPU Instancing
  369. #pragma multi_compile_instancing
  370. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  371. #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthNormalsPass.hlsl"
  372. ENDHLSL
  373. }
  374. // This pass it not used during regular rendering, only for lightmap baking.
  375. Pass
  376. {
  377. Name "Meta"
  378. Tags{"LightMode" = "Meta"}
  379. Cull Off
  380. HLSLPROGRAM
  381. #pragma only_renderers gles gles3 glcore d3d11
  382. #pragma target 2.0
  383. #pragma vertex UniversalVertexMeta
  384. #pragma fragment UniversalFragmentMetaBakedLit
  385. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  386. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitMetaPass.hlsl"
  387. ENDHLSL
  388. }
  389. Pass
  390. {
  391. Name "Universal2D"
  392. Tags{ "LightMode" = "Universal2D" }
  393. Blend[_SrcBlend][_DstBlend]
  394. ZWrite[_ZWrite]
  395. Cull[_Cull]
  396. HLSLPROGRAM
  397. #pragma only_renderers gles gles3 glcore d3d11
  398. #pragma target 2.0
  399. #pragma vertex vert
  400. #pragma fragment frag
  401. #pragma shader_feature_local_fragment _ALPHATEST_ON
  402. #pragma shader_feature_local_fragment _ALPHAPREMULTIPLY_ON
  403. #include "Packages/com.unity.render-pipelines.universal/Shaders/BakedLitInput.hlsl"
  404. #include "Packages/com.unity.render-pipelines.universal/Shaders/Utils/Universal2D.hlsl"
  405. ENDHLSL
  406. }
  407. }
  408. FallBack "Universal Render Pipeline/Unlit"
  409. CustomEditor "UnityEditor.Rendering.Universal.ShaderGUI.BakedLitShader"
  410. }