LitGBufferPass.hlsl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 staticLightmapUV : TEXCOORD1;
  23. float2 dynamicLightmapUV : TEXCOORD2;
  24. UNITY_VERTEX_INPUT_INSTANCE_ID
  25. };
  26. struct Varyings
  27. {
  28. float2 uv : TEXCOORD0;
  29. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  30. float3 positionWS : TEXCOORD1;
  31. #endif
  32. half3 normalWS : TEXCOORD2;
  33. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
  34. half4 tangentWS : TEXCOORD3; // xyz: tangent, w: sign
  35. #endif
  36. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  37. half3 vertexLighting : TEXCOORD4; // xyz: vertex lighting
  38. #endif
  39. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  40. float4 shadowCoord : TEXCOORD5;
  41. #endif
  42. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  43. half3 viewDirTS : TEXCOORD6;
  44. #endif
  45. DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 7);
  46. #ifdef DYNAMICLIGHTMAP_ON
  47. float2 dynamicLightmapUV : TEXCOORD8; // Dynamic lightmap UVs
  48. #endif
  49. float4 positionCS : SV_POSITION;
  50. UNITY_VERTEX_INPUT_INSTANCE_ID
  51. UNITY_VERTEX_OUTPUT_STEREO
  52. };
  53. void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
  54. {
  55. inputData = (InputData)0;
  56. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  57. inputData.positionWS = input.positionWS;
  58. #endif
  59. inputData.positionCS = input.positionCS;
  60. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
  61. #if defined(_NORMALMAP) || defined(_DETAIL)
  62. float sgn = input.tangentWS.w; // should be either +1 or -1
  63. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  64. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz));
  65. #else
  66. inputData.normalWS = input.normalWS;
  67. #endif
  68. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  69. inputData.viewDirectionWS = viewDirWS;
  70. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  71. inputData.shadowCoord = input.shadowCoord;
  72. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  73. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  74. #else
  75. inputData.shadowCoord = float4(0, 0, 0, 0);
  76. #endif
  77. inputData.fogCoord = 0.0; // we don't apply fog in the guffer pass
  78. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  79. inputData.vertexLighting = input.vertexLighting.xyz;
  80. #else
  81. inputData.vertexLighting = half3(0, 0, 0);
  82. #endif
  83. #if defined(DYNAMICLIGHTMAP_ON)
  84. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  85. #else
  86. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  87. #endif
  88. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  89. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  90. }
  91. ///////////////////////////////////////////////////////////////////////////////
  92. // Vertex and Fragment functions //
  93. ///////////////////////////////////////////////////////////////////////////////
  94. // Used in Standard (Physically Based) shader
  95. Varyings LitGBufferPassVertex(Attributes input)
  96. {
  97. Varyings output = (Varyings)0;
  98. UNITY_SETUP_INSTANCE_ID(input);
  99. UNITY_TRANSFER_INSTANCE_ID(input, output);
  100. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  101. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  102. // normalWS and tangentWS already normalize.
  103. // this is required to avoid skewing the direction during interpolation
  104. // also required for per-vertex lighting and SH evaluation
  105. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  106. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  107. // already normalized from normal transform to WS.
  108. output.normalWS = normalInput.normalWS;
  109. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR) || defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  110. real sign = input.tangentOS.w * GetOddNegativeScale();
  111. half4 tangentWS = half4(normalInput.tangentWS.xyz, sign);
  112. #endif
  113. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
  114. output.tangentWS = tangentWS;
  115. #endif
  116. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  117. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  118. half3 viewDirTS = GetViewDirectionTangentSpace(tangentWS, output.normalWS, viewDirWS);
  119. output.viewDirTS = viewDirTS;
  120. #endif
  121. OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  122. #ifdef DYNAMICLIGHTMAP_ON
  123. output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  124. #endif
  125. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  126. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  127. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  128. output.vertexLighting = vertexLight;
  129. #endif
  130. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  131. output.positionWS = vertexInput.positionWS;
  132. #endif
  133. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  134. output.shadowCoord = GetShadowCoord(vertexInput);
  135. #endif
  136. output.positionCS = vertexInput.positionCS;
  137. return output;
  138. }
  139. // Used in Standard (Physically Based) shader
  140. FragmentOutput LitGBufferPassFragment(Varyings input)
  141. {
  142. UNITY_SETUP_INSTANCE_ID(input);
  143. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  144. #if defined(_PARALLAXMAP)
  145. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  146. half3 viewDirTS = input.viewDirTS;
  147. #else
  148. half3 viewDirTS = GetViewDirectionTangentSpace(input.tangentWS, input.normalWS, input.viewDirWS);
  149. #endif
  150. ApplyPerPixelDisplacement(viewDirTS, input.uv);
  151. #endif
  152. SurfaceData surfaceData;
  153. InitializeStandardLitSurfaceData(input.uv, surfaceData);
  154. InputData inputData;
  155. InitializeInputData(input, surfaceData.normalTS, inputData);
  156. SETUP_DEBUG_TEXTURE_DATA(inputData, input.uv, _BaseMap);
  157. #ifdef _DBUFFER
  158. ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
  159. #endif
  160. // Stripped down version of UniversalFragmentPBR().
  161. // in LitForwardPass GlobalIllumination (and temporarily LightingPhysicallyBased) are called inside UniversalFragmentPBR
  162. // in Deferred rendering we store the sum of these values (and of emission as well) in the GBuffer
  163. BRDFData brdfData;
  164. InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
  165. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
  166. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
  167. half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceData.occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);
  168. return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission + color, surfaceData.occlusion);
  169. }
  170. #endif