CTI URP Bark DepthNormal Pass.hlsl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. float2 uv : TEXCOORD0;
  8. #endif
  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. #if defined (_NORMALMAP) && defined(_NORMALINDEPTHNORMALPASS)
  18. float2 uv : TEXCOORD0;
  19. #endif
  20. float3 normalWS : TEXCOORD4;
  21. #if defined (_NORMALMAP) && defined(_NORMALINDEPTHNORMALPASS)
  22. half4 tangentWS : TEXCOORD5;
  23. #endif
  24. // CTI specific
  25. #if defined(CTIBARKARRAY)
  26. half layer : TEXCOORD6;
  27. #endif
  28. //UNITY_VERTEX_INPUT_INSTANCE_ID
  29. UNITY_VERTEX_OUTPUT_STEREO
  30. };
  31. //--------------------------------------
  32. // Vertex shader
  33. #include "Includes/CTI URP Bending.hlsl"
  34. Varyings DepthNormalsVertex(Attributes input)
  35. {
  36. Varyings output = (Varyings)0;
  37. UNITY_SETUP_INSTANCE_ID(input);
  38. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  39. CTI_AnimateVertex(
  40. input,
  41. #if defined (_BENDINGCOLRSONLY)
  42. float4(input.color.rg, input.color.ab), // animParams,
  43. #else
  44. float4(input.color.rg, input.texcoord1.xy), // animParams,
  45. #endif
  46. _BaseWindMultipliers
  47. );
  48. // CTI special
  49. #if defined(CTIBARKARRAY)
  50. output.layer = input.color.b;
  51. #endif
  52. #if defined (_NORMALMAP) && defined(_NORMALINDEPTHNORMALPASS)
  53. output.uv = input.uv;
  54. #endif
  55. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  56. #if defined (_NORMALMAP) && defined(_NORMALINDEPTHNORMALPASS)
  57. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  58. #else
  59. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, float4(1,1,1,1));
  60. #endif
  61. output.normalWS = normalInput.normalWS;
  62. #if defined (_NORMALMAP) && defined(_NORMALINDEPTHNORMALPASS)
  63. real sign = input.tangentOS.w * GetOddNegativeScale();
  64. output.tangentWS = half4(normalInput.tangentWS.xyz, sign);
  65. #endif
  66. output.positionCS = vertexInput.positionCS;
  67. return output;
  68. }
  69. half4 DepthNormalsFragment(Varyings input) : SV_TARGET
  70. {
  71. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  72. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  73. LODDitheringTransition(input.positionCS.xyz, unity_LODFade.x);
  74. #endif
  75. // URP 12
  76. #if defined(_NORMALMAP) && defined(_NORMALINDEPTHNORMALPASS)
  77. half3 normalTS;
  78. #if defined(CTIBARKARRAY)
  79. half4 sampleNormal = SAMPLE_TEXTURE2D_ARRAY(_BumpOcclusionMapArray, sampler_BumpOcclusionMapArray, input.uv, input.layer);
  80. #else
  81. half4 sampleNormal = SAMPLE_TEXTURE2D(_BumpOcclusionMap, sampler_BumpOcclusionMap, input.uv);
  82. #endif
  83. half3 tangentNormal;
  84. normalTS.xy = sampleNormal.ag * 2 - 1;
  85. normalTS.z = max(1.0e-16, sqrt(1.0h - saturate(dot(normalTS.xy, normalTS.xy))));
  86. float sgn = input.tangentWS.w; // should be either +1 or -1
  87. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  88. input.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS.xyz));
  89. #endif
  90. #if defined(_GBUFFER_NORMALS_OCT)
  91. float3 normalWS = normalize(input.normalWS);
  92. float2 octNormalWS = PackNormalOctQuadEncode(normalWS); // values between [-1, +1], must use fp32 on some platforms.
  93. float2 remappedOctNormalWS = saturate(octNormalWS * 0.5 + 0.5); // values between [ 0, 1]
  94. half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); // values between [ 0, 1]
  95. return half4(packedNormalWS, 0.0);
  96. #else
  97. float3 normalWS = NormalizeNormalPerPixel(input.normalWS);
  98. return half4(normalWS, 0.0);
  99. #endif
  100. }