InTerra_TerrainLitBasemapGen.shader 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //NOTE! This file is based on Unity file "TerrainLitBasemapGen.shader" which was used as a template for adding all the InTerra features.
  2. Shader "Hidden/InTerra/Lit (Basemap Gen)"
  3. {
  4. Properties
  5. {
  6. // Layer count is passed down to guide height-blend enable/disable, due
  7. // to the fact that heigh-based blend will be broken with multipass.
  8. [HideInInspector] [PerRendererData] _NumLayersCount ("Total Layer Count", Float) = 1.0
  9. [HideInInspector] _Control("AlphaMap", 2D) = "" {}
  10. [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
  11. [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
  12. [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
  13. [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
  14. [HideInInspector] _Mask3("Mask 3 (A)", 2D) = "grey" {}
  15. [HideInInspector] _Mask2("Mask 2 (B)", 2D) = "grey" {}
  16. [HideInInspector] _Mask1("Mask 1 (G)", 2D) = "grey" {}
  17. [HideInInspector] _Mask0("Mask 0 (R)", 2D) = "grey" {}
  18. [HideInInspector] [Gamma] _Metallic0 ("Metallic 0", Range(0.0, 1.0)) = 0.0
  19. [HideInInspector] [Gamma] _Metallic1 ("Metallic 1", Range(0.0, 1.0)) = 0.0
  20. [HideInInspector] [Gamma] _Metallic2 ("Metallic 2", Range(0.0, 1.0)) = 0.0
  21. [HideInInspector] [Gamma] _Metallic3 ("Metallic 3", Range(0.0, 1.0)) = 0.0
  22. [HideInInspector] _Smoothness0 ("Smoothness 0", Range(0.0, 1.0)) = 1.0
  23. [HideInInspector] _Smoothness1 ("Smoothness 1", Range(0.0, 1.0)) = 1.0
  24. [HideInInspector] _Smoothness2 ("Smoothness 2", Range(0.0, 1.0)) = 1.0
  25. [HideInInspector] _Smoothness3 ("Smoothness 3", Range(0.0, 1.0)) = 1.0
  26. [HideInInspector] _DstBlend("DstBlend", Float) = 0.0
  27. }
  28. Subshader
  29. {
  30. HLSLINCLUDE
  31. #pragma target 3.0
  32. #define _METALLICSPECGLOSSMAP 1
  33. #define _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A 1
  34. #define _TERRAIN_BASEMAP_GEN
  35. #pragma shader_feature_local _MASKMAP
  36. #pragma shader_feature_local __ _TERRAIN_TRIPLANAR_ONE _TERRAIN_TRIPLANAR
  37. #pragma shader_feature_local _TERRAIN_DISTANCEBLEND
  38. #pragma shader_feature_local __ _LAYERS_TWO _LAYERS_EIGHT
  39. #include "InTerra_URP_DefinedGlobalKeywords.hlsl"
  40. #define INTERRA_TERRAIN
  41. #if defined(_TERRAIN_TRIPLANAR) || defined(_TERRAIN_TRIPLANAR_ONE)
  42. #ifdef _TERRAIN_TRIPLANAR_ONE
  43. #define TRIPLANAR_TINT
  44. #endif
  45. #undef _TERRAIN_TRIPLANAR
  46. #undef _TERRAIN_TRIPLANAR_ONE
  47. #endif
  48. #include "InTerra_TerrainLitInput.hlsl"
  49. #include "InTerra_TerrainLitPasses.hlsl"
  50. ENDHLSL
  51. PackageRequirements { "com.unity.render-pipelines.universal":"[12.1,19.0]" }
  52. //Tags {"RenderPipeline" = "UniversalRenderPipeline" }
  53. Pass
  54. {
  55. Tags
  56. {
  57. "Name" = "_MainTex"
  58. "Format" = "ARGB32"
  59. "Size" = "1"
  60. }
  61. ZTest Always Cull Off ZWrite Off
  62. Blend One [_DstBlend]
  63. HLSLPROGRAM
  64. #pragma vertex Vert
  65. #pragma fragment Frag
  66. Varyings Vert(Attributes IN)
  67. {
  68. Varyings output = (Varyings) 0;
  69. output.clipPos = TransformWorldToHClip(IN.positionOS.xyz);
  70. // NOTE : This is basically coming from the vertex shader in TerrainLitPasses
  71. // There are other plenty of other values that the original version computes, but for this
  72. // pass, we are only interested in a few, so I'm just skipping the rest.
  73. output.uvMainAndLM.xy = IN.texcoord;
  74. output.uvSplat01.xy = TRANSFORM_TEX(IN.texcoord, _Splat0);
  75. output.uvSplat01.zw = TRANSFORM_TEX(IN.texcoord, _Splat1);
  76. #ifndef _LAYERS_TWO
  77. output.uvSplat23.xy = TRANSFORM_TEX(IN.texcoord, _Splat2);
  78. output.uvSplat23.zw = TRANSFORM_TEX(IN.texcoord, _Splat3);
  79. #ifdef _LAYERS_EIGHT
  80. output.uvSplat45.xy = TRANSFORM_TEX(IN.texcoord, _Splat4);
  81. output.uvSplat45.zw = TRANSFORM_TEX(IN.texcoord, _Splat5);
  82. output.uvSplat67.xy = TRANSFORM_TEX(IN.texcoord, _Splat6);
  83. output.uvSplat67.zw = TRANSFORM_TEX(IN.texcoord, _Splat7);
  84. #endif
  85. #endif
  86. return output;
  87. }
  88. half4 Frag(Varyings IN) : SV_Target
  89. {
  90. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  91. half4 splatControl;
  92. half weight;
  93. half4 mixedDiffuse = 0.0h;
  94. half metallic;
  95. half occlusion;
  96. half smoothness;
  97. float2 uv[_LAYER_COUNT];
  98. float4 inUV[_LAYER_COUNT/2];
  99. inUV[0] = IN.uvSplat01;
  100. #ifndef _LAYERS_TWO
  101. inUV[1] = IN.uvSplat23;
  102. #ifdef _LAYERS_EIGHT
  103. inUV[2] = IN.uvSplat45;
  104. inUV[3] = IN.uvSplat67;
  105. #endif
  106. #endif
  107. uvArray(uv, inUV);
  108. SplatmapMix(IN.uvMainAndLM, uv, float3(0, 0, 0), float3(0, 0, 0), IN.positionWS, weight, mixedDiffuse, smoothness, metallic, occlusion, normalTS);
  109. return half4(mixedDiffuse.rgb, smoothness);
  110. }
  111. ENDHLSL
  112. }
  113. Pass
  114. {
  115. Tags
  116. {
  117. "Name" = "_MetallicTex"
  118. "Format" = "RGBA32"
  119. "Size" = "1/4"
  120. "EmptyColor" = "FF000000"
  121. }
  122. ZTest Always Cull Off ZWrite Off
  123. Blend One [_DstBlend]
  124. HLSLPROGRAM
  125. #pragma vertex Vert
  126. #pragma fragment Frag
  127. Varyings Vert(Attributes IN)
  128. {
  129. Varyings output = (Varyings)0;
  130. output.clipPos = TransformWorldToHClip(IN.positionOS.xyz);
  131. // This is just like the other in that it is from TerrainLitPasses
  132. output.uvMainAndLM.xy = IN.texcoord;
  133. output.uvSplat01.xy = TRANSFORM_TEX(IN.texcoord, _Splat0);
  134. output.uvSplat01.zw = TRANSFORM_TEX(IN.texcoord, _Splat1);
  135. #ifndef _LAYERS_TWO
  136. output.uvSplat23.xy = TRANSFORM_TEX(IN.texcoord, _Splat2);
  137. output.uvSplat23.zw = TRANSFORM_TEX(IN.texcoord, _Splat3);
  138. #ifdef _LAYERS_EIGHT
  139. output.uvSplat45.xy = TRANSFORM_TEX(IN.texcoord, _Splat4);
  140. output.uvSplat45.zw = TRANSFORM_TEX(IN.texcoord, _Splat5);
  141. output.uvSplat67.xy = TRANSFORM_TEX(IN.texcoord, _Splat6);
  142. output.uvSplat67.zw = TRANSFORM_TEX(IN.texcoord, _Splat7);
  143. #endif
  144. #endif
  145. return output;
  146. }
  147. half4 Frag(Varyings IN) : SV_Target
  148. {
  149. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  150. half4 splatControl;
  151. half weight;
  152. half4 mixedDiffuse;
  153. half4 defaultSmoothness;
  154. half4 masks[4];
  155. half metallic;
  156. half occlusion;
  157. half smoothness;
  158. float2 uv[_LAYER_COUNT];
  159. float4 inUV[_LAYER_COUNT/2];
  160. inUV[0] = IN.uvSplat01;
  161. #ifndef _LAYERS_TWO
  162. inUV[1] = IN.uvSplat23;
  163. #ifdef _LAYERS_EIGHT
  164. inUV[2] = IN.uvSplat45;
  165. inUV[3] = IN.uvSplat67;
  166. #endif
  167. #endif
  168. uvArray(uv, inUV);
  169. SplatmapMix(IN.uvMainAndLM, uv, float3(0, 0, 0), float3(0, 0, 0), IN.positionWS, weight, mixedDiffuse, smoothness, metallic, occlusion, normalTS);
  170. return float4(metallic, occlusion, splatControl.r, SAMPLE_TEXTURE2D(_Splat0, sampler_Splat0, uv[0]).r * 1e-5f);
  171. }
  172. ENDHLSL
  173. }
  174. Pass
  175. {
  176. Tags
  177. {
  178. "Name" = "_TriplanarTex"
  179. "Format" = "ARGB32"
  180. "Size" = "1"
  181. }
  182. ZTest Always Cull Off ZWrite Off
  183. Blend One [_DstBlend]
  184. HLSLPROGRAM
  185. #pragma vertex Vert
  186. #pragma fragment Frag
  187. #define _TERRAIN_BASEMAP_GEN_TRIPLANAR
  188. Varyings Vert(Attributes IN)
  189. {
  190. Varyings output = (Varyings)0;
  191. output.clipPos = TransformWorldToHClip(IN.positionOS.xyz);
  192. // This is just like the other in that it is from TerrainLitPasses
  193. output.uvMainAndLM.xy = IN.texcoord;
  194. output.uvSplat01.xy = TRANSFORM_TEX(IN.texcoord, _Splat0);
  195. return output;
  196. }
  197. half4 Frag(Varyings IN) : SV_Target
  198. {
  199. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  200. half4 splatControl = float4(1, 0, 0, 0);
  201. half weight;
  202. half4 mixedDiffuse;
  203. half4 defaultSmoothness;
  204. half metallic;
  205. half occlusion;
  206. half smoothness;
  207. float2 uv[_LAYER_COUNT];
  208. half4 albedo[_LAYER_COUNT];
  209. float4 mask[_LAYER_COUNT];
  210. float4 inUV[_LAYER_COUNT/2];
  211. inUV[0] = IN.uvSplat01;
  212. #ifndef _LAYERS_TWO
  213. inUV[1] = IN.uvSplat23;
  214. #ifdef _LAYERS_EIGHT
  215. inUV[2] = IN.uvSplat45;
  216. inUV[3] = IN.uvSplat67;
  217. #endif
  218. #endif
  219. uvArray(uv, inUV);
  220. SplatmapMix(IN.uvMainAndLM, uv, float3(0, 0, 0), float3(0, 0, 0), IN.positionWS, weight, mixedDiffuse, smoothness, metallic, occlusion, normalTS);
  221. #ifdef _TERRAIN_DISTANCEBLEND
  222. uv[0] *= (_DiffuseRemapOffset0.r + 1) * _HT_distance_scale;
  223. #endif
  224. mask[0] = Mask(0);
  225. mask[0] = RemapMask(0);
  226. mask[0] *= weight;
  227. albedo[0] = SAMPLE_TEXTURE2D(_Splat0, sampler_Splat0, uv[0]) * weight;
  228. albedo[0].rgb *= DiffuseRemap(0).xyz;
  229. albedo[0].a = Smoothness(0).x;
  230. return half4(albedo[0].rgb, albedo[0].a);
  231. }
  232. ENDHLSL
  233. }
  234. Pass
  235. {
  236. Tags
  237. {
  238. "Name" = "_Triplanar_MetallicAO"
  239. "Format" = "RGBA32"
  240. "Size" = "1/4"
  241. }
  242. ZTest Always Cull Off ZWrite Off
  243. Blend One [_DstBlend]
  244. HLSLPROGRAM
  245. #pragma vertex Vert
  246. #pragma fragment Frag
  247. #define _TERRAIN_BASEMAP_GEN_TRIPLANAR
  248. Varyings Vert(Attributes IN)
  249. {
  250. Varyings output = (Varyings)0;
  251. output.clipPos = TransformWorldToHClip(IN.positionOS.xyz);
  252. // This is just like the other in that it is from TerrainLitPasses
  253. output.uvMainAndLM.xy = IN.texcoord;
  254. output.uvSplat01.xy = TRANSFORM_TEX(IN.texcoord, _Splat0);
  255. return output;
  256. }
  257. half4 Frag(Varyings IN) : SV_Target
  258. {
  259. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  260. half4 splatControl = float4(1, 0, 0, 0);
  261. half weight;
  262. half4 mixedDiffuse;
  263. half4 defaultSmoothness;
  264. half metallic;
  265. half occlusion;
  266. half smoothness;
  267. float2 uv[_LAYER_COUNT];
  268. float4 mask[_LAYER_COUNT];
  269. float4 inUV[_LAYER_COUNT/2];
  270. inUV[0] = IN.uvSplat01;
  271. #ifndef _LAYERS_TWO
  272. inUV[1] = IN.uvSplat23;
  273. #ifdef _LAYERS_EIGHT
  274. inUV[2] = IN.uvSplat45;
  275. inUV[3] = IN.uvSplat67;
  276. #endif
  277. #endif
  278. uvArray(uv, inUV);
  279. SplatmapMix(IN.uvMainAndLM, uv, float3(0, 0, 0), float3(0, 0, 0), IN.positionWS, weight, mixedDiffuse, smoothness, metallic, occlusion, normalTS);
  280. mask[0] = Mask(0);
  281. mask[0] = RemapMask(0);
  282. mask[0] *= weight;
  283. #ifdef _TERRAIN_NORMAL_IN_MASK
  284. occlusion = mask[0].r;
  285. #else
  286. occlusion = mask[0].g;
  287. #endif
  288. #ifndef _TERRAIN_MASK_MAPS
  289. metallic = _Metallic0;
  290. #else
  291. metallic = mask[0].r;
  292. #endif
  293. return float4(metallic, occlusion, 0, SAMPLE_TEXTURE2D(_Splat0, sampler_Splat0, uv[0]).r * 1e-5f);
  294. }
  295. ENDHLSL
  296. }
  297. }
  298. }