BakedLitDepthNormalsPass.hlsl 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef UNIVERSAL_BAKEDLIT_DEPTH_NORMALS_PASS_INCLUDED
  2. #define UNIVERSAL_BAKEDLIT_DEPTH_NORMALS_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  4. struct Attributes
  5. {
  6. float4 positionOS : POSITION;
  7. float2 uv : TEXCOORD0;
  8. half3 normalOS : NORMAL;
  9. half4 tangentOS : TANGENT;
  10. UNITY_VERTEX_INPUT_INSTANCE_ID
  11. };
  12. struct Varyings
  13. {
  14. float4 vertex : SV_POSITION;
  15. float2 uv : TEXCOORD0;
  16. half3 normalWS : TEXCOORD1;
  17. #if defined(_NORMALMAP)
  18. half4 tangentWS : TEXCOORD3;
  19. #endif
  20. UNITY_VERTEX_INPUT_INSTANCE_ID
  21. UNITY_VERTEX_OUTPUT_STEREO
  22. };
  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. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  29. output.vertex = vertexInput.positionCS;
  30. output.uv = TRANSFORM_TEX(input.uv, _BaseMap).xy;
  31. // normalWS and tangentWS already normalize.
  32. // this is required to avoid skewing the direction during interpolation
  33. // also required for per-vertex SH evaluation
  34. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  35. output.normalWS = half3(normalInput.normalWS);
  36. #if defined(_NORMALMAP)
  37. real sign = input.tangentOS.w * GetOddNegativeScale();
  38. output.tangentWS = half4(normalInput.tangentWS.xyz, sign);
  39. #endif
  40. return output;
  41. }
  42. float4 DepthNormalsFragment(Varyings input) : SV_TARGET
  43. {
  44. UNITY_SETUP_INSTANCE_ID(input);
  45. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  46. half4 texColor = (half4) SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv);
  47. half alpha = texColor.a * _BaseColor.a;
  48. AlphaDiscard(alpha, _Cutoff);
  49. #if defined(_GBUFFER_NORMALS_OCT)
  50. float3 normalWS = normalize(input.normalWS);
  51. float2 octNormalWS = PackNormalOctQuadEncode(normalWS); // values between [-1, +1], must use fp32 on some platforms
  52. float2 remappedOctNormalWS = saturate(octNormalWS * 0.5 + 0.5); // values between [ 0, 1]
  53. half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); // values between [ 0, 1]
  54. return half4(packedNormalWS, 0.0);
  55. #else
  56. #if defined(_NORMALMAP)
  57. half3 normalTS = SampleNormal(input.uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap)).xyz;
  58. half sgn = input.tangentWS.w; // should be either +1 or -1
  59. half3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  60. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS));
  61. #else
  62. half3 normalWS = input.normalWS;
  63. #endif
  64. return half4(NormalizeNormalPerPixel(normalWS), 0.0);
  65. #endif
  66. }
  67. #endif