InTerra_TerrainLitInput.hlsl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #ifndef UNIVERSAL_TERRAIN_LIT_INPUT_INCLUDED
  2. #define UNIVERSAL_TERRAIN_LIT_INPUT_INCLUDED
  3. #if defined(_TERRAIN_NORMAL_IN_MASK)
  4. #undef _NORMALMAP
  5. #define _NORMALMAP
  6. #endif
  7. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  8. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  9. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  10. CBUFFER_START(UnityPerMaterial)
  11. float4 _MainTex_ST;
  12. half4 _BaseColor;
  13. half _Cutoff;
  14. CBUFFER_END
  15. #define _Surface 0.0 // Terrain is always opaque
  16. #ifdef _LAYERS_ONE
  17. #define _LAYER_COUNT 1
  18. #else
  19. #ifdef _LAYERS_TWO
  20. #define _LAYER_COUNT 2
  21. #else
  22. #if defined(_LAYERS_EIGHT)
  23. #define _TERRAIN_8_LAYERS
  24. #define _LAYER_COUNT 8
  25. #else
  26. #define _LAYER_COUNT 4
  27. #endif
  28. #endif
  29. #endif
  30. CBUFFER_START(_Terrain)
  31. float4 _Control_ST;
  32. float4 _Control_TexelSize;
  33. half _DiffuseHasAlpha0, _DiffuseHasAlpha1, _DiffuseHasAlpha2, _DiffuseHasAlpha3;
  34. half _HeightTransition;
  35. half _NumLayersCount;
  36. #ifdef UNITY_INSTANCING_ENABLED
  37. float4 _TerrainHeightmapRecipSize; // float4(1.0f/width, 1.0f/height, 1.0f/(width-1), 1.0f/(height-1))
  38. float4 _TerrainHeightmapScale; // float4(hmScale.x, hmScale.y / (float)(kMaxHeight), hmScale.z, 0.0f)
  39. #endif
  40. #ifdef SCENESELECTIONPASS
  41. int _ObjectId;
  42. int _PassValue;
  43. #endif
  44. //InTerra
  45. half4 _HT_distance, _MipMapFade;
  46. float _HT_distance_scale, _HT_cover, _MipMapLevel;
  47. half _Distance_Height_blending, _Distance_HeightTransition, _TriplanarOneToAllSteep, _TriplanarSharpness;
  48. half4 _TerrainSize;
  49. float3 _TerrainSizeXZPosY;
  50. half _ControlNumber;
  51. half _ParallaxAffineStepsTerrain;
  52. float _TerrainColorTintStrenght;
  53. float _TerrainNormalTintStrenght;
  54. float4 _TerrainColorTintTexture_ST;
  55. float4 _TerrainNormalTintTexture_ST;
  56. float4 _TerrainNormalTintDistance;
  57. float _HeightmapBlending;
  58. float _Terrain_Parallax;
  59. float _Tracks;
  60. float _GlobalWetness;
  61. float _Gamma;
  62. float _WorldMapping;
  63. //-----Track Property -----
  64. float _TrackAO;
  65. float _TrackEdgeNormals, _TrackEdgeSharpness;
  66. float _TrackNormalStrenght;
  67. float _TrackDetailNormalStrenght;
  68. float _TrackHeightOffset;
  69. float4 _TrackDetailTexture_ST;
  70. float4 _Splat0TriplanarBaseMap_ST;
  71. float _ParallaxTrackAffineSteps;
  72. float _ParallaxTrackSteps;
  73. float _TrackHeightTransition;
  74. float _TrackMultiplyStrenght;
  75. TEXTURE2D(_TrackDetailTexture);
  76. TEXTURE2D(_TrackDetailNormalTexture);
  77. TEXTURE2D(_Splat0TriplanarBaseMap);
  78. #include "InTerra_LayersProperties.hlsl"
  79. DECLARE_TERRAIN_LAYER_PROPS(0)
  80. #ifndef _LAYERS_ONE
  81. DECLARE_TERRAIN_LAYER_PROPS(1)
  82. #ifndef _LAYERS_TWO
  83. DECLARE_TERRAIN_LAYER_PROPS(2)
  84. DECLARE_TERRAIN_LAYER_PROPS(3)
  85. #ifdef _LAYERS_EIGHT
  86. DECLARE_TERRAIN_LAYER_PROPS(4)
  87. DECLARE_TERRAIN_LAYER_PROPS(5)
  88. DECLARE_TERRAIN_LAYER_PROPS(6)
  89. DECLARE_TERRAIN_LAYER_PROPS(7)
  90. #endif
  91. #endif
  92. #endif
  93. CBUFFER_END
  94. TEXTURE2D(_Control); SAMPLER(sampler_Control);
  95. TEXTURE2D(_Control1);
  96. SAMPLER(sampler_Splat0);
  97. #ifdef _TERRAIN_BASEMAP_GEN
  98. SAMPLER(sampler_Mask0);
  99. #endif
  100. TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
  101. #ifdef TERRAIN_SPLAT_BASEPASS
  102. TEXTURE2D(_SpecGlossMap); SAMPLER(sampler_SpecGlossMap);
  103. TEXTURE2D(_MetallicTex); SAMPLER(sampler_MetallicTex);
  104. TEXTURE2D(_TriplanarTex); SAMPLER(sampler_TriplanarTex);
  105. TEXTURE2D(_Triplanar_MetallicAO); SAMPLER(sampler_Triplanar_MetallicAO);
  106. #endif
  107. TEXTURE2D(_TerrainColorTintTexture);
  108. TEXTURE2D(_TerrainNormalTintTexture); SAMPLER(SamplerState_Linear_Repeat);
  109. #if defined(UNITY_INSTANCING_ENABLED) && defined(_TERRAIN_INSTANCED_PERPIXEL_NORMAL)
  110. #define ENABLE_TERRAIN_PERPIXEL_NORMAL
  111. #endif
  112. #if defined(UNITY_INSTANCING_ENABLED) || defined(TRIPLANAR) || defined(_PUDDLES)
  113. TEXTURE2D(_TerrainHeightmapTexture);
  114. TEXTURE2D(_TerrainNormalmapTexture);
  115. SAMPLER(sampler_TerrainNormalmapTexture);
  116. #endif
  117. UNITY_INSTANCING_BUFFER_START(Terrain)
  118. UNITY_DEFINE_INSTANCED_PROP(float4, _TerrainPatchInstanceData) // float4(xBase, yBase, skipScale, ~)
  119. UNITY_INSTANCING_BUFFER_END(Terrain)
  120. #ifdef _ALPHATEST_ON
  121. TEXTURE2D(_TerrainHolesTexture);
  122. SAMPLER(sampler_TerrainHolesTexture);
  123. void ClipHoles(float2 uv)
  124. {
  125. float hole = SAMPLE_TEXTURE2D(_TerrainHolesTexture, sampler_TerrainHolesTexture, uv).r;
  126. clip(hole == 0.0f ? -1 : 1);
  127. }
  128. #endif
  129. #ifdef TERRAIN_SPLAT_BASEPASS
  130. half4 SampleMetallicSpecGloss(float2 uv, half albedoAlpha)
  131. {
  132. half4 specGloss;
  133. specGloss = SAMPLE_TEXTURE2D(_MetallicTex, sampler_MetallicTex, uv);
  134. specGloss.a = albedoAlpha;
  135. return specGloss;
  136. }
  137. inline void InitializeStandardLitSurfaceData(float2 uv, out SurfaceData outSurfaceData)
  138. {
  139. outSurfaceData = (SurfaceData)0;
  140. half4 albedoSmoothness = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
  141. outSurfaceData.alpha = 1;
  142. half4 specGloss = SampleMetallicSpecGloss(uv, albedoSmoothness.a);
  143. outSurfaceData.albedo = albedoSmoothness.rgb;
  144. outSurfaceData.metallic = specGloss.r;
  145. outSurfaceData.specular = half3(0.0h, 0.0h, 0.0h);
  146. outSurfaceData.smoothness = specGloss.a;
  147. outSurfaceData.normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  148. outSurfaceData.occlusion = 1;
  149. outSurfaceData.emission = 0;
  150. }
  151. float _InTerra_GlobalWetness;
  152. #endif
  153. void TerrainInstancing(inout float4 positionOS, inout float3 normal, inout float2 uv)
  154. {
  155. #ifdef UNITY_INSTANCING_ENABLED
  156. float2 patchVertex = positionOS.xy;
  157. float4 instanceData = UNITY_ACCESS_INSTANCED_PROP(Terrain, _TerrainPatchInstanceData);
  158. float2 sampleCoords = (patchVertex.xy + instanceData.xy) * instanceData.z;
  159. float height = UnpackHeightmap(_TerrainHeightmapTexture.Load(int3(sampleCoords, 0)));
  160. positionOS.xz = sampleCoords * _TerrainHeightmapScale.xz;
  161. positionOS.y = height * _TerrainHeightmapScale.y;
  162. #ifdef ENABLE_TERRAIN_PERPIXEL_NORMAL
  163. normal = float3(0, 1, 0);
  164. #else
  165. normal = _TerrainNormalmapTexture.Load(int3(sampleCoords, 0)).rgb * 2 - 1;
  166. #endif
  167. uv = sampleCoords * _TerrainHeightmapRecipSize.zw;
  168. #if defined(TRIPLANAR) || defined(_PUDDLES)
  169. normal = _TerrainNormalmapTexture.Load(int3(sampleCoords, 0)).rgb * 2 - 1;
  170. #endif
  171. #endif
  172. }
  173. void TerrainInstancing(inout float4 positionOS, inout float3 normal)
  174. {
  175. float2 uv = { 0, 0 };
  176. TerrainInstancing(positionOS, normal, uv);
  177. }
  178. #endif