InTerra_DiffuseBaseGen.shader 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. Shader "Hidden/InTerra/InTerra-Diffuse-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. }
  9. SubShader
  10. {
  11. CGINCLUDE
  12. #include "UnityCG.cginc"
  13. #pragma multi_compile_local __ _TERRAIN_TRIPLANAR_ONE _TERRAIN_TRIPLANAR
  14. #pragma multi_compile_local __ _TERRAIN_DISTANCEBLEND
  15. #define _TERRAIN_TINT_TEXTURE
  16. #define INTERRA_TERRAIN
  17. #define DIFFUSE
  18. #define TERRAIN_BASEGEN
  19. #include "InTerra_DefinedGlobalKeywords.cginc"
  20. #include "InTerra_InputsAndFunctions.cginc"
  21. struct appdata_t
  22. {
  23. float4 vertex : POSITION;
  24. float2 texcoord : TEXCOORD0;
  25. };
  26. float2 ComputeControlUV(float2 uv)
  27. {
  28. // adjust splatUVs so the edges of the terrain tile lie on pixel centers
  29. return (uv * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  30. }
  31. ENDCG
  32. Pass
  33. {
  34. Tags
  35. {
  36. "Name" = "_MainTex"
  37. "Format" = "RGBA32"
  38. "Size" = "1"
  39. }
  40. ZTest Always Cull Off ZWrite Off
  41. Blend One[_DstBlend]
  42. CGPROGRAM
  43. #pragma vertex vert
  44. #pragma fragment frag
  45. struct v2f
  46. {
  47. float4 vertex : SV_POSITION;
  48. float2 texcoord0 : TEXCOORD0;
  49. float2 texcoord1 : TEXCOORD1;
  50. float2 texcoord2 : TEXCOORD2;
  51. #ifndef _LAYERS_TWO
  52. float2 texcoord3 : TEXCOORD3;
  53. float2 texcoord4 : TEXCOORD4;
  54. #endif
  55. float2 texcoord5 : TEXCOORD5;
  56. };
  57. v2f vert(appdata_t v)
  58. {
  59. v2f o;
  60. o.vertex = UnityObjectToClipPos(v.vertex);
  61. float2 uv = TRANSFORM_TEX(v.texcoord, _Control);
  62. o.texcoord0 = ComputeControlUV(uv);
  63. o.texcoord1 = TRANSFORM_TEX(uv, _Splat0);
  64. o.texcoord2 = TRANSFORM_TEX(uv, _Splat1);
  65. #ifndef _LAYERS_TWO
  66. o.texcoord3 = TRANSFORM_TEX(uv, _Splat2);
  67. o.texcoord4 = TRANSFORM_TEX(uv, _Splat3);
  68. #endif
  69. o.texcoord5 = uv;
  70. return o;
  71. }
  72. float4 frag(v2f i) : SV_Target
  73. {
  74. half4 mixedDiffuse = 1;
  75. float3 mixedNormal = float3(0, 0, 1);
  76. float2 uvSplat[_LAYER_COUNT];
  77. half4 splat[_LAYER_COUNT], mask[_LAYER_COUNT];
  78. half4 blendMask[2];
  79. blendMask[0] = UNITY_SAMPLE_TEX2D(_Control, i.texcoord0);
  80. blendMask[1] = UNITY_SAMPLE_TEX2D_SAMPLER(_Control1, _Control, i.texcoord0);
  81. UvSplat(uvSplat, i.texcoord0);
  82. #ifdef _TERRAIN_DISTANCEBLEND
  83. float4 distantDiffuse = 1;
  84. float3 distantNormal = float3(0, 0, 1);
  85. half4 dBlendMask[2];
  86. dBlendMask[0] = blendMask[0];
  87. dBlendMask[1] = blendMask[1];
  88. float2 distantUV[_LAYER_COUNT];
  89. half4 dSplat[_LAYER_COUNT], dMask[_LAYER_COUNT];
  90. DistantUV(distantUV, uvSplat);
  91. SampleMask(dMask, distantUV, dBlendMask, 1.0f);
  92. #endif
  93. SampleMask(mask, uvSplat, blendMask, 1.0f);
  94. //---------- HEIGHT MAP SPLAT BLENDINGS ---------
  95. #if defined(_TERRAIN_BLEND_HEIGHT)
  96. if (_NumLayersCount <= 4)
  97. {
  98. if (_HeightmapBlending == 1)
  99. {
  100. HeightBlend(mask, blendMask, _HeightTransition);
  101. #ifdef _TERRAIN_DISTANCEBLEND
  102. HeightBlend(dMask, dBlendMask, _Distance_HeightTransition);
  103. #endif
  104. }
  105. }
  106. #endif
  107. SampleSplat(uvSplat, blendMask, 1.0f, mask, mixedDiffuse, mixedNormal, splat);
  108. #ifdef _TERRAIN_DISTANCEBLEND
  109. SampleSplat(distantUV, dBlendMask, 1.0f, dMask, distantDiffuse, distantNormal, dSplat);
  110. mixedDiffuse = lerp(mixedDiffuse, distantDiffuse, _HT_cover);
  111. #endif
  112. #ifndef _TERRAIN_TRIPLANAR_ONE
  113. half3 tint = tex2D(_TerrainColorTintTexture, i.texcoord5 * _TerrainColorTintTexture_ST.xy + _TerrainColorTintTexture_ST.zw);
  114. mixedDiffuse.rgb = lerp(mixedDiffuse.rgb, ((mixedDiffuse.rgb) * (tint)), _TerrainColorTintStrenght).rgb;
  115. #endif
  116. return mixedDiffuse;
  117. }
  118. ENDCG
  119. }
  120. Pass
  121. {
  122. Tags
  123. {
  124. "Name" = "_TriplanarTex"
  125. "Format" = "RGBA32"
  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. };
  138. v2f vert(appdata_t v)
  139. {
  140. v2f o;
  141. o.vertex = UnityObjectToClipPos(v.vertex);
  142. float2 uv = TRANSFORM_TEX(v.texcoord, _Control);
  143. o.texcoord0 = TRANSFORM_TEX(uv, _Splat0);
  144. return o;
  145. }
  146. float4 frag(v2f i) : SV_Target
  147. {
  148. float2 uv = i.texcoord0;
  149. float4 splat0 = UNITY_SAMPLE_TEX2D(_Splat0, uv);
  150. #ifdef _TERRAIN_DISTANCEBLEND
  151. float4 dSplat0 = UNITY_SAMPLE_TEX2D(_Splat0, uv * (_DiffuseRemapOffset0.r + 1) * _HT_distance_scale);
  152. splat0 = lerp(splat0, dSplat0, _HT_cover);
  153. #endif
  154. return splat0;
  155. }
  156. ENDCG
  157. }
  158. }
  159. Fallback Off
  160. }