Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TerrainLitDepthNormalsPass.hlsl 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef UNIVERSAL_FORWARD_LIT_DEPTH_NORMALS_PASS_INCLUDED
  2. #define UNIVERSAL_FORWARD_LIT_DEPTH_NORMALS_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/Shaders/Terrain/TerrainLitPasses.hlsl"
  4. // DepthNormal pass
  5. struct AttributesDepthNormal
  6. {
  7. float4 positionOS : POSITION;
  8. half3 normalOS : NORMAL;
  9. float2 texcoord : TEXCOORD0;
  10. UNITY_VERTEX_INPUT_INSTANCE_ID
  11. };
  12. struct VaryingsDepthNormal
  13. {
  14. float4 uvMainAndLM : TEXCOORD0; // xy: control, zw: lightmap
  15. #ifndef TERRAIN_SPLAT_BASEPASS
  16. float4 uvSplat01 : TEXCOORD1; // xy: splat0, zw: splat1
  17. float4 uvSplat23 : TEXCOORD2; // xy: splat2, zw: splat3
  18. #endif
  19. #if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  20. half4 normal : TEXCOORD3; // xyz: normal, w: viewDir.x
  21. half4 tangent : TEXCOORD4; // xyz: tangent, w: viewDir.y
  22. half4 bitangent : TEXCOORD5; // xyz: bitangent, w: viewDir.z
  23. #else
  24. half3 normal : TEXCOORD3;
  25. #endif
  26. float4 clipPos : SV_POSITION;
  27. UNITY_VERTEX_OUTPUT_STEREO
  28. };
  29. VaryingsDepthNormal DepthNormalOnlyVertex(AttributesDepthNormal v)
  30. {
  31. VaryingsDepthNormal o = (VaryingsDepthNormal)0;
  32. UNITY_SETUP_INSTANCE_ID(v);
  33. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  34. TerrainInstancing(v.positionOS, v.normalOS, v.texcoord);
  35. const VertexPositionInputs attributes = GetVertexPositionInputs(v.positionOS.xyz);
  36. o.uvMainAndLM.xy = v.texcoord;
  37. o.uvMainAndLM.zw = v.texcoord * unity_LightmapST.xy + unity_LightmapST.zw;
  38. #ifndef TERRAIN_SPLAT_BASEPASS
  39. o.uvSplat01.xy = TRANSFORM_TEX(v.texcoord, _Splat0);
  40. o.uvSplat01.zw = TRANSFORM_TEX(v.texcoord, _Splat1);
  41. o.uvSplat23.xy = TRANSFORM_TEX(v.texcoord, _Splat2);
  42. o.uvSplat23.zw = TRANSFORM_TEX(v.texcoord, _Splat3);
  43. #endif
  44. #if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  45. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(attributes.positionWS);
  46. float4 vertexTangent = float4(cross(float3(0, 0, 1), v.normalOS), 1.0);
  47. VertexNormalInputs normalInput = GetVertexNormalInputs(v.normalOS, vertexTangent);
  48. o.normal = half4(normalInput.normalWS, viewDirWS.x);
  49. o.tangent = half4(normalInput.tangentWS, viewDirWS.y);
  50. o.bitangent = half4(normalInput.bitangentWS, viewDirWS.z);
  51. #else
  52. o.normal = TransformObjectToWorldNormal(v.normalOS);
  53. #endif
  54. o.clipPos = attributes.positionCS;
  55. return o;
  56. }
  57. void DepthNormalOnlyFragment(
  58. VaryingsDepthNormal IN
  59. , out half4 outNormalWS : SV_Target0
  60. #ifdef _WRITE_RENDERING_LAYERS
  61. , out float4 outRenderingLayers : SV_Target1
  62. #endif
  63. )
  64. {
  65. #ifdef _ALPHATEST_ON
  66. ClipHoles(IN.uvMainAndLM.xy);
  67. #endif
  68. float2 splatUV = (IN.uvMainAndLM.xy * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  69. half4 splatControl = SAMPLE_TEXTURE2D(_Control, sampler_Control, splatUV);
  70. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  71. NormalMapMix(IN.uvSplat01, IN.uvSplat23, splatControl, normalTS);
  72. #if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  73. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(-IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz));
  74. #elif defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  75. half3 viewDirWS = IN.viewDir;
  76. float2 sampleCoords = (IN.uvMainAndLM.xy / _TerrainHeightmapRecipSize.zw + 0.5f) * _TerrainHeightmapRecipSize.xy;
  77. half3 normalWS = TransformObjectToWorldNormal(normalize(SAMPLE_TEXTURE2D(_TerrainNormalmapTexture, sampler_TerrainNormalmapTexture, sampleCoords).rgb * 2 - 1));
  78. half3 tangentWS = cross(GetObjectToWorldMatrix()._13_23_33, normalWS);
  79. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(-tangentWS, cross(normalWS, tangentWS), normalWS));
  80. #else
  81. half3 normalWS = IN.normal;
  82. #endif
  83. normalWS = NormalizeNormalPerPixel(normalWS);
  84. outNormalWS = half4(normalWS, 0.0);
  85. #ifdef _WRITE_RENDERING_LAYERS
  86. uint renderingLayers = GetMeshRenderingLayer();
  87. outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
  88. #endif
  89. }
  90. #endif