SimpleLitDepthNormalsPass.hlsl 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef UNIVERSAL_SIMPLE_LIT_DEPTH_NORMALS_PASS_INCLUDED
  2. #define UNIVERSAL_SIMPLE_LIT_DEPTH_NORMALS_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. struct Attributes
  5. {
  6. float4 positionOS : POSITION;
  7. float4 tangentOS : TANGENT;
  8. float2 texcoord : TEXCOORD0;
  9. float3 normal : NORMAL;
  10. UNITY_VERTEX_INPUT_INSTANCE_ID
  11. };
  12. struct Varyings
  13. {
  14. float4 positionCS : SV_POSITION;
  15. float2 uv : TEXCOORD1;
  16. #ifdef _NORMALMAP
  17. half4 normalWS : TEXCOORD2; // xyz: normal, w: viewDir.x
  18. half4 tangentWS : TEXCOORD3; // xyz: tangent, w: viewDir.y
  19. half4 bitangentWS : TEXCOORD4; // xyz: bitangent, w: viewDir.z
  20. #else
  21. half3 normalWS : TEXCOORD2;
  22. half3 viewDir : TEXCOORD3;
  23. #endif
  24. UNITY_VERTEX_INPUT_INSTANCE_ID
  25. UNITY_VERTEX_OUTPUT_STEREO
  26. };
  27. Varyings DepthNormalsVertex(Attributes input)
  28. {
  29. Varyings output = (Varyings)0;
  30. UNITY_SETUP_INSTANCE_ID(input);
  31. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  32. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  33. output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
  34. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  35. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normal, input.tangentOS);
  36. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  37. #if defined(_NORMALMAP)
  38. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  39. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  40. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  41. #else
  42. output.normalWS = half3(NormalizeNormalPerVertex(normalInput.normalWS));
  43. #endif
  44. return output;
  45. }
  46. half4 DepthNormalsFragment(Varyings input) : SV_TARGET
  47. {
  48. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  49. Alpha(SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a, _BaseColor, _Cutoff);
  50. #if defined(_GBUFFER_NORMALS_OCT)
  51. float3 normalWS = normalize(input.normalWS);
  52. float2 octNormalWS = PackNormalOctQuadEncode(normalWS); // values between [-1, +1], must use fp32 on some platforms
  53. float2 remappedOctNormalWS = saturate(octNormalWS * 0.5 + 0.5); // values between [ 0, 1]
  54. half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); // values between [ 0, 1]
  55. return half4(packedNormalWS, 0.0);
  56. #else
  57. float2 uv = input.uv;
  58. #if defined(_NORMALMAP)
  59. half3 normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  60. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
  61. #else
  62. half3 normalWS = input.normalWS;
  63. #endif
  64. normalWS = NormalizeNormalPerPixel(normalWS);
  65. return half4(normalWS, 0.0);
  66. #endif
  67. }
  68. #endif