Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TerrainLitInput.hlsl 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef UNIVERSAL_TERRAIN_LIT_INPUT_INCLUDED
  2. #define UNIVERSAL_TERRAIN_LIT_INPUT_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  4. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  6. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/DebugMipmapStreamingMacros.hlsl"
  7. CBUFFER_START(UnityPerMaterial)
  8. float4 _MainTex_ST;
  9. half4 _BaseColor;
  10. half _Cutoff;
  11. UNITY_TEXTURE_STREAMING_DEBUG_VARS_FOR_TEX(_Control);
  12. float4 _Splat0_TexelSize, _Splat1_TexelSize, _Splat2_TexelSize, _Splat3_TexelSize;
  13. UNITY_TEXTURE_STREAMING_DEBUG_VARS_FOR_TEX(_Splat0);
  14. UNITY_TEXTURE_STREAMING_DEBUG_VARS_FOR_TEX(_Splat1);
  15. UNITY_TEXTURE_STREAMING_DEBUG_VARS_FOR_TEX(_Splat2);
  16. UNITY_TEXTURE_STREAMING_DEBUG_VARS_FOR_TEX(_Splat3);
  17. CBUFFER_END
  18. #define _Surface 0.0 // Terrain is always opaque
  19. CBUFFER_START(_Terrain)
  20. half _NormalScale0, _NormalScale1, _NormalScale2, _NormalScale3;
  21. half _Metallic0, _Metallic1, _Metallic2, _Metallic3;
  22. half _Smoothness0, _Smoothness1, _Smoothness2, _Smoothness3;
  23. half4 _DiffuseRemapScale0, _DiffuseRemapScale1, _DiffuseRemapScale2, _DiffuseRemapScale3;
  24. half4 _MaskMapRemapOffset0, _MaskMapRemapOffset1, _MaskMapRemapOffset2, _MaskMapRemapOffset3;
  25. half4 _MaskMapRemapScale0, _MaskMapRemapScale1, _MaskMapRemapScale2, _MaskMapRemapScale3;
  26. float4 _Control_ST;
  27. float4 _Control_TexelSize;
  28. half _DiffuseHasAlpha0, _DiffuseHasAlpha1, _DiffuseHasAlpha2, _DiffuseHasAlpha3;
  29. half _LayerHasMask0, _LayerHasMask1, _LayerHasMask2, _LayerHasMask3;
  30. half4 _Splat0_ST, _Splat1_ST, _Splat2_ST, _Splat3_ST;
  31. half _HeightTransition;
  32. half _NumLayersCount;
  33. #ifdef UNITY_INSTANCING_ENABLED
  34. float4 _TerrainHeightmapRecipSize; // float4(1.0f/width, 1.0f/height, 1.0f/(width-1), 1.0f/(height-1))
  35. float4 _TerrainHeightmapScale; // float4(hmScale.x, hmScale.y / (float)(kMaxHeight), hmScale.z, 0.0f)
  36. #endif
  37. #ifdef SCENESELECTIONPASS
  38. int _ObjectId;
  39. int _PassValue;
  40. #endif
  41. CBUFFER_END
  42. TEXTURE2D(_Control); SAMPLER(sampler_Control);
  43. TEXTURE2D(_Splat0); SAMPLER(sampler_Splat0);
  44. TEXTURE2D(_Splat1);
  45. TEXTURE2D(_Splat2);
  46. TEXTURE2D(_Splat3);
  47. #ifdef _NORMALMAP
  48. TEXTURE2D(_Normal0); SAMPLER(sampler_Normal0);
  49. TEXTURE2D(_Normal1);
  50. TEXTURE2D(_Normal2);
  51. TEXTURE2D(_Normal3);
  52. #endif
  53. #ifdef _MASKMAP
  54. TEXTURE2D(_Mask0); SAMPLER(sampler_Mask0);
  55. TEXTURE2D(_Mask1);
  56. TEXTURE2D(_Mask2);
  57. TEXTURE2D(_Mask3);
  58. #endif
  59. TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
  60. TEXTURE2D(_SpecGlossMap); SAMPLER(sampler_SpecGlossMap);
  61. TEXTURE2D(_MetallicTex); SAMPLER(sampler_MetallicTex);
  62. #if defined(UNITY_INSTANCING_ENABLED) && defined(_TERRAIN_INSTANCED_PERPIXEL_NORMAL)
  63. #define ENABLE_TERRAIN_PERPIXEL_NORMAL
  64. #endif
  65. #ifdef UNITY_INSTANCING_ENABLED
  66. TEXTURE2D(_TerrainHeightmapTexture);
  67. TEXTURE2D(_TerrainNormalmapTexture);
  68. SAMPLER(sampler_TerrainNormalmapTexture);
  69. #endif
  70. UNITY_INSTANCING_BUFFER_START(Terrain)
  71. UNITY_DEFINE_INSTANCED_PROP(float4, _TerrainPatchInstanceData) // float4(xBase, yBase, skipScale, ~)
  72. UNITY_INSTANCING_BUFFER_END(Terrain)
  73. #ifdef _ALPHATEST_ON
  74. TEXTURE2D(_TerrainHolesTexture);
  75. SAMPLER(sampler_TerrainHolesTexture);
  76. void ClipHoles(float2 uv)
  77. {
  78. float hole = SAMPLE_TEXTURE2D(_TerrainHolesTexture, sampler_TerrainHolesTexture, uv).r;
  79. // Fixes bug where compression is enabled and 0 isn't actually 0 but low like 1/2047. (UUM-61913)
  80. float epsilon = 0.0005f;
  81. clip(hole < epsilon ? -1 : 1);
  82. }
  83. #endif
  84. half4 SampleMetallicSpecGloss(float2 uv, half albedoAlpha)
  85. {
  86. half4 specGloss;
  87. specGloss = SAMPLE_TEXTURE2D(_MetallicTex, sampler_MetallicTex, uv);
  88. specGloss.a = albedoAlpha;
  89. return specGloss;
  90. }
  91. inline void InitializeStandardLitSurfaceData(float2 uv, out SurfaceData outSurfaceData)
  92. {
  93. outSurfaceData = (SurfaceData)0;
  94. half4 albedoSmoothness = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
  95. outSurfaceData.alpha = 1;
  96. half4 specGloss = SampleMetallicSpecGloss(uv, albedoSmoothness.a);
  97. outSurfaceData.albedo = albedoSmoothness.rgb;
  98. outSurfaceData.metallic = specGloss.r;
  99. outSurfaceData.specular = half3(0.0h, 0.0h, 0.0h);
  100. outSurfaceData.smoothness = specGloss.a;
  101. outSurfaceData.normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  102. outSurfaceData.occlusion = 1;
  103. outSurfaceData.emission = 0;
  104. }
  105. void TerrainInstancing(inout float4 positionOS, inout float3 normal, inout float2 uv)
  106. {
  107. #ifdef UNITY_INSTANCING_ENABLED
  108. float2 patchVertex = positionOS.xy;
  109. float4 instanceData = UNITY_ACCESS_INSTANCED_PROP(Terrain, _TerrainPatchInstanceData);
  110. float2 sampleCoords = (patchVertex.xy + instanceData.xy) * instanceData.z; // (xy + float2(xBase,yBase)) * skipScale
  111. float height = UnpackHeightmap(_TerrainHeightmapTexture.Load(int3(sampleCoords, 0)));
  112. positionOS.xz = sampleCoords * _TerrainHeightmapScale.xz;
  113. positionOS.y = height * _TerrainHeightmapScale.y;
  114. #ifdef ENABLE_TERRAIN_PERPIXEL_NORMAL
  115. normal = float3(0, 1, 0);
  116. #else
  117. normal = _TerrainNormalmapTexture.Load(int3(sampleCoords, 0)).rgb * 2 - 1;
  118. #endif
  119. uv = sampleCoords * _TerrainHeightmapRecipSize.zw;
  120. #endif
  121. }
  122. void TerrainInstancing(inout float4 positionOS, inout float3 normal)
  123. {
  124. float2 uv = { 0, 0 };
  125. TerrainInstancing(positionOS, normal, uv);
  126. }
  127. #endif