Nessuna descrizione
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.

LitDepthNormalsPass.hlsl 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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(LOD_FADE_CROSSFADE)
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl"
  6. #endif
  7. #if defined(_DETAIL_MULX2) || defined(_DETAIL_SCALED)
  8. #define _DETAIL
  9. #endif
  10. #if defined(_PARALLAXMAP)
  11. #define REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR
  12. #endif
  13. #if (defined(_NORMALMAP) || (defined(_PARALLAXMAP) && !defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR))) || defined(_DETAIL)
  14. #define REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR
  15. #endif
  16. #if defined(_ALPHATEST_ON) || defined(_PARALLAXMAP) || defined(_NORMALMAP) || defined(_DETAIL)
  17. #define REQUIRES_UV_INTERPOLATOR
  18. #endif
  19. struct Attributes
  20. {
  21. float4 positionOS : POSITION;
  22. float4 tangentOS : TANGENT;
  23. float2 texcoord : TEXCOORD0;
  24. float3 normal : NORMAL;
  25. UNITY_VERTEX_INPUT_INSTANCE_ID
  26. };
  27. struct Varyings
  28. {
  29. float4 positionCS : SV_POSITION;
  30. #if defined(REQUIRES_UV_INTERPOLATOR)
  31. float2 uv : TEXCOORD1;
  32. #endif
  33. half3 normalWS : TEXCOORD2;
  34. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
  35. half4 tangentWS : TEXCOORD4; // xyz: tangent, w: sign
  36. #endif
  37. half3 viewDirWS : TEXCOORD5;
  38. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  39. half3 viewDirTS : TEXCOORD8;
  40. #endif
  41. UNITY_VERTEX_INPUT_INSTANCE_ID
  42. UNITY_VERTEX_OUTPUT_STEREO
  43. };
  44. Varyings DepthNormalsVertex(Attributes input)
  45. {
  46. Varyings output = (Varyings)0;
  47. UNITY_SETUP_INSTANCE_ID(input);
  48. UNITY_TRANSFER_INSTANCE_ID(input, output);
  49. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  50. #if defined(REQUIRES_UV_INTERPOLATOR)
  51. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  52. #endif
  53. output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
  54. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  55. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normal, input.tangentOS);
  56. output.normalWS = half3(normalInput.normalWS);
  57. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR) || defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  58. float sign = input.tangentOS.w * float(GetOddNegativeScale());
  59. half4 tangentWS = half4(normalInput.tangentWS.xyz, sign);
  60. #endif
  61. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
  62. output.tangentWS = tangentWS;
  63. #endif
  64. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  65. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  66. half3 viewDirTS = GetViewDirectionTangentSpace(tangentWS, output.normalWS, viewDirWS);
  67. output.viewDirTS = viewDirTS;
  68. #endif
  69. return output;
  70. }
  71. void DepthNormalsFragment(
  72. Varyings input
  73. , out half4 outNormalWS : SV_Target0
  74. #ifdef _WRITE_RENDERING_LAYERS
  75. , out float4 outRenderingLayers : SV_Target1
  76. #endif
  77. )
  78. {
  79. UNITY_SETUP_INSTANCE_ID(input);
  80. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  81. #if defined(_ALPHATEST_ON)
  82. Alpha(SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a, _BaseColor, _Cutoff);
  83. #endif
  84. #if defined(LOD_FADE_CROSSFADE)
  85. LODFadeCrossFade(input.positionCS);
  86. #endif
  87. #if defined(_GBUFFER_NORMALS_OCT)
  88. float3 normalWS = normalize(input.normalWS);
  89. float2 octNormalWS = PackNormalOctQuadEncode(normalWS); // values between [-1, +1], must use fp32 on some platforms
  90. float2 remappedOctNormalWS = saturate(octNormalWS * 0.5 + 0.5); // values between [ 0, 1]
  91. half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); // values between [ 0, 1]
  92. outNormalWS = half4(packedNormalWS, 0.0);
  93. #else
  94. #if defined(_PARALLAXMAP)
  95. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  96. half3 viewDirTS = input.viewDirTS;
  97. #else
  98. half3 viewDirTS = GetViewDirectionTangentSpace(input.tangentWS, input.normalWS, input.viewDirWS);
  99. #endif
  100. ApplyPerPixelDisplacement(viewDirTS, input.uv);
  101. #endif
  102. #if defined(_NORMALMAP) || defined(_DETAIL)
  103. float sgn = input.tangentWS.w; // should be either +1 or -1
  104. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  105. float3 normalTS = SampleNormal(input.uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);
  106. #if defined(_DETAIL)
  107. half detailMask = SAMPLE_TEXTURE2D(_DetailMask, sampler_DetailMask, input.uv).a;
  108. float2 detailUv = input.uv * _DetailAlbedoMap_ST.xy + _DetailAlbedoMap_ST.zw;
  109. normalTS = ApplyDetailNormal(detailUv, normalTS, detailMask);
  110. #endif
  111. float3 normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz));
  112. #else
  113. float3 normalWS = input.normalWS;
  114. #endif
  115. outNormalWS = half4(NormalizeNormalPerPixel(normalWS), 0.0);
  116. #endif
  117. #ifdef _WRITE_RENDERING_LAYERS
  118. uint renderingLayers = GetMeshRenderingLayer();
  119. outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
  120. #endif
  121. }
  122. #endif