LitDepthNormalsPass.hlsl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/ShaderLibrary/Lighting.hlsl"
  4. #if defined(_DETAIL_MULX2) || defined(_DETAIL_SCALED)
  5. #define _DETAIL
  6. #endif
  7. // GLES2 has limited amount of interpolators
  8. #if defined(_PARALLAXMAP) && !defined(SHADER_API_GLES)
  9. #define REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR
  10. #endif
  11. #if (defined(_NORMALMAP) || (defined(_PARALLAXMAP) && !defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR))) || defined(_DETAIL)
  12. #define REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR
  13. #endif
  14. struct Attributes
  15. {
  16. float4 positionOS : POSITION;
  17. float4 tangentOS : TANGENT;
  18. float2 texcoord : TEXCOORD0;
  19. float3 normal : NORMAL;
  20. UNITY_VERTEX_INPUT_INSTANCE_ID
  21. };
  22. struct Varyings
  23. {
  24. float4 positionCS : SV_POSITION;
  25. float2 uv : TEXCOORD1;
  26. half3 normalWS : TEXCOORD2;
  27. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
  28. half4 tangentWS : TEXCOORD4; // xyz: tangent, w: sign
  29. #endif
  30. half3 viewDirWS : TEXCOORD5;
  31. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  32. half3 viewDirTS : TEXCOORD8;
  33. #endif
  34. UNITY_VERTEX_INPUT_INSTANCE_ID
  35. UNITY_VERTEX_OUTPUT_STEREO
  36. };
  37. Varyings DepthNormalsVertex(Attributes input)
  38. {
  39. Varyings output = (Varyings)0;
  40. UNITY_SETUP_INSTANCE_ID(input);
  41. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  42. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  43. output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
  44. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  45. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normal, input.tangentOS);
  46. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  47. output.normalWS = half3(normalInput.normalWS);
  48. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR) || defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  49. float sign = input.tangentOS.w * float(GetOddNegativeScale());
  50. half4 tangentWS = half4(normalInput.tangentWS.xyz, sign);
  51. #endif
  52. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
  53. output.tangentWS = tangentWS;
  54. #endif
  55. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  56. half3 viewDirTS = GetViewDirectionTangentSpace(tangentWS, output.normalWS, viewDirWS);
  57. output.viewDirTS = viewDirTS;
  58. #endif
  59. return output;
  60. }
  61. half4 DepthNormalsFragment(Varyings input) : SV_TARGET
  62. {
  63. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  64. Alpha(SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a, _BaseColor, _Cutoff);
  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.5 + 0.5); // values between [ 0, 1]
  69. half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); // values between [ 0, 1]
  70. return half4(packedNormalWS, 0.0);
  71. #else
  72. float2 uv = input.uv;
  73. #if defined(_PARALLAXMAP)
  74. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  75. half3 viewDirTS = input.viewDirTS;
  76. #else
  77. half3 viewDirTS = GetViewDirectionTangentSpace(input.tangentWS, input.normalWS, input.viewDirWS);
  78. #endif
  79. ApplyPerPixelDisplacement(viewDirTS, uv);
  80. #endif
  81. #if defined(_NORMALMAP) || defined(_DETAIL)
  82. float sgn = input.tangentWS.w; // should be either +1 or -1
  83. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  84. float3 normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);
  85. #if defined(_DETAIL)
  86. half detailMask = SAMPLE_TEXTURE2D(_DetailMask, sampler_DetailMask, uv).a;
  87. float2 detailUv = uv * _DetailAlbedoMap_ST.xy + _DetailAlbedoMap_ST.zw;
  88. normalTS = ApplyDetailNormal(detailUv, normalTS, detailMask);
  89. #endif
  90. float3 normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz));
  91. #else
  92. float3 normalWS = input.normalWS;
  93. #endif
  94. return half4(NormalizeNormalPerPixel(normalWS), 0.0);
  95. #endif
  96. }
  97. #endif