No Description
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.

LitForwardPass.hlsl 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef UNIVERSAL_FORWARD_LIT_PASS_INCLUDED
  2. #define UNIVERSAL_FORWARD_LIT_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(_PARALLAXMAP)
  8. #define REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR
  9. #endif
  10. #if (defined(_NORMALMAP) || (defined(_PARALLAXMAP) && !defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR))) || defined(_DETAIL)
  11. #define REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR
  12. #endif
  13. // keep this file in sync with LitGBufferPass.hlsl
  14. struct Attributes
  15. {
  16. float4 positionOS : POSITION;
  17. float3 normalOS : NORMAL;
  18. float4 tangentOS : TANGENT;
  19. float2 texcoord : TEXCOORD0;
  20. float2 staticLightmapUV : TEXCOORD1;
  21. float2 dynamicLightmapUV : TEXCOORD2;
  22. UNITY_VERTEX_INPUT_INSTANCE_ID
  23. };
  24. struct Varyings
  25. {
  26. float2 uv : TEXCOORD0;
  27. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  28. float3 positionWS : TEXCOORD1;
  29. #endif
  30. float3 normalWS : TEXCOORD2;
  31. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
  32. half4 tangentWS : TEXCOORD3; // xyz: tangent, w: sign
  33. #endif
  34. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  35. half4 fogFactorAndVertexLight : TEXCOORD5; // x: fogFactor, yzw: vertex light
  36. #else
  37. half fogFactor : TEXCOORD5;
  38. #endif
  39. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  40. float4 shadowCoord : TEXCOORD6;
  41. #endif
  42. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  43. half3 viewDirTS : TEXCOORD7;
  44. #endif
  45. DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 8);
  46. #ifdef DYNAMICLIGHTMAP_ON
  47. float2 dynamicLightmapUV : TEXCOORD9; // Dynamic lightmap UVs
  48. #endif
  49. #ifdef USE_APV_PROBE_OCCLUSION
  50. float4 probeOcclusion : TEXCOORD10;
  51. #endif
  52. float4 positionCS : SV_POSITION;
  53. UNITY_VERTEX_INPUT_INSTANCE_ID
  54. UNITY_VERTEX_OUTPUT_STEREO
  55. };
  56. void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
  57. {
  58. inputData = (InputData)0;
  59. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  60. inputData.positionWS = input.positionWS;
  61. #endif
  62. #if defined(DEBUG_DISPLAY)
  63. inputData.positionCS = input.positionCS;
  64. #endif
  65. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
  66. #if defined(_NORMALMAP) || defined(_DETAIL)
  67. float sgn = input.tangentWS.w; // should be either +1 or -1
  68. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  69. half3x3 tangentToWorld = half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz);
  70. #if defined(_NORMALMAP)
  71. inputData.tangentToWorld = tangentToWorld;
  72. #endif
  73. inputData.normalWS = TransformTangentToWorld(normalTS, tangentToWorld);
  74. #else
  75. inputData.normalWS = input.normalWS;
  76. #endif
  77. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  78. inputData.viewDirectionWS = viewDirWS;
  79. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  80. inputData.shadowCoord = input.shadowCoord;
  81. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  82. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  83. #else
  84. inputData.shadowCoord = float4(0, 0, 0, 0);
  85. #endif
  86. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  87. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactorAndVertexLight.x);
  88. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  89. #else
  90. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactor);
  91. #endif
  92. #if defined(DYNAMICLIGHTMAP_ON)
  93. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  94. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  95. #elif !defined(LIGHTMAP_ON) && (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
  96. inputData.bakedGI = SAMPLE_GI(input.vertexSH,
  97. GetAbsolutePositionWS(inputData.positionWS),
  98. inputData.normalWS,
  99. inputData.viewDirectionWS,
  100. input.positionCS.xy,
  101. input.probeOcclusion,
  102. inputData.shadowMask);
  103. #else
  104. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  105. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  106. #endif
  107. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  108. #if defined(DEBUG_DISPLAY)
  109. #if defined(DYNAMICLIGHTMAP_ON)
  110. inputData.dynamicLightmapUV = input.dynamicLightmapUV;
  111. #endif
  112. #if defined(LIGHTMAP_ON)
  113. inputData.staticLightmapUV = input.staticLightmapUV;
  114. #else
  115. inputData.vertexSH = input.vertexSH;
  116. #endif
  117. #endif
  118. }
  119. ///////////////////////////////////////////////////////////////////////////////
  120. // Vertex and Fragment functions //
  121. ///////////////////////////////////////////////////////////////////////////////
  122. // Used in Standard (Physically Based) shader
  123. Varyings LitPassVertex(Attributes input)
  124. {
  125. Varyings output = (Varyings)0;
  126. UNITY_SETUP_INSTANCE_ID(input);
  127. UNITY_TRANSFER_INSTANCE_ID(input, output);
  128. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  129. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  130. // normalWS and tangentWS already normalize.
  131. // this is required to avoid skewing the direction during interpolation
  132. // also required for per-vertex lighting and SH evaluation
  133. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  134. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  135. half fogFactor = 0;
  136. #if !defined(_FOG_FRAGMENT)
  137. fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  138. #endif
  139. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  140. // already normalized from normal transform to WS.
  141. output.normalWS = normalInput.normalWS;
  142. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR) || defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  143. real sign = input.tangentOS.w * GetOddNegativeScale();
  144. half4 tangentWS = half4(normalInput.tangentWS.xyz, sign);
  145. #endif
  146. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
  147. output.tangentWS = tangentWS;
  148. #endif
  149. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  150. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  151. half3 viewDirTS = GetViewDirectionTangentSpace(tangentWS, output.normalWS, viewDirWS);
  152. output.viewDirTS = viewDirTS;
  153. #endif
  154. OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  155. #ifdef DYNAMICLIGHTMAP_ON
  156. output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  157. #endif
  158. OUTPUT_SH4(vertexInput.positionWS, output.normalWS.xyz, GetWorldSpaceNormalizeViewDir(vertexInput.positionWS), output.vertexSH, output.probeOcclusion);
  159. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  160. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  161. #else
  162. output.fogFactor = fogFactor;
  163. #endif
  164. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  165. output.positionWS = vertexInput.positionWS;
  166. #endif
  167. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  168. output.shadowCoord = GetShadowCoord(vertexInput);
  169. #endif
  170. output.positionCS = vertexInput.positionCS;
  171. return output;
  172. }
  173. // Used in Standard (Physically Based) shader
  174. void LitPassFragment(
  175. Varyings input
  176. , out half4 outColor : SV_Target0
  177. #ifdef _WRITE_RENDERING_LAYERS
  178. , out float4 outRenderingLayers : SV_Target1
  179. #endif
  180. )
  181. {
  182. UNITY_SETUP_INSTANCE_ID(input);
  183. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  184. #if defined(_PARALLAXMAP)
  185. #if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  186. half3 viewDirTS = input.viewDirTS;
  187. #else
  188. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
  189. half3 viewDirTS = GetViewDirectionTangentSpace(input.tangentWS, input.normalWS, viewDirWS);
  190. #endif
  191. ApplyPerPixelDisplacement(viewDirTS, input.uv);
  192. #endif
  193. SurfaceData surfaceData;
  194. InitializeStandardLitSurfaceData(input.uv, surfaceData);
  195. #ifdef LOD_FADE_CROSSFADE
  196. LODFadeCrossFade(input.positionCS);
  197. #endif
  198. InputData inputData;
  199. InitializeInputData(input, surfaceData.normalTS, inputData);
  200. SETUP_DEBUG_TEXTURE_DATA(inputData, UNDO_TRANSFORM_TEX(input.uv, _BaseMap));
  201. #ifdef _DBUFFER
  202. ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
  203. #endif
  204. half4 color = UniversalFragmentPBR(inputData, surfaceData);
  205. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  206. color.a = OutputAlpha(color.a, IsSurfaceTypeTransparent(_Surface));
  207. outColor = color;
  208. #ifdef _WRITE_RENDERING_LAYERS
  209. uint renderingLayers = GetMeshRenderingLayer();
  210. outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
  211. #endif
  212. }
  213. #endif