TerrainLitBasemapGen.shader 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. Shader "Hidden/Universal Render Pipeline/Terrain/Lit (Basemap Gen)"
  2. {
  3. Properties
  4. {
  5. // Layer count is passed down to guide height-blend enable/disable, due
  6. // to the fact that heigh-based blend will be broken with multipass.
  7. [HideInInspector] [PerRendererData] _NumLayersCount ("Total Layer Count", Float) = 1.0
  8. [HideInInspector] _Control("AlphaMap", 2D) = "" {}
  9. [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
  10. [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
  11. [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
  12. [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
  13. [HideInInspector] _Mask3("Mask 3 (A)", 2D) = "grey" {}
  14. [HideInInspector] _Mask2("Mask 2 (B)", 2D) = "grey" {}
  15. [HideInInspector] _Mask1("Mask 1 (G)", 2D) = "grey" {}
  16. [HideInInspector] _Mask0("Mask 0 (R)", 2D) = "grey" {}
  17. [HideInInspector] [Gamma] _Metallic0 ("Metallic 0", Range(0.0, 1.0)) = 0.0
  18. [HideInInspector] [Gamma] _Metallic1 ("Metallic 1", Range(0.0, 1.0)) = 0.0
  19. [HideInInspector] [Gamma] _Metallic2 ("Metallic 2", Range(0.0, 1.0)) = 0.0
  20. [HideInInspector] [Gamma] _Metallic3 ("Metallic 3", Range(0.0, 1.0)) = 0.0
  21. [HideInInspector] _Smoothness0 ("Smoothness 0", Range(0.0, 1.0)) = 1.0
  22. [HideInInspector] _Smoothness1 ("Smoothness 1", Range(0.0, 1.0)) = 1.0
  23. [HideInInspector] _Smoothness2 ("Smoothness 2", Range(0.0, 1.0)) = 1.0
  24. [HideInInspector] _Smoothness3 ("Smoothness 3", Range(0.0, 1.0)) = 1.0
  25. [HideInInspector] _DstBlend("DstBlend", Float) = 0.0
  26. }
  27. Subshader
  28. {
  29. HLSLINCLUDE
  30. #pragma target 3.0
  31. #define _METALLICSPECGLOSSMAP 1
  32. #define _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A 1
  33. #define _TERRAIN_BASEMAP_GEN
  34. #pragma shader_feature_local _TERRAIN_BLEND_HEIGHT
  35. #pragma shader_feature_local _MASKMAP
  36. #include "TerrainLitInput.hlsl"
  37. #include "TerrainLitPasses.hlsl"
  38. ENDHLSL
  39. Pass
  40. {
  41. Tags
  42. {
  43. "Name" = "_MainTex"
  44. "Format" = "ARGB32"
  45. "Size" = "1"
  46. }
  47. ZTest Always Cull Off ZWrite Off
  48. Blend One [_DstBlend]
  49. HLSLPROGRAM
  50. #pragma vertex Vert
  51. #pragma fragment Frag
  52. Varyings Vert(Attributes IN)
  53. {
  54. Varyings output = (Varyings) 0;
  55. output.clipPos = TransformWorldToHClip(IN.positionOS.xyz);
  56. // NOTE : This is basically coming from the vertex shader in TerrainLitPasses
  57. // There are other plenty of other values that the original version computes, but for this
  58. // pass, we are only interested in a few, so I'm just skipping the rest.
  59. output.uvMainAndLM.xy = IN.texcoord;
  60. output.uvSplat01.xy = TRANSFORM_TEX(IN.texcoord, _Splat0);
  61. output.uvSplat01.zw = TRANSFORM_TEX(IN.texcoord, _Splat1);
  62. output.uvSplat23.xy = TRANSFORM_TEX(IN.texcoord, _Splat2);
  63. output.uvSplat23.zw = TRANSFORM_TEX(IN.texcoord, _Splat3);
  64. return output;
  65. }
  66. half4 Frag(Varyings IN) : SV_Target
  67. {
  68. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  69. half4 splatControl;
  70. half weight;
  71. half4 mixedDiffuse = 0.0h;
  72. half4 defaultSmoothness = 0.0h;
  73. half4 masks[4];
  74. float2 splatUV = (IN.uvMainAndLM.xy * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  75. splatControl = SAMPLE_TEXTURE2D(_Control, sampler_Control, splatUV);
  76. masks[0] = 1.0h;
  77. masks[1] = 1.0h;
  78. masks[2] = 1.0h;
  79. masks[3] = 1.0h;
  80. #ifdef _MASKMAP
  81. masks[0] = SAMPLE_TEXTURE2D(_Mask0, sampler_Mask0, IN.uvSplat01.xy);
  82. masks[1] = SAMPLE_TEXTURE2D(_Mask1, sampler_Mask0, IN.uvSplat01.zw);
  83. masks[2] = SAMPLE_TEXTURE2D(_Mask2, sampler_Mask0, IN.uvSplat23.xy);
  84. masks[3] = SAMPLE_TEXTURE2D(_Mask3, sampler_Mask0, IN.uvSplat23.zw);
  85. #ifdef _TERRAIN_BLEND_HEIGHT
  86. HeightBasedSplatModify(splatControl, masks);
  87. #endif
  88. #endif
  89. SplatmapMix(IN.uvMainAndLM, IN.uvSplat01, IN.uvSplat23, splatControl, weight, mixedDiffuse, defaultSmoothness, normalTS);
  90. half4 hasMask = half4(_LayerHasMask0, _LayerHasMask1, _LayerHasMask2, _LayerHasMask3);
  91. half4 maskSmoothness = half4(masks[0].a, masks[1].a, masks[2].a, masks[3].a);
  92. maskSmoothness *= half4(_MaskMapRemapScale0.a, _MaskMapRemapScale1.a, _MaskMapRemapScale2.a, _MaskMapRemapScale3.a);
  93. maskSmoothness += half4(_MaskMapRemapOffset0.a, _MaskMapRemapOffset1.a, _MaskMapRemapOffset2.a, _MaskMapRemapOffset3.a);
  94. defaultSmoothness = lerp(defaultSmoothness, maskSmoothness, hasMask);
  95. half smoothness = dot(splatControl, defaultSmoothness);
  96. return half4(mixedDiffuse.rgb, smoothness);
  97. }
  98. ENDHLSL
  99. }
  100. Pass
  101. {
  102. Tags
  103. {
  104. "Name" = "_MetallicTex"
  105. "Format" = "R8"
  106. "Size" = "1/4"
  107. "EmptyColor" = "FF000000"
  108. }
  109. ZTest Always Cull Off ZWrite Off
  110. Blend One [_DstBlend]
  111. HLSLPROGRAM
  112. #pragma vertex Vert
  113. #pragma fragment Frag
  114. Varyings Vert(Attributes IN)
  115. {
  116. Varyings output = (Varyings)0;
  117. output.clipPos = TransformWorldToHClip(IN.positionOS.xyz);
  118. // This is just like the other in that it is from TerrainLitPasses
  119. output.uvMainAndLM.xy = IN.texcoord;
  120. output.uvSplat01.xy = TRANSFORM_TEX(IN.texcoord, _Splat0);
  121. output.uvSplat01.zw = TRANSFORM_TEX(IN.texcoord, _Splat1);
  122. output.uvSplat23.xy = TRANSFORM_TEX(IN.texcoord, _Splat2);
  123. output.uvSplat23.zw = TRANSFORM_TEX(IN.texcoord, _Splat3);
  124. return output;
  125. }
  126. half4 Frag(Varyings IN) : SV_Target
  127. {
  128. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  129. half4 splatControl;
  130. half weight;
  131. half4 mixedDiffuse;
  132. half4 defaultSmoothness;
  133. half4 masks[4];
  134. float2 splatUV = (IN.uvMainAndLM.xy * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  135. splatControl = SAMPLE_TEXTURE2D(_Control, sampler_Control, splatUV);
  136. masks[0] = 1.0h;
  137. masks[1] = 1.0h;
  138. masks[2] = 1.0h;
  139. masks[3] = 1.0h;
  140. #ifdef _MASKMAP
  141. masks[0] = SAMPLE_TEXTURE2D(_Mask0, sampler_Mask0, IN.uvSplat01.xy);
  142. masks[1] = SAMPLE_TEXTURE2D(_Mask1, sampler_Mask0, IN.uvSplat01.zw);
  143. masks[2] = SAMPLE_TEXTURE2D(_Mask2, sampler_Mask0, IN.uvSplat23.xy);
  144. masks[3] = SAMPLE_TEXTURE2D(_Mask3, sampler_Mask0, IN.uvSplat23.zw);
  145. #ifdef _TERRAIN_BLEND_HEIGHT
  146. HeightBasedSplatModify(splatControl, masks);
  147. #endif
  148. #endif
  149. SplatmapMix(IN.uvMainAndLM, IN.uvSplat01, IN.uvSplat23, splatControl, weight, mixedDiffuse, defaultSmoothness, normalTS);
  150. half4 hasMask = half4(_LayerHasMask0, _LayerHasMask1, _LayerHasMask2, _LayerHasMask3);
  151. half4 defaultMetallic = half4(_Metallic0, _Metallic1, _Metallic2, _Metallic3);
  152. half4 maskMetallic = half4(masks[0].r, masks[1].r, masks[2].r, masks[3].r);
  153. maskMetallic *= half4(_MaskMapRemapScale0.r, _MaskMapRemapScale1.r, _MaskMapRemapScale3.r, _MaskMapRemapScale3.r);
  154. maskMetallic += half4(_MaskMapRemapOffset0.r, _MaskMapRemapOffset1.r, _MaskMapRemapOffset2.r, _MaskMapRemapOffset3.r);
  155. defaultMetallic = lerp(defaultMetallic, maskMetallic, hasMask);
  156. half metallic = dot(splatControl, defaultMetallic);
  157. return metallic;
  158. }
  159. ENDHLSL
  160. }
  161. }
  162. }