LitForwardPass.hlsl 7.7 KB

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