TerrainLitInput.hlsl 4.7 KB

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