暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BakedLitDepthNormalsPass.hlsl 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #if defined(LOD_FADE_CROSSFADE)
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl"
  6. #endif
  7. struct Attributes
  8. {
  9. float4 positionOS : POSITION;
  10. float2 uv : TEXCOORD0;
  11. half3 normalOS : NORMAL;
  12. half4 tangentOS : TANGENT;
  13. UNITY_VERTEX_INPUT_INSTANCE_ID
  14. };
  15. struct Varyings
  16. {
  17. float4 positionCS : SV_POSITION;
  18. float2 uv : TEXCOORD0;
  19. half3 normalWS : TEXCOORD1;
  20. #if defined(_NORMALMAP)
  21. half4 tangentWS : TEXCOORD3;
  22. #endif
  23. UNITY_VERTEX_INPUT_INSTANCE_ID
  24. UNITY_VERTEX_OUTPUT_STEREO
  25. };
  26. Varyings DepthNormalsVertex(Attributes input)
  27. {
  28. Varyings output = (Varyings)0;
  29. UNITY_SETUP_INSTANCE_ID(input);
  30. UNITY_TRANSFER_INSTANCE_ID(input, output);
  31. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  32. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  33. output.positionCS = vertexInput.positionCS;
  34. output.uv = TRANSFORM_TEX(input.uv, _BaseMap).xy;
  35. // normalWS and tangentWS already normalize.
  36. // this is required to avoid skewing the direction during interpolation
  37. // also required for per-vertex SH evaluation
  38. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  39. output.normalWS = half3(normalInput.normalWS);
  40. #if defined(_NORMALMAP)
  41. real sign = input.tangentOS.w * GetOddNegativeScale();
  42. output.tangentWS = half4(normalInput.tangentWS.xyz, sign);
  43. #endif
  44. return output;
  45. }
  46. void DepthNormalsFragment(
  47. Varyings input
  48. , out half4 outNormalWS : SV_Target0
  49. #ifdef _WRITE_RENDERING_LAYERS
  50. , out float4 outRenderingLayers : SV_Target1
  51. #endif
  52. )
  53. {
  54. UNITY_SETUP_INSTANCE_ID(input);
  55. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  56. half4 texColor = (half4) SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv);
  57. half alpha = texColor.a * _BaseColor.a;
  58. AlphaDiscard(alpha, _Cutoff);
  59. #ifdef LOD_FADE_CROSSFADE
  60. LODFadeCrossFade(input.positionCS);
  61. #endif
  62. #if defined(_GBUFFER_NORMALS_OCT)
  63. float3 normalWS = normalize(input.normalWS);
  64. float2 octNormalWS = PackNormalOctQuadEncode(normalWS); // values between [-1, +1], must use fp32 on some platforms
  65. float2 remappedOctNormalWS = saturate(octNormalWS * 0.5 + 0.5); // values between [ 0, 1]
  66. half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); // values between [ 0, 1]
  67. outNormalWS = half4(packedNormalWS, 0.0);
  68. #else
  69. #if defined(_NORMALMAP)
  70. half3 normalTS = SampleNormal(input.uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap)).xyz;
  71. half sgn = input.tangentWS.w; // should be either +1 or -1
  72. half3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  73. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS));
  74. #else
  75. half3 normalWS = input.normalWS;
  76. #endif
  77. outNormalWS = half4(NormalizeNormalPerPixel(normalWS), 0.0);
  78. #endif
  79. #ifdef _WRITE_RENDERING_LAYERS
  80. uint renderingLayers = GetMeshRenderingLayer();
  81. outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
  82. #endif
  83. }
  84. #endif