InTerra_SplatMapControl.hlsl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. float2 uvSplat[_LAYER_COUNT];
  2. float4 mask[_LAYER_COUNT];
  3. #ifdef TRIPLANAR
  4. float2 uvSplat_front[_LAYER_COUNT], uvSplat_side[_LAYER_COUNT];
  5. float4 mask_front[_LAYER_COUNT], mask_side[_LAYER_COUNT];
  6. #endif
  7. #ifdef _TERRAIN_DISTANCEBLEND
  8. float2 distantUV[_LAYER_COUNT];
  9. float4 dMask[_LAYER_COUNT];
  10. #ifdef TRIPLANAR
  11. float2 distantUV_front[_LAYER_COUNT], distantUV_side[_LAYER_COUNT];
  12. float4 dMask_front[_LAYER_COUNT], dMask_side[_LAYER_COUNT];
  13. #endif
  14. #endif
  15. #ifdef _TRACKS
  16. float4 trackSplats[_LAYER_COUNT];
  17. float4 trackSplatsColor[_LAYER_COUNT];
  18. #endif
  19. //-------------------- MIP MAP LOD -------------------------
  20. #if defined(PARALLAX) || defined(TESSELLATION_ON)
  21. float lod = smoothstep(_MipMapFade.x, _MipMapFade.y, (distance(worldPos, _WorldSpaceCameraPos)));
  22. #endif
  23. //====================================================================================
  24. //--------------------------------- SPLAT MAP CONTROL --------------------------------
  25. //====================================================================================
  26. float4 blendMask[4];
  27. blendMask[0] = 0;
  28. blendMask[1] = 0;
  29. blendMask[2] = 0;
  30. blendMask[3] = 0;
  31. float4 trackDepth = 0;
  32. half4 mixedMask = 0;
  33. half4 dMixedMask = 0;
  34. #ifndef TESSELLATION_SAMPLING
  35. occlusion = mixedMask.g;
  36. metallic = mixedMask.r;
  37. #endif
  38. float halfTrackArea = _InTerra_TrackArea * 0.5f;
  39. float _InTerra_TrackFading = 28;
  40. float2 trackUV = float2((worldPos.x - _InTerra_TrackPosition.x) + (halfTrackArea), -(worldPos.z - _InTerra_TrackPosition.z - (halfTrackArea))) * 1.0f / _InTerra_TrackArea;
  41. float trackDist = smoothstep(_InTerra_TrackArea - 1.0f, _InTerra_TrackArea - _InTerra_TrackFading, (distance(worldPos, float3(_WorldSpaceCameraPos.x, _WorldSpaceCameraPos.y, _WorldSpaceCameraPos.z))));
  42. float2 minDist = step(float2(0.0f, 0.0f), trackUV);
  43. float2 maxDist = step(float2(0.0f, 0.0f), 1.0 - trackUV);
  44. trackDist *= (minDist.x * minDist.y * maxDist.x * maxDist.y);
  45. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  46. float2 splatBaseUV = (worldPos.xz - _TerrainPosition.xz) * (1 / _TerrainSize.xz);
  47. #ifndef _LAYERS_ONE
  48. float2 splatMapUV = (splatBaseUV * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  49. #ifndef TESSELLATION_SAMPLING
  50. blendMask[0] = SAMPLE_TEXTURE2D(_Control, sampler_Control, splatMapUV);
  51. #else
  52. blendMask[0] = SAMPLE_TEXTURE2D_LOD(_Control, sampler_Control, splatMapUV, 0);
  53. #endif
  54. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  55. #ifndef TESSELLATION_SAMPLING
  56. blendMask[1] = SAMPLE_TEXTURE2D(_Control1, sampler_Control, splatMapUV);
  57. #ifdef _LAYERS_SIXTEEN
  58. blendMask[2] = SAMPLE_TEXTURE2D(_Control2, sampler_Control, splatMapUV);
  59. blendMask[3] = SAMPLE_TEXTURE2D(_Control3, sampler_Control, splatMapUV);
  60. #endif
  61. #else
  62. blendMask[1] = SAMPLE_TEXTURE2D_LOD(_Control1, sampler_Control, splatMapUV, 0);
  63. #ifdef _LAYERS_SIXTEEN
  64. blendMask[2] = SAMPLE_TEXTURE2D_LOD(_Control2, sampler_Control, splatMapUV, 0);
  65. blendMask[3] = SAMPLE_TEXTURE2D_LOD(_Control3, sampler_Control, splatMapUV, 0);
  66. #endif
  67. #endif
  68. #endif
  69. #else
  70. blendMask[0] = float4(1, 0, 0, 0);
  71. blendMask[1] = float4(0, 0, 0, 0);
  72. blendMask[2] = float4(0, 0, 0, 0);
  73. blendMask[3] = float4(0, 0, 0, 0);
  74. #endif
  75. #else
  76. float2 blendUV0 = (splatBaseUV.xy * (_Control0_TexelSize.zw - 1.0f) + 0.5f) * _Control0_TexelSize.xy;
  77. #ifndef TESSELLATION_SAMPLING
  78. blendMask[0] = SAMPLE_TEXTURE2D(_Control0, sampler_Control0, blendUV0);
  79. #else
  80. blendMask[0] = SAMPLE_TEXTURE2D_LOD(_Control0, sampler_Control0, blendUV0, 0);
  81. #endif
  82. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  83. #ifndef TESSELLATION_SAMPLING
  84. blendMask[1] = SAMPLE_TEXTURE2D(_Control1, sampler_Control0, blendUV0);
  85. #ifdef _LAYERS_SIXTEEN
  86. blendMask[2] = SAMPLE_TEXTURE2D(_Control2, sampler_Control0, blendUV0);
  87. blendMask[3] = SAMPLE_TEXTURE2D(_Control3, sampler_Control0, blendUV0);
  88. #endif
  89. #else
  90. blendMask[1] = SAMPLE_TEXTURE2D_LOD(_Control1, sampler_Control0, blendUV0, 0);
  91. #ifdef _LAYERS_SIXTEEN
  92. blendMask[2] = SAMPLE_TEXTURE2D_LOD(_Control2, sampler_Control0, blendUV0, 0);
  93. blendMask[3] = SAMPLE_TEXTURE2D_LOD(_Control3, sampler_Control0, blendUV0, 0);
  94. #endif
  95. #endif
  96. #endif
  97. #endif
  98. #if defined(_TERRAIN_BLEND_HEIGHT)
  99. blendMask[0] += 0.00001; //to prevent nan when heightblending
  100. #endif
  101. float2 tintUV = splatBaseUV * _TerrainColorTintTexture_ST.xy + _TerrainColorTintTexture_ST.zw;
  102. float2 normalTintUV = splatBaseUV * _TerrainNormalTintTexture_ST.xy + _TerrainNormalTintTexture_ST.zw;
  103. float3 flipUV = worldNormal.rgb < 0 ? -1 : 1;
  104. float3 triplanarWeights = abs(worldNormal.rgb);
  105. triplanarWeights = pow(triplanarWeights, _TriplanarSharpness);
  106. triplanarWeights = triplanarWeights / (triplanarWeights.x + triplanarWeights.y + triplanarWeights.z);
  107. #ifdef INTERRA_OBJECT
  108. TriplanarOneToAllSteep(blendMask, (1 - terrainNormals.w));
  109. #else
  110. TriplanarOneToAllSteep(blendMask, (1 - triplanarWeights.y));
  111. #endif
  112. #if defined(_LAYERS_TWO) && defined(INTERRA_OBJECT)
  113. blendMask[0].r = _ControlNumber == 0 ? blendMask[0].r : _ControlNumber == 1 ? blendMask[0].g : _ControlNumber == 2 ? blendMask[0].b : blendMask[0].a;
  114. blendMask[0].g = 1 - blendMask[0].r;
  115. #endif
  116. #if defined(_TERRAIN_BASEMAP_GEN_TRIPLANAR) || defined(_LAYERS_ONE)
  117. blendMask[0] = float4(1, 0, 0, 0);
  118. blendMask[1] = float4(0, 0, 0, 0);
  119. blendMask[2] = float4(0, 0, 0, 0);
  120. blendMask[3] = float4(0, 0, 0, 0);
  121. #endif
  122. #ifdef _TRACKS
  123. float4 tBlendMask[4];
  124. tBlendMask[0] = blendMask[0];
  125. tBlendMask[1] = blendMask[1];
  126. tBlendMask[2] = blendMask[2];
  127. tBlendMask[3] = blendMask[3];
  128. #endif
  129. float4 origBlendMask[4] = blendMask;
  130. #if defined(_TERRAIN_DISTANCEBLEND)
  131. float4 dBlendMask[4];
  132. float4 dOrigBlendMask[4] = blendMask;
  133. dBlendMask[0] = blendMask[0];
  134. dBlendMask[1] = blendMask[1];
  135. dBlendMask[2] = blendMask[2];
  136. dBlendMask[3] = blendMask[3];
  137. float dist = smoothstep(_HT_distance.x, _HT_distance.y, (distance(worldPos, _WorldSpaceCameraPos)));
  138. float sampleDistMask = dist < 0.001f ? 1 : 0;
  139. if(sampleDistMask)
  140. {
  141. dBlendMask[0] *= sampleDistMask;
  142. #ifdef _LAYERS_EIGHT
  143. dBlendMask[1] *= sampleDistMask;
  144. #endif
  145. }
  146. #endif
  147. float heightSum = 0.5f;
  148. float dHeightSum = 0.5f;
  149. //================================================================================
  150. //-------------------------------------- UVs -------------------------------------
  151. //================================================================================
  152. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  153. float3 positionOffset = _WorldMapping ? worldPos : (worldPos - _TerrainPosition);
  154. #ifndef TRIPLANAR
  155. float distortion = 0;
  156. #if defined(INTERRA_OBJECT) && !defined(TESSELLATION_SAMPLING)
  157. distortion = worldNormal.y > 0.5 ? 0 : (1 - worldNormal.y) * _SteepDistortion * objectAlbedo.r;
  158. #endif
  159. UvSplat(uvSplat, positionOffset, distortion);
  160. #else
  161. float offsetZ = -flipUV.z * worldPos.y;
  162. float offsetX = -flipUV.x * worldPos.y;
  163. #if defined(INTERRA_OBJECT)
  164. offsetZ = _DisableOffsetY == 1 ? -flipUV.z * worldPos.y : heightOffset * -flipUV.z + (worldPos.z);
  165. offsetX = _DisableOffsetY == 1 ? -flipUV.x * worldPos.y : heightOffset * -flipUV.x + (worldPos.x);
  166. #endif
  167. offsetZ -= _TerrainPosition.z;
  168. offsetX -= _TerrainPosition.x;
  169. UvSplat(uvSplat, uvSplat_front, uvSplat_side, positionOffset, offsetZ, offsetX, flipUV);
  170. #endif
  171. #else
  172. #ifdef _TERRAIN_BASEMAP_GEN
  173. float2 uv = splatBaseUV.xy;
  174. #else
  175. float2 uv = _WorldMapping ? (worldPos.xz / _TerrainSizeXZPosY.xy) : splatBaseUV.xy;
  176. #endif
  177. #ifndef TRIPLANAR
  178. UvSplat(uvSplat, uv);
  179. #else
  180. UvSplat(uvSplat, uvSplat_front, uvSplat_side, worldPos, uv, flipUV);
  181. #endif
  182. #endif
  183. //-------------------- PARALLAX OFFSET -------------------------
  184. #if defined(PARALLAX) && defined(_TERRAIN_PARALLAX) && !defined(TESSELLATION_ON) && !defined(_TERRAIN_BASEMAP_GEN)
  185. if (_Terrain_Parallax == 1)
  186. {
  187. ParallaxUV(uvSplat, tangentViewDirTerrain, lod);
  188. }
  189. #endif
  190. //--------------------- DISTANCE UV ------------------------
  191. #ifdef _TERRAIN_DISTANCEBLEND
  192. DistantUV(distantUV, uvSplat);
  193. #ifdef TRIPLANAR
  194. #ifdef _TERRAIN_TRIPLANAR_ONE
  195. distantUV_front[0] = uvSplat_front[0] * (_DiffuseRemapOffset0.r + 1) * _HT_distance_scale;
  196. distantUV_side[0] = uvSplat_side[0] * (_DiffuseRemapOffset0.r + 1) * _HT_distance_scale;
  197. #else
  198. DistantUV(distantUV_front, uvSplat_front);
  199. DistantUV(distantUV_side, uvSplat_side);
  200. #endif
  201. #endif
  202. #endif