TerrainLitDepthNormalsPass.hlsl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. half4 DepthNormalOnlyFragment(VaryingsDepthNormal IN) : SV_TARGET
  58. {
  59. #ifdef _ALPHATEST_ON
  60. ClipHoles(IN.uvMainAndLM.xy);
  61. #endif
  62. float2 splatUV = (IN.uvMainAndLM.xy * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  63. half4 splatControl = SAMPLE_TEXTURE2D(_Control, sampler_Control, splatUV);
  64. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  65. NormalMapMix(IN.uvSplat01, IN.uvSplat23, splatControl, normalTS);
  66. #if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  67. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(-IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz));
  68. #elif defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  69. half3 viewDirWS = IN.viewDir;
  70. float2 sampleCoords = (IN.uvMainAndLM.xy / _TerrainHeightmapRecipSize.zw + 0.5f) * _TerrainHeightmapRecipSize.xy;
  71. half3 normalWS = TransformObjectToWorldNormal(normalize(SAMPLE_TEXTURE2D(_TerrainNormalmapTexture, sampler_TerrainNormalmapTexture, sampleCoords).rgb * 2 - 1));
  72. half3 tangentWS = cross(GetObjectToWorldMatrix()._13_23_33, normalWS);
  73. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(-tangentWS, cross(normalWS, tangentWS), normalWS));
  74. #else
  75. half3 normalWS = IN.normal;
  76. #endif
  77. normalWS = NormalizeNormalPerPixel(normalWS);
  78. return half4(normalWS, 0.0);
  79. }
  80. #endif