InTerra_Tessellation.hlsl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #if (defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)) && !defined(TESSELLATION_ON)
  2. #define TESSELLATION_ON
  3. #endif
  4. #define TESSELLATION_SAMPLING
  5. #undef _TERRAIN_PARALLAX
  6. #if !(defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN))
  7. #include "InTerra_Functions.hlsl"
  8. #endif
  9. #define Height(i, lod) SAMPLE_TEXTURE2D_LOD(_Mask##i, sampler_Mask0, uv[i], (_MipMapLevel + (lod * (log2(max(_Mask##i##_TexelSize.z, _Mask##i##_TexelSize.w)) + 1))));
  10. #define RemapHeight(i) mask[i] * _MaskMapRemapScale##i + _MaskMapRemapOffset##i;
  11. #define TessellationHeight(i) ((mask[i] - _DiffuseRemapOffset##i.y) * DiffuseRemap(i).w * 0.01) + ((_DiffuseRemapOffset##i.z ) - ((DiffuseRemap(i).w * 0.01)/2));
  12. void SampleHeights(out float4 mask[_LAYER_COUNT], float2 uv[_LAYER_COUNT], float4 blendMask[4], float lod)
  13. {
  14. #define SampleHeight(i, blendMask) \
  15. UNITY_BRANCH if (blendMask > 0 && _LayerHasMask##i > 0 ) \
  16. { \
  17. mask[i] = Height(i, lod); \
  18. mask[i] = RemapHeight(i); \
  19. } \
  20. else \
  21. { \
  22. mask[i] = float4(0, 0, 0.5, 0); \
  23. } \
  24. SampleHeight(0, blendMask[0].r);
  25. #ifndef _LAYERS_ONE
  26. SampleHeight(1, blendMask[0].g);
  27. #ifndef _LAYERS_TWO
  28. SampleHeight(2, blendMask[0].b);
  29. SampleHeight(3, blendMask[0].a);
  30. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  31. SampleHeight(4, blendMask[1].r);
  32. SampleHeight(5, blendMask[1].g);
  33. SampleHeight(6, blendMask[1].b);
  34. SampleHeight(7, blendMask[1].a);
  35. #endif
  36. #ifdef _LAYERS_SIXTEEN
  37. SampleHeight(8, blendMask[2].r);
  38. SampleHeight(9, blendMask[2].g);
  39. SampleHeight(10, blendMask[2].b);
  40. SampleHeight(11, blendMask[2].a);
  41. SampleHeight(12, blendMask[3].r);
  42. SampleHeight(13, blendMask[3].g);
  43. SampleHeight(14, blendMask[3].b);
  44. SampleHeight(15, blendMask[3].a);
  45. #endif
  46. #endif
  47. #endif
  48. #undef SampleHeight
  49. }
  50. void SampleHeightTOL(out float4 mask[_LAYER_COUNT], float4 noTriplanarMask[_LAYER_COUNT], float2 uv[_LAYER_COUNT], float lod)
  51. {
  52. mask[0] = Height(0, lod);
  53. mask[1] = noTriplanarMask[1];
  54. #ifndef _LAYERS_TWO
  55. mask[2] = noTriplanarMask[2];
  56. mask[3] = noTriplanarMask[3];
  57. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  58. mask[4] = noTriplanarMask[4];
  59. mask[5] = noTriplanarMask[5];
  60. mask[6] = noTriplanarMask[6];
  61. mask[7] = noTriplanarMask[7];
  62. #endif
  63. #ifdef _LAYERS_SIXTEEN
  64. mask[8] = noTriplanarMask[8];
  65. mask[9] = noTriplanarMask[9];
  66. mask[10] = noTriplanarMask[10];
  67. mask[11] = noTriplanarMask[11];
  68. mask[12] = noTriplanarMask[12];
  69. mask[13] = noTriplanarMask[13];
  70. mask[14] = noTriplanarMask[14];
  71. mask[15] = noTriplanarMask[15];
  72. #endif
  73. #endif
  74. }
  75. #if !(defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN))
  76. void Tessellation(float3 worldPos, float3 worldNormal, float2 splatBaseUV, out float3 displacement)
  77. {
  78. #ifdef ENABLE_TERRAIN_PERPIXEL_NORMAL
  79. float2 terrainNormalMapUV = (splatBaseUV + 0.5f) * _TerrainHeightmapRecipSize.xy;
  80. splatBaseUV *= _TerrainHeightmapRecipSize.zw;
  81. #ifndef TERRAIN_PERPIXEL_NORMAL_OVERRIDE
  82. float3 normalOS = SAMPLE_TEXTURE2D_LOD(_TerrainNormalmapTexture, sampler_TerrainNormalmapTexture, terrainNormalMapUV, 0).rgb * 2 - 1;
  83. worldNormal = mul((float3x3)GetObjectToWorldMatrix(), normalOS);
  84. #endif
  85. #endif
  86. #else
  87. #ifdef INTERRA_OBJECT
  88. void Tessellation_float(float3 worldPos, float3 normal, float4 mUV, float4 terrainNormals, float heightOffset, out float3 displacement, out float tessFactor, out float3 oWorldPos)
  89. {
  90. oWorldPos = worldPos;
  91. float4 worldNormal = float4(normal, 0);
  92. #else
  93. void Tessellation_float(float4 worldNormal, float3 worldPos, out float3 displacement, out float tessFactor)
  94. {
  95. #endif
  96. #endif
  97. #include "InTerra_SplatMapControl.hlsl"
  98. SampleHeights(mask, uvSplat, blendMask, lod);
  99. #if defined(TRIPLANAR) && !defined(TERRAIN_SPLAT_ADDPASS)
  100. #ifdef _TERRAIN_TRIPLANAR_ONE
  101. SampleHeightTOL(mask_front, mask, uvSplat_front, lod);
  102. SampleHeightTOL(mask_side, mask, uvSplat_side, lod);
  103. #else
  104. SampleHeights(mask_front, uvSplat_front, blendMask, lod);
  105. SampleHeights(mask_side, uvSplat_side, blendMask, lod);
  106. #endif
  107. MaskWeight(mask, mask_front, mask_side, blendMask, triplanarWeights, _HeightTransition);
  108. #endif
  109. #ifdef _TERRAIN_BLEND_HEIGHT
  110. #if !defined(_LAYERS_ONE)
  111. HeightBlend(mask, blendMask, _Tessellation_HeightTransition);
  112. #endif
  113. #endif
  114. heightSum = HeightSum(mask, blendMask);
  115. float2 puddlesHeight = float2(0.0f, 1.0f);
  116. float horizontalWeight = 0.0f;
  117. float3 ripNormal = float3(0, 0, 1);
  118. puddlesHeight = (1 / (1 * pow(2, float2(_InTerra_GlobalPuddles.x, heightSum) * (-(100)))) + 1) * 0.5;
  119. puddlesHeight /= (puddlesHeight.r + puddlesHeight.g);
  120. horizontalWeight = smoothstep(0.999999f, 1.0f, saturate(triplanarWeights.y - ((triplanarWeights.x + triplanarWeights.z) * 100000000)));
  121. puddlesHeight.r *= horizontalWeight;
  122. #define TessllationHeight(i, blendMask) \
  123. UNITY_BRANCH if (blendMask > 0 && _LayerHasMask##i > 0 ) \
  124. { \
  125. mask[i] = ( puddlesHeight.r > mask[i]) ? ( _InTerra_GlobalPuddles.x ) : mask[i];\
  126. mask[i] = TessellationHeight(i); \
  127. \
  128. } \
  129. else \
  130. { \
  131. mask[i] = float4(0, 0, _DiffuseRemapOffset##i.z, 0); \
  132. } \
  133. TessllationHeight(0, blendMask[0].r);
  134. #ifndef _LAYERS_ONE
  135. TessllationHeight(1, blendMask[0].g);
  136. #ifndef _LAYERS_TWO
  137. TessllationHeight(2, blendMask[0].b);
  138. TessllationHeight(3, blendMask[0].a);
  139. #if defined(_LAYERS_EIGHT) || defined(_LAYERS_SIXTEEN)
  140. TessllationHeight(4, blendMask[1].r);
  141. TessllationHeight(5, blendMask[1].g);
  142. TessllationHeight(6, blendMask[1].b);
  143. TessllationHeight(7, blendMask[1].a);
  144. #endif
  145. #ifdef _LAYERS_SIXTEEN
  146. TessllationHeight(8, blendMask[2].r);
  147. TessllationHeight(9, blendMask[2].g);
  148. TessllationHeight(10, blendMask[2].b);
  149. TessllationHeight(11, blendMask[2].a);
  150. TessllationHeight(12, blendMask[3].r);
  151. TessllationHeight(13, blendMask[3].g);
  152. TessllationHeight(14, blendMask[3].b);
  153. TessllationHeight(15, blendMask[3].a);
  154. #endif
  155. #endif
  156. #endif
  157. #undef TessllationHeight
  158. float terrainHeightSum = HeightSum(mask, blendMask);
  159. #ifdef _TRACKS
  160. if (_Tracks == 1)
  161. {
  162. UnpackTrackSplatValues(trackSplats);
  163. trackDepth = SAMPLE_TEXTURE2D_LOD(_InTerra_TrackTexture, SamplerState_Linear_Repeat, trackUV, 0).r;
  164. float4 td = TrackSplatValues(blendMask, trackSplats) * trackDist;
  165. float2 trackIntersect = float2(trackDepth.b, 1 - trackDepth.b);
  166. trackIntersect = (1 / (pow(2, float2(trackDepth.b, heightSum) * (-(_TrackTessallationHeightTransition)))) + 1) * 0.5;
  167. trackIntersect /= (trackIntersect.r + trackIntersect.g);
  168. terrainHeightSum = terrainHeightSum + (trackIntersect.r * - td.y * min(trackDepth.a, 0.35));
  169. }
  170. #endif
  171. #ifdef INTERRA_OBJECT
  172. float objectHeightMap = SAMPLE_TEXTURE2D_LOD(_MaskMap, sampler_MaskMap, mUV.xy * _BaseColorMap_ST.xy + _BaseColorMap_ST.zw, _MipMapLevel + (lod * _MipMapCount)).b * _MaskMapRemapScale.b + _MaskMapRemapOffset.b;
  173. float heightIntersect = ObjectTerrainIntersection(normal, heightOffset, mask, blendMask, objectHeightMap, _Tessellation_Sharpness);
  174. displacement = lerp((terrainHeightSum + _TerrainTessOffset), (objectHeightMap * _TessellationDisplacement + _TessellationOffset), heightIntersect.r);
  175. displacement *= normal.xyz;
  176. #else
  177. displacement = terrainHeightSum.xxx;
  178. #if defined(TRIPLANAR)
  179. displacement *= float3(worldNormal.x, worldNormal.y * triplanarWeights.y, worldNormal.z);
  180. #else
  181. displacement *= worldNormal.xyz;
  182. #endif
  183. #endif
  184. #if defined(INTERRA_OBJECT) || defined(INTERRA_MESH_TERRAIN)
  185. #if SHADERPASS == SHADERPASS_SHADOWS
  186. tessFactor = _TessellationFactor * _TessellationShadowQuality;
  187. #else
  188. tessFactor = _TessellationFactor;
  189. #endif
  190. #endif
  191. }
  192. #undef TESSELLATION_SAMPLING