InTerra_BaseGen.shader 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. Shader "Hidden/InTerra/InTerra-BaseGen"
  2. {
  3. Properties
  4. {
  5. [HideInInspector] _DstBlend("DstBlend", Float) = 0.0
  6. [HideInInspector] _HT_distance_scale("Scale", Range(0,0.55)) = 0.2
  7. [HideInInspector] _HT_cover("Cover strenght", Range(0,1)) = 0.6
  8. [HideInInspector] _HeightmapBlending("", Float) = 1
  9. }
  10. SubShader
  11. {
  12. CGINCLUDE
  13. #include "UnityCG.cginc"
  14. #pragma multi_compile_local __ _TERRAIN_TRIPLANAR_ONE
  15. #pragma multi_compile_local __ _TERRAIN_DISTANCEBLEND
  16. #define INTERRA_TERRAIN
  17. #define TERRAIN_BASEGEN
  18. #include "InTerra_DefinedGlobalKeywords.cginc"
  19. #include "InTerra_InputsAndFunctions.cginc"
  20. struct appdata_t
  21. {
  22. float4 vertex : POSITION;
  23. float2 texcoord : TEXCOORD0;
  24. };
  25. float2 ComputeControlUV(float2 uv)
  26. {
  27. // adjust splatUVs so the edges of the terrain tile lie on pixel centers
  28. return (uv * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  29. }
  30. ENDCG
  31. Pass
  32. {
  33. Tags
  34. {
  35. "Name" = "_MainTex"
  36. "Format" = "RGBA32"
  37. "Size" = "1"
  38. }
  39. ZTest Always Cull Off ZWrite Off
  40. Blend One[_DstBlend]
  41. CGPROGRAM
  42. #pragma vertex vert
  43. #pragma fragment frag
  44. struct v2f
  45. {
  46. float4 vertex : SV_POSITION;
  47. float2 texcoord0 : TEXCOORD0;
  48. float2 texcoord1 : TEXCOORD1;
  49. float2 texcoord2 : TEXCOORD2;
  50. #ifndef _LAYERS_TWO
  51. float2 texcoord3 : TEXCOORD3;
  52. float2 texcoord4 : TEXCOORD4;
  53. #endif
  54. float2 texcoord5 : TEXCOORD5;
  55. };
  56. v2f vert(appdata_t v)
  57. {
  58. v2f o;
  59. o.vertex = UnityObjectToClipPos(v.vertex);
  60. float2 uv = TRANSFORM_TEX(v.texcoord, _Control);
  61. o.texcoord0 = ComputeControlUV(uv);
  62. o.texcoord1 = TRANSFORM_TEX(uv, _Splat0);
  63. o.texcoord2 = TRANSFORM_TEX(uv, _Splat1);
  64. #ifndef _LAYERS_TWO
  65. o.texcoord3 = TRANSFORM_TEX(uv, _Splat2);
  66. o.texcoord4 = TRANSFORM_TEX(uv, _Splat3);
  67. #endif
  68. o.texcoord5 = uv;
  69. return o;
  70. }
  71. float4 frag(v2f i) : SV_Target
  72. {
  73. half4 mixedDiffuse = 1;
  74. float3 mixedNormal = float3(0, 0, 1);
  75. float2 uvSplat[_LAYER_COUNT];
  76. half4 splat[_LAYER_COUNT], mask[_LAYER_COUNT];
  77. half4 blendMask[2];
  78. blendMask[0] = UNITY_SAMPLE_TEX2D(_Control, i.texcoord0);
  79. blendMask[1] = UNITY_SAMPLE_TEX2D_SAMPLER(_Control1, _Control, i.texcoord0);
  80. UvSplat(uvSplat, i.texcoord0);
  81. #ifdef _TERRAIN_DISTANCEBLEND
  82. float4 distantDiffuse = 1;
  83. float3 distantNormal = float3(0, 0, 1);
  84. half4 dBlendMask[2];
  85. dBlendMask[0] = blendMask[0];
  86. dBlendMask[1] = blendMask[1];
  87. float2 distantUV[_LAYER_COUNT];
  88. half4 dSplat[_LAYER_COUNT], dMask[_LAYER_COUNT];
  89. DistantUV(distantUV, uvSplat);
  90. SampleMask(dMask, distantUV, dBlendMask, 1.0f);
  91. #endif
  92. SampleMask(mask, uvSplat, blendMask, 1.0f);
  93. //---------- HEIGHT MAP SPLAT BLENDINGS ---------
  94. #if defined(_TERRAIN_BLEND_HEIGHT)
  95. if (_NumLayersCount <= 4)
  96. {
  97. if (_HeightmapBlending == 1)
  98. {
  99. HeightBlend(mask, blendMask, _HeightTransition);
  100. #ifdef _TERRAIN_DISTANCEBLEND
  101. HeightBlend(dMask, dBlendMask, _Distance_HeightTransition);
  102. #endif
  103. }
  104. }
  105. #endif
  106. SampleSplat(uvSplat, blendMask, 1.0f, mask, mixedDiffuse, mixedNormal, splat);
  107. #ifdef _TERRAIN_DISTANCEBLEND
  108. SampleSplat(distantUV, dBlendMask, 1.0f, dMask, distantDiffuse, distantNormal, dSplat);
  109. mixedDiffuse = lerp(mixedDiffuse, distantDiffuse, _HT_cover);
  110. #endif
  111. #ifndef _TERRAIN_TRIPLANAR_ONE
  112. half3 tint = tex2D(_TerrainColorTintTexture, i.texcoord5 * _TerrainColorTintTexture_ST.xy + _TerrainColorTintTexture_ST.zw);
  113. mixedDiffuse.rgb = lerp(mixedDiffuse.rgb, ((mixedDiffuse.rgb) * (tint)), _TerrainColorTintStrenght).rgb;
  114. #endif
  115. return mixedDiffuse;
  116. }
  117. ENDCG
  118. }
  119. Pass
  120. {
  121. // _NormalMap pass will get ignored by terrain basemap generation code. Put here so that the VTC can use it to generate cache for normal maps.
  122. Tags
  123. {
  124. "Name" = "_NormalMap"
  125. "Format" = "A2R10G10B10"
  126. "Size" = "1"
  127. }
  128. ZTest Always Cull Off ZWrite Off
  129. Blend One[_DstBlend]
  130. CGPROGRAM
  131. #pragma vertex vert
  132. #pragma fragment frag
  133. struct v2f
  134. {
  135. float4 vertex : SV_POSITION;
  136. float2 texcoord0 : TEXCOORD0;
  137. float2 texcoord1 : TEXCOORD1;
  138. float2 texcoord2 : TEXCOORD2;
  139. #ifndef _LAYERS_TWO
  140. float2 texcoord3 : TEXCOORD3;
  141. float2 texcoord4 : TEXCOORD4;
  142. #endif
  143. };
  144. v2f vert(appdata_t v)
  145. {
  146. v2f o;
  147. o.vertex = UnityObjectToClipPos(v.vertex);
  148. float2 uv = TRANSFORM_TEX(v.texcoord, _Control);
  149. o.texcoord0 = ComputeControlUV(uv);
  150. o.texcoord1 = TRANSFORM_TEX(uv, _Splat0);
  151. o.texcoord2 = TRANSFORM_TEX(uv, _Splat1);
  152. #ifndef _LAYERS_TWO
  153. o.texcoord3 = TRANSFORM_TEX(uv, _Splat2);
  154. o.texcoord4 = TRANSFORM_TEX(uv, _Splat3);
  155. #endif
  156. return o;
  157. }
  158. float4 frag(v2f i) : SV_Target
  159. {
  160. float4 alpha = UNITY_SAMPLE_TEX2D(_Control, i.texcoord0);
  161. float3 normal = float3(0,0,1);
  162. return float4(normal.xyz * 0.5f + 0.5f, 1.0f);
  163. }
  164. ENDCG
  165. }
  166. Pass
  167. {
  168. Tags
  169. {
  170. "Name" = "_MetallicTex"
  171. "Format" = "RGBA32"
  172. "Size" = "1/4"
  173. }
  174. ZTest Always Cull Off ZWrite Off
  175. Blend One[_DstBlend]
  176. CGPROGRAM
  177. #pragma vertex vert
  178. #pragma fragment frag
  179. struct v2f
  180. {
  181. float4 vertex : SV_POSITION;
  182. float2 texcoord0 : TEXCOORD0;
  183. float2 texcoord1 : TEXCOORD1;
  184. float2 texcoord2 : TEXCOORD2;
  185. #ifndef _LAYERS_TWO
  186. float2 texcoord3 : TEXCOORD3;
  187. float2 texcoord4 : TEXCOORD4;
  188. #endif
  189. };
  190. v2f vert(appdata_t v)
  191. {
  192. v2f o;
  193. o.vertex = UnityObjectToClipPos(v.vertex);
  194. float2 uv = TRANSFORM_TEX(v.texcoord, _Control);
  195. o.texcoord0 = ComputeControlUV(TRANSFORM_TEX(v.texcoord, _Control));
  196. o.texcoord1 = TRANSFORM_TEX(uv, _Splat0);
  197. o.texcoord2 = TRANSFORM_TEX(uv, _Splat1);
  198. #ifndef _LAYERS_TWO
  199. o.texcoord3 = TRANSFORM_TEX(uv, _Splat2);
  200. o.texcoord4 = TRANSFORM_TEX(uv, _Splat3);
  201. #endif
  202. return o;
  203. }
  204. float4 frag(v2f i) : SV_Target
  205. {
  206. half metallic = 0;
  207. half ao = 1;
  208. half4 mixedDiffuse = 1;
  209. float3 mixedNormal = float3(0, 0, 1);
  210. float2 uvSplat[_LAYER_COUNT];
  211. half4 splat[_LAYER_COUNT], mask[_LAYER_COUNT];
  212. half4 blendMask[2];
  213. blendMask[0] = UNITY_SAMPLE_TEX2D(_Control, i.texcoord0);
  214. blendMask[0].g = (1 - blendMask[0].r);
  215. blendMask[1] = half4(0, 0, 0, 0);
  216. UvSplat(uvSplat, i.texcoord0);
  217. SampleMask(mask, uvSplat, blendMask, 1.0f);
  218. #ifdef _TERRAIN_DISTANCEBLEND
  219. float4 distantDiffuse = 1;
  220. float3 distantNormal = float3(0, 0, 1);
  221. half4 dBlendMask[2];
  222. dBlendMask[0] = blendMask[0];
  223. dBlendMask[1] = blendMask[1];
  224. float2 distantUV[_LAYER_COUNT];
  225. half4 dSplat[_LAYER_COUNT], dMask[_LAYER_COUNT];
  226. DistantUV(distantUV, uvSplat);
  227. SampleMask(dMask, distantUV, dBlendMask, 1.0f);
  228. #endif
  229. //-------- AMBIENT OCCLUSION --------
  230. ao = 1;
  231. #if defined(_TERRAIN_MASK_MAPS) || defined(_TERRAIN_NORMAL_IN_MASK)
  232. ao = AmbientOcclusion(mask, blendMask);
  233. #if defined (_TERRAIN_DISTANCEBLEND)
  234. float dAo = AmbientOcclusion(dMask, dBlendMask);
  235. ao = lerp(ao, dAo, _HT_cover);
  236. #endif
  237. #endif
  238. //---------- METALLIC ---------------
  239. metallic = MetallicMask(mask, blendMask);
  240. #if defined (_TERRAIN_DISTANCEBLEND)
  241. float dMetallic = MetallicMask(dMask, dBlendMask);
  242. metallic = lerp(metallic, dMetallic, _HT_cover);
  243. #endif
  244. return float4(metallic, ao, blendMask[0].r, UNITY_SAMPLE_TEX2D(_Splat0, float2(0,0)).r);
  245. }
  246. ENDCG
  247. }
  248. Pass
  249. {
  250. Tags
  251. {
  252. "Name" = "_TriplanarTex"
  253. "Format" = "RGBA32"
  254. "Size" = "1"
  255. }
  256. ZTest Always Cull Off ZWrite Off
  257. Blend One[_DstBlend]
  258. CGPROGRAM
  259. #pragma vertex vert
  260. #pragma fragment frag
  261. struct v2f
  262. {
  263. float4 vertex : SV_POSITION;
  264. float2 texcoord0 : TEXCOORD0;
  265. };
  266. v2f vert(appdata_t v)
  267. {
  268. v2f o;
  269. o.vertex = UnityObjectToClipPos(v.vertex);
  270. float2 uv = TRANSFORM_TEX(v.texcoord, _Control);
  271. o.texcoord0 = TRANSFORM_TEX(uv, _Splat0);
  272. return o;
  273. }
  274. float4 frag(v2f i) : SV_Target
  275. {
  276. float2 uv = i.texcoord0;
  277. float4 splat0 = UNITY_SAMPLE_TEX2D(_Splat0, uv);
  278. float4 mask0 = UNITY_SAMPLE_TEX2D(_Mask0, uv);
  279. #ifdef _TERRAIN_DISTANCEBLEND
  280. float4 dSplat0 = UNITY_SAMPLE_TEX2D(_Splat0, uv * (_DiffuseRemapOffset0.r + 1) * _HT_distance_scale);
  281. splat0 = lerp(splat0, dSplat0, _HT_cover);
  282. float4 dMask0 = UNITY_SAMPLE_TEX2D(_Mask0, uv * (_DiffuseRemapOffset0.r + 1) * _HT_distance_scale);
  283. mask0 = lerp(mask0, dMask0, _HT_cover);
  284. #endif
  285. #if defined(_TERRAIN_MASK_MAPS) && !defined(_TERRAIN_NORMAL_IN_MASK)
  286. #ifdef _TERRAIN_NORMAL_IN_MASK
  287. mask0.rb = mask0.rb * _MaskMapRemapScale0.gb + _MaskMapRemapOffset0.gb;
  288. #else
  289. mask0.rgba = mask0.rgba * _MaskMapRemapScale0.rgba + _MaskMapRemapOffset0.rgba;
  290. #endif
  291. splat0.a = mask0.a;
  292. #else
  293. splat0.a = splat0.a * _Smoothness0;
  294. #endif
  295. return splat0;
  296. }
  297. ENDCG
  298. }
  299. Pass
  300. {
  301. Tags
  302. {
  303. "Name" = "_Triplanar_MetallicAO"
  304. "Format" = "RGBA32"
  305. "Size" = "1/4"
  306. }
  307. ZTest Always Cull Off ZWrite Off
  308. Blend One[_DstBlend]
  309. CGPROGRAM
  310. #pragma vertex vert
  311. #pragma fragment frag
  312. struct v2f
  313. {
  314. float4 vertex : SV_POSITION;
  315. float2 texcoord0 : TEXCOORD0;
  316. };
  317. v2f vert(appdata_t v)
  318. {
  319. v2f o;
  320. o.vertex = UnityObjectToClipPos(v.vertex);
  321. float2 uv = TRANSFORM_TEX(v.texcoord, _Control);
  322. o.texcoord0 = TRANSFORM_TEX(uv, _Splat0);
  323. return o;
  324. }
  325. float4 frag(v2f i) : SV_Target
  326. {
  327. float2 uv = i.texcoord0;
  328. float4 mask0 = UNITY_SAMPLE_TEX2D(_Mask0, uv);
  329. half ao = 1;
  330. half metallic = _Metallic0;
  331. #if defined(_TERRAIN_MASK_MAPS) || defined(_TERRAIN_NORMAL_IN_MASK)
  332. #ifdef _TERRAIN_DISTANCEBLEND
  333. float4 dMask0 = UNITY_SAMPLE_TEX2D(_Mask0, uv * _HT_distance_scale);
  334. mask0 = lerp(mask0, dMask0, _HT_cover);
  335. #endif
  336. #ifdef _TERRAIN_NORMAL_IN_MASK
  337. ao = mask0.r * _MaskMapRemapScale0.g + _MaskMapRemapOffset0.g;
  338. #else
  339. metallic = mask0.r * _MaskMapRemapScale0.r + _MaskMapRemapOffset0.r;
  340. ao = mask0.g * _MaskMapRemapScale0.g + _MaskMapRemapOffset0.g;
  341. #endif
  342. #endif
  343. return float4(metallic, ao, 0, 0);
  344. }
  345. ENDCG
  346. }
  347. }
  348. Fallback Off
  349. }