TerrainLitBasemapGen.shader 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // Required to compile gles 2.0 with standard srp library
  31. #pragma prefer_hlslcc gles
  32. #pragma exclude_renderers d3d11_9x
  33. #pragma target 3.0
  34. #define _METALLICSPECGLOSSMAP 1
  35. #define _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A 1
  36. #define _TERRAIN_BASEMAP_GEN
  37. #pragma shader_feature_local _TERRAIN_BLEND_HEIGHT
  38. #pragma shader_feature_local _MASKMAP
  39. #include "TerrainLitInput.hlsl"
  40. #include "TerrainLitPasses.hlsl"
  41. ENDHLSL
  42. Pass
  43. {
  44. Tags
  45. {
  46. "Name" = "_MainTex"
  47. "Format" = "ARGB32"
  48. "Size" = "1"
  49. }
  50. ZTest Always Cull Off ZWrite Off
  51. Blend One [_DstBlend]
  52. HLSLPROGRAM
  53. #pragma vertex Vert
  54. #pragma fragment Frag
  55. Varyings Vert(Attributes IN)
  56. {
  57. Varyings output = (Varyings) 0;
  58. output.clipPos = TransformWorldToHClip(IN.positionOS.xyz);
  59. // NOTE : This is basically coming from the vertex shader in TerrainLitPasses
  60. // There are other plenty of other values that the original version computes, but for this
  61. // pass, we are only interested in a few, so I'm just skipping the rest.
  62. output.uvMainAndLM.xy = IN.texcoord;
  63. output.uvSplat01.xy = TRANSFORM_TEX(IN.texcoord, _Splat0);
  64. output.uvSplat01.zw = TRANSFORM_TEX(IN.texcoord, _Splat1);
  65. output.uvSplat23.xy = TRANSFORM_TEX(IN.texcoord, _Splat2);
  66. output.uvSplat23.zw = TRANSFORM_TEX(IN.texcoord, _Splat3);
  67. return output;
  68. }
  69. half4 Frag(Varyings IN) : SV_Target
  70. {
  71. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  72. half4 splatControl;
  73. half weight;
  74. half4 mixedDiffuse = 0.0h;
  75. half4 defaultSmoothness = 0.0h;
  76. half4 masks[4];
  77. float2 splatUV = (IN.uvMainAndLM.xy * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  78. splatControl = SAMPLE_TEXTURE2D(_Control, sampler_Control, splatUV);
  79. masks[0] = 1.0h;
  80. masks[1] = 1.0h;
  81. masks[2] = 1.0h;
  82. masks[3] = 1.0h;
  83. #ifdef _MASKMAP
  84. masks[0] = SAMPLE_TEXTURE2D(_Mask0, sampler_Mask0, IN.uvSplat01.xy);
  85. masks[1] = SAMPLE_TEXTURE2D(_Mask1, sampler_Mask0, IN.uvSplat01.zw);
  86. masks[2] = SAMPLE_TEXTURE2D(_Mask2, sampler_Mask0, IN.uvSplat23.xy);
  87. masks[3] = SAMPLE_TEXTURE2D(_Mask3, sampler_Mask0, IN.uvSplat23.zw);
  88. #ifdef _TERRAIN_BLEND_HEIGHT
  89. HeightBasedSplatModify(splatControl, masks);
  90. #endif
  91. #endif
  92. SplatmapMix(IN.uvMainAndLM, IN.uvSplat01, IN.uvSplat23, splatControl, weight, mixedDiffuse, defaultSmoothness, normalTS);
  93. half4 hasMask = half4(_LayerHasMask0, _LayerHasMask1, _LayerHasMask2, _LayerHasMask3);
  94. half4 maskSmoothness = half4(masks[0].a, masks[1].a, masks[2].a, masks[3].a);
  95. maskSmoothness *= half4(_MaskMapRemapScale0.a, _MaskMapRemapScale1.a, _MaskMapRemapScale2.a, _MaskMapRemapScale3.a);
  96. maskSmoothness += half4(_MaskMapRemapOffset0.a, _MaskMapRemapOffset1.a, _MaskMapRemapOffset2.a, _MaskMapRemapOffset3.a);
  97. defaultSmoothness = lerp(defaultSmoothness, maskSmoothness, hasMask);
  98. half smoothness = dot(splatControl, defaultSmoothness);
  99. return half4(mixedDiffuse.rgb, smoothness);
  100. }
  101. ENDHLSL
  102. }
  103. Pass
  104. {
  105. Tags
  106. {
  107. "Name" = "_MetallicTex"
  108. "Format" = "R8"
  109. "Size" = "1/4"
  110. "EmptyColor" = "FF000000"
  111. }
  112. ZTest Always Cull Off ZWrite Off
  113. Blend One [_DstBlend]
  114. HLSLPROGRAM
  115. #pragma vertex Vert
  116. #pragma fragment Frag
  117. Varyings Vert(Attributes IN)
  118. {
  119. Varyings output = (Varyings)0;
  120. output.clipPos = TransformWorldToHClip(IN.positionOS.xyz);
  121. // This is just like the other in that it is from TerrainLitPasses
  122. output.uvMainAndLM.xy = IN.texcoord;
  123. output.uvSplat01.xy = TRANSFORM_TEX(IN.texcoord, _Splat0);
  124. output.uvSplat01.zw = TRANSFORM_TEX(IN.texcoord, _Splat1);
  125. output.uvSplat23.xy = TRANSFORM_TEX(IN.texcoord, _Splat2);
  126. output.uvSplat23.zw = TRANSFORM_TEX(IN.texcoord, _Splat3);
  127. return output;
  128. }
  129. half4 Frag(Varyings IN) : SV_Target
  130. {
  131. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  132. half4 splatControl;
  133. half weight;
  134. half4 mixedDiffuse;
  135. half4 defaultSmoothness;
  136. half4 masks[4];
  137. float2 splatUV = (IN.uvMainAndLM.xy * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  138. splatControl = SAMPLE_TEXTURE2D(_Control, sampler_Control, splatUV);
  139. masks[0] = 1.0h;
  140. masks[1] = 1.0h;
  141. masks[2] = 1.0h;
  142. masks[3] = 1.0h;
  143. #ifdef _MASKMAP
  144. masks[0] = SAMPLE_TEXTURE2D(_Mask0, sampler_Mask0, IN.uvSplat01.xy);
  145. masks[1] = SAMPLE_TEXTURE2D(_Mask1, sampler_Mask0, IN.uvSplat01.zw);
  146. masks[2] = SAMPLE_TEXTURE2D(_Mask2, sampler_Mask0, IN.uvSplat23.xy);
  147. masks[3] = SAMPLE_TEXTURE2D(_Mask3, sampler_Mask0, IN.uvSplat23.zw);
  148. #ifdef _TERRAIN_BLEND_HEIGHT
  149. HeightBasedSplatModify(splatControl, masks);
  150. #endif
  151. #endif
  152. SplatmapMix(IN.uvMainAndLM, IN.uvSplat01, IN.uvSplat23, splatControl, weight, mixedDiffuse, defaultSmoothness, normalTS);
  153. half4 hasMask = half4(_LayerHasMask0, _LayerHasMask1, _LayerHasMask2, _LayerHasMask3);
  154. half4 defaultMetallic = half4(_Metallic0, _Metallic1, _Metallic2, _Metallic3);
  155. half4 maskMetallic = half4(masks[0].r, masks[1].r, masks[2].r, masks[3].r);
  156. maskMetallic *= half4(_MaskMapRemapScale0.r, _MaskMapRemapScale1.r, _MaskMapRemapScale3.r, _MaskMapRemapScale3.r);
  157. maskMetallic += half4(_MaskMapRemapOffset0.r, _MaskMapRemapOffset1.r, _MaskMapRemapOffset2.r, _MaskMapRemapOffset3.r);
  158. defaultMetallic = lerp(defaultMetallic, maskMetallic, hasMask);
  159. half metallic = dot(splatControl, defaultMetallic);
  160. return metallic;
  161. }
  162. ENDHLSL
  163. }
  164. }
  165. }