CTI URP Billboard DepthNormal Pass.hlsl 3.3 KB

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