설명 없음
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.

LitGBufferPass.hlsl 9.0KB

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