CTI URP Leaves DepthNormal Pass.hlsl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. struct Attributes
  2. {
  3. float3 positionOS : POSITION;
  4. float3 normalOS : NORMAL; // Bending also
  5. #if defined (_NORMALMAP) && defined(_NORMALINDEPTHNORMALPASS)
  6. float4 tangentOS : TANGENT;
  7. #endif
  8. float2 uv : TEXCOORD0;
  9. float2 texcoord1 : TEXCOORD1; // Bending
  10. half4 color : COLOR; // Bending
  11. float3 texcoord2 : TEXCOORD2; // Leaves only: Pivot
  12. UNITY_VERTEX_INPUT_INSTANCE_ID
  13. };
  14. struct Varyings
  15. {
  16. float4 positionCS : SV_POSITION;
  17. float2 uv : TEXCOORD0;
  18. half3 normalWS : TEXCOORD4;
  19. #if defined (_NORMALMAP) && defined(_NORMALINDEPTHNORMALPASS)
  20. half4 tangentWS : TEXCOORD5;
  21. #endif
  22. //UNITY_VERTEX_INPUT_INSTANCE_ID
  23. UNITY_VERTEX_OUTPUT_STEREO
  24. };
  25. //--------------------------------------
  26. // Vertex shader
  27. #include "Includes/CTI URP Bending.hlsl"
  28. Varyings DepthNormalsVertex(Attributes input)
  29. {
  30. Varyings output = (Varyings)0;
  31. UNITY_SETUP_INSTANCE_ID(input);
  32. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  33. CTI_AnimateVertex(
  34. input,
  35. #if defined (_BENDINGCOLRSONLY)
  36. float4(input.color.rg, input.color.ab), // animParams,
  37. #else
  38. float4(input.color.rg, input.texcoord1.xy), // animParams,
  39. #endif
  40. _BaseWindMultipliers
  41. );
  42. output.uv = input.uv;
  43. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  44. #if defined (_NORMALMAP) && defined(_NORMALINDEPTHNORMALPASS)
  45. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  46. #else
  47. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, float4(1,1,1,1));
  48. #endif
  49. output.normalWS = normalInput.normalWS;
  50. #if defined (_NORMALMAP) && defined(_NORMALINDEPTHNORMALPASS)
  51. real sign = input.tangentOS.w * GetOddNegativeScale();
  52. output.tangentWS = half4(normalInput.tangentWS.xyz, sign);
  53. #endif
  54. output.positionCS = vertexInput.positionCS;
  55. return output;
  56. }
  57. half4 DepthNormalsFragment(Varyings input, half facing : VFACE) : SV_TARGET
  58. {
  59. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  60. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  61. LODDitheringTransition(input.positionCS.xyz, unity_LODFade.x);
  62. #endif
  63. half alpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv).a;
  64. clip(alpha - _Cutoff);
  65. // URP 12
  66. #if defined(_NORMALMAP) && defined(_NORMALINDEPTHNORMALPASS)
  67. half3 normalTS;
  68. half4 sampleNormal = SAMPLE_TEXTURE2D(_BumpSpecMap, sampler_BumpSpecMap, input.uv);
  69. normalTS.xy = sampleNormal.ag * 2.0h - 1.0h;
  70. normalTS.z = max(1.0e-16, sqrt(1.0h - saturate(dot(normalTS.xy, normalTS.xy))));
  71. normalTS.z *= facing;
  72. float sgn = input.tangentWS.w; // should be either +1 or -1
  73. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  74. input.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS.xyz));
  75. #else
  76. input.normalWS *= facing;
  77. #endif
  78. #if defined(_GBUFFER_NORMALS_OCT)
  79. float3 normalWS = normalize(input.normalWS);
  80. float2 octNormalWS = PackNormalOctQuadEncode(normalWS); // values between [-1, +1], must use fp32 on some platforms.
  81. float2 remappedOctNormalWS = saturate(octNormalWS * 0.5h + 0.5h); // values between [ 0, 1]
  82. half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); // values between [ 0, 1]
  83. return half4(packedNormalWS, 0.0h);
  84. #else
  85. float3 normalWS = NormalizeNormalPerPixel(input.normalWS);
  86. return half4(normalWS, 0.0h);
  87. #endif
  88. }