SimpleLitForwardPass.hlsl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef UNIVERSAL_SIMPLE_LIT_PASS_INCLUDED
  2. #define UNIVERSAL_SIMPLE_LIT_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. struct Attributes
  5. {
  6. float4 positionOS : POSITION;
  7. float3 normalOS : NORMAL;
  8. float4 tangentOS : TANGENT;
  9. float2 texcoord : TEXCOORD0;
  10. float2 staticLightmapUV : TEXCOORD1;
  11. float2 dynamicLightmapUV : TEXCOORD2;
  12. UNITY_VERTEX_INPUT_INSTANCE_ID
  13. };
  14. struct Varyings
  15. {
  16. float2 uv : TEXCOORD0;
  17. float3 positionWS : TEXCOORD1; // xyz: posWS
  18. #ifdef _NORMALMAP
  19. half4 normalWS : TEXCOORD2; // xyz: normal, w: viewDir.x
  20. half4 tangentWS : TEXCOORD3; // xyz: tangent, w: viewDir.y
  21. half4 bitangentWS : TEXCOORD4; // xyz: bitangent, w: viewDir.z
  22. #else
  23. half3 normalWS : TEXCOORD2;
  24. #endif
  25. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  26. half4 fogFactorAndVertexLight : TEXCOORD5; // x: fogFactor, yzw: vertex light
  27. #else
  28. half fogFactor : TEXCOORD5;
  29. #endif
  30. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  31. float4 shadowCoord : TEXCOORD6;
  32. #endif
  33. DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 7);
  34. #ifdef DYNAMICLIGHTMAP_ON
  35. float2 dynamicLightmapUV : TEXCOORD8; // Dynamic lightmap UVs
  36. #endif
  37. float4 positionCS : SV_POSITION;
  38. UNITY_VERTEX_INPUT_INSTANCE_ID
  39. UNITY_VERTEX_OUTPUT_STEREO
  40. };
  41. void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
  42. {
  43. inputData = (InputData)0;
  44. inputData.positionWS = input.positionWS;
  45. #ifdef _NORMALMAP
  46. half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  47. inputData.tangentToWorld = half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz);
  48. inputData.normalWS = TransformTangentToWorld(normalTS, inputData.tangentToWorld);
  49. #else
  50. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(inputData.positionWS);
  51. inputData.normalWS = input.normalWS;
  52. #endif
  53. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  54. viewDirWS = SafeNormalize(viewDirWS);
  55. inputData.viewDirectionWS = viewDirWS;
  56. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  57. inputData.shadowCoord = input.shadowCoord;
  58. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  59. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  60. #else
  61. inputData.shadowCoord = float4(0, 0, 0, 0);
  62. #endif
  63. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  64. inputData.fogCoord = InitializeInputDataFog(float4(inputData.positionWS, 1.0), input.fogFactorAndVertexLight.x);
  65. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  66. #else
  67. inputData.fogCoord = InitializeInputDataFog(float4(inputData.positionWS, 1.0), input.fogFactor);
  68. inputData.vertexLighting = half3(0, 0, 0);
  69. #endif
  70. #if defined(DYNAMICLIGHTMAP_ON)
  71. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  72. #else
  73. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  74. #endif
  75. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  76. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  77. #if defined(DEBUG_DISPLAY)
  78. #if defined(DYNAMICLIGHTMAP_ON)
  79. inputData.dynamicLightmapUV = input.dynamicLightmapUV.xy;
  80. #endif
  81. #if defined(LIGHTMAP_ON)
  82. inputData.staticLightmapUV = input.staticLightmapUV;
  83. #else
  84. inputData.vertexSH = input.vertexSH;
  85. #endif
  86. #endif
  87. }
  88. ///////////////////////////////////////////////////////////////////////////////
  89. // Vertex and Fragment functions //
  90. ///////////////////////////////////////////////////////////////////////////////
  91. // Used in Standard (Simple Lighting) shader
  92. Varyings LitPassVertexSimple(Attributes input)
  93. {
  94. Varyings output = (Varyings)0;
  95. UNITY_SETUP_INSTANCE_ID(input);
  96. UNITY_TRANSFER_INSTANCE_ID(input, output);
  97. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  98. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  99. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  100. #if defined(_FOG_FRAGMENT)
  101. half fogFactor = 0;
  102. #else
  103. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  104. #endif
  105. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  106. output.positionWS.xyz = vertexInput.positionWS;
  107. output.positionCS = vertexInput.positionCS;
  108. #ifdef _NORMALMAP
  109. half3 viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS);
  110. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  111. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  112. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  113. #else
  114. output.normalWS = NormalizeNormalPerVertex(normalInput.normalWS);
  115. #endif
  116. OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  117. #ifdef DYNAMICLIGHTMAP_ON
  118. output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  119. #endif
  120. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  121. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  122. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  123. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  124. #else
  125. output.fogFactor = fogFactor;
  126. #endif
  127. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  128. output.shadowCoord = GetShadowCoord(vertexInput);
  129. #endif
  130. return output;
  131. }
  132. // Used for StandardSimpleLighting shader
  133. half4 LitPassFragmentSimple(Varyings input) : SV_Target
  134. {
  135. UNITY_SETUP_INSTANCE_ID(input);
  136. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  137. SurfaceData surfaceData;
  138. InitializeSimpleLitSurfaceData(input.uv, surfaceData);
  139. InputData inputData;
  140. InitializeInputData(input, surfaceData.normalTS, inputData);
  141. SETUP_DEBUG_TEXTURE_DATA(inputData, input.uv, _BaseMap);
  142. #ifdef _DBUFFER
  143. ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
  144. #endif
  145. half4 color = UniversalFragmentBlinnPhong(inputData, surfaceData);
  146. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  147. color.a = OutputAlpha(color.a, _Surface);
  148. return color;
  149. }
  150. #endif