LitGBufferPass.hlsl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifndef UNIVERSAL_LIT_GBUFFER_PASS_INCLUDED
  2. #define UNIVERSAL_LIT_GBUFFER_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityGBuffer.hlsl"
  5. // TODO: Currently we support viewDirTS caclulated in vertex shader and in fragments shader.
  6. // As both solutions have their advantages and disadvantages (etc. shader target 2.0 has only 8 interpolators).
  7. // We need to find out if we can stick to one solution, which we needs testing.
  8. // So keeping this until I get manaul QA pass.
  9. #if defined(_PARALLAXMAP) && (SHADER_TARGET >= 30)
  10. #define REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR
  11. #endif
  12. #if (defined(_NORMALMAP) || (defined(_PARALLAXMAP) && !defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR))) || defined(_DETAIL)
  13. #define REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR
  14. #endif
  15. // keep this file in sync with LitForwardPass.hlsl
  16. struct Attributes
  17. {
  18. float4 positionOS : POSITION;
  19. float3 normalOS : NORMAL;
  20. float4 tangentOS : TANGENT;
  21. float2 texcoord : TEXCOORD0;
  22. float2 lightmapUV : TEXCOORD1;
  23. UNITY_VERTEX_INPUT_INSTANCE_ID
  24. };
  25. struct Varyings
  26. {
  27. float2 uv : TEXCOORD0;
  28. DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);
  29. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  30. float3 positionWS : TEXCOORD2;
  31. #endif
  32. float3 normalWS : TEXCOORD3;
  33. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
  34. float4 tangentWS : TEXCOORD4; // xyz: tangent, w: sign
  35. #endif
  36. float3 viewDirWS : TEXCOORD5;
  37. half3 vertexLighting : TEXCOORD6; // xyz: vertex lighting
  38. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  39. float4 shadowCoord : TEXCOORD7;
  40. #endif
  41. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  42. float3 viewDirTS : TEXCOORD8;
  43. #endif
  44. float4 positionCS : SV_POSITION;
  45. UNITY_VERTEX_INPUT_INSTANCE_ID
  46. UNITY_VERTEX_OUTPUT_STEREO
  47. };
  48. void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
  49. {
  50. inputData = (InputData)0;
  51. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  52. inputData.positionWS = input.positionWS;
  53. #endif
  54. half3 viewDirWS = SafeNormalize(input.viewDirWS);
  55. #if defined(_NORMALMAP) || defined(_DETAIL)
  56. float sgn = input.tangentWS.w; // should be either +1 or -1
  57. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  58. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz));
  59. #else
  60. inputData.normalWS = input.normalWS;
  61. #endif
  62. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  63. inputData.viewDirectionWS = viewDirWS;
  64. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  65. inputData.shadowCoord = input.shadowCoord;
  66. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  67. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  68. #else
  69. inputData.shadowCoord = float4(0, 0, 0, 0);
  70. #endif
  71. inputData.fogCoord = 0.0; // we don't apply fog in the guffer pass
  72. inputData.vertexLighting = input.vertexLighting.xyz;
  73. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
  74. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  75. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  76. }
  77. ///////////////////////////////////////////////////////////////////////////////
  78. // Vertex and Fragment functions //
  79. ///////////////////////////////////////////////////////////////////////////////
  80. // Used in Standard (Physically Based) shader
  81. Varyings LitGBufferPassVertex(Attributes input)
  82. {
  83. Varyings output = (Varyings)0;
  84. UNITY_SETUP_INSTANCE_ID(input);
  85. UNITY_TRANSFER_INSTANCE_ID(input, output);
  86. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  87. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  88. // normalWS and tangentWS already normalize.
  89. // this is required to avoid skewing the direction during interpolation
  90. // also required for per-vertex lighting and SH evaluation
  91. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  92. half3 viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS);
  93. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  94. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  95. // already normalized from normal transform to WS.
  96. output.normalWS = normalInput.normalWS;
  97. output.viewDirWS = viewDirWS;
  98. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR) || defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  99. real sign = input.tangentOS.w * GetOddNegativeScale();
  100. half4 tangentWS = half4(normalInput.tangentWS.xyz, sign);
  101. #endif
  102. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
  103. output.tangentWS = tangentWS;
  104. #endif
  105. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  106. half3 viewDirTS = GetViewDirectionTangentSpace(tangentWS, output.normalWS, viewDirWS);
  107. output.viewDirTS = viewDirTS;
  108. #endif
  109. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  110. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  111. output.vertexLighting = vertexLight;
  112. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  113. output.positionWS = vertexInput.positionWS;
  114. #endif
  115. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  116. output.shadowCoord = GetShadowCoord(vertexInput);
  117. #endif
  118. output.positionCS = vertexInput.positionCS;
  119. return output;
  120. }
  121. // Used in Standard (Physically Based) shader
  122. FragmentOutput LitGBufferPassFragment(Varyings input)
  123. {
  124. UNITY_SETUP_INSTANCE_ID(input);
  125. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  126. #if defined(_PARALLAXMAP)
  127. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  128. half3 viewDirTS = input.viewDirTS;
  129. #else
  130. half3 viewDirTS = GetViewDirectionTangentSpace(input.tangentWS, input.normalWS, input.viewDirWS);
  131. #endif
  132. ApplyPerPixelDisplacement(viewDirTS, input.uv);
  133. #endif
  134. SurfaceData surfaceData;
  135. InitializeStandardLitSurfaceData(input.uv, surfaceData);
  136. InputData inputData;
  137. InitializeInputData(input, surfaceData.normalTS, inputData);
  138. // Stripped down version of UniversalFragmentPBR().
  139. #ifdef _SPECULARHIGHLIGHTS_OFF
  140. bool specularHighlightsOff = true;
  141. #else
  142. bool specularHighlightsOff = false;
  143. #endif
  144. // in LitForwardPass GlobalIllumination (and temporarily LightingPhysicallyBased) are called inside UniversalFragmentPBR
  145. // in Deferred rendering we store the sum of these values (and of emission as well) in the GBuffer
  146. BRDFData brdfData;
  147. InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
  148. half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceData.occlusion, inputData.normalWS, inputData.viewDirectionWS);
  149. return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission + color);
  150. }
  151. #endif