SimpleLitGBufferPass.hlsl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef UNIVERSAL_SIMPLELIT_GBUFFER_PASS_INCLUDED
  2. #define UNIVERSAL_SIMPLELIT_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. // keep this file in sync with LitForwardPass.hlsl
  6. struct Attributes
  7. {
  8. float4 positionOS : POSITION;
  9. float3 normalOS : NORMAL;
  10. float4 tangentOS : TANGENT;
  11. float2 texcoord : TEXCOORD0;
  12. float2 staticLightmapUV : TEXCOORD1;
  13. float2 dynamicLightmapUV : TEXCOORD2;
  14. UNITY_VERTEX_INPUT_INSTANCE_ID
  15. };
  16. struct Varyings
  17. {
  18. float2 uv : TEXCOORD0;
  19. float3 posWS : TEXCOORD1; // xyz: posWS
  20. #ifdef _NORMALMAP
  21. half4 normal : TEXCOORD2; // xyz: normal, w: viewDir.x
  22. half4 tangent : TEXCOORD3; // xyz: tangent, w: viewDir.y
  23. half4 bitangent : TEXCOORD4; // xyz: bitangent, w: viewDir.z
  24. #else
  25. half3 normal : TEXCOORD2;
  26. #endif
  27. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  28. half3 vertexLighting : TEXCOORD5; // xyz: vertex light
  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.posWS;
  45. inputData.positionCS = input.positionCS;
  46. #ifdef _NORMALMAP
  47. half3 viewDirWS = half3(input.normal.w, input.tangent.w, input.bitangent.w);
  48. inputData.normalWS = TransformTangentToWorld(normalTS,half3x3(input.tangent.xyz, input.bitangent.xyz, input.normal.xyz));
  49. #else
  50. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(inputData.positionWS);
  51. inputData.normalWS = input.normal;
  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.vertexLighting = input.vertexLighting.xyz;
  65. #else
  66. inputData.vertexLighting = half3(0, 0, 0);
  67. #endif
  68. inputData.fogCoord = 0; // we don't apply fog in the gbuffer pass
  69. #if defined(DYNAMICLIGHTMAP_ON)
  70. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  71. #else
  72. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  73. #endif
  74. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  75. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  76. #if defined(DEBUG_DISPLAY)
  77. #if defined(DYNAMICLIGHTMAP_ON)
  78. inputData.dynamicLightmapUV = input.dynamicLightmapUV;
  79. #endif
  80. #if defined(LIGHTMAP_ON)
  81. inputData.staticLightmapUV = input.staticLightmapUV;
  82. #else
  83. inputData.vertexSH = input.vertexSH;
  84. #endif
  85. #endif
  86. }
  87. ///////////////////////////////////////////////////////////////////////////////
  88. // Vertex and Fragment functions //
  89. ///////////////////////////////////////////////////////////////////////////////
  90. // Used in Standard (Simple Lighting) shader
  91. Varyings LitPassVertexSimple(Attributes input)
  92. {
  93. Varyings output = (Varyings)0;
  94. UNITY_SETUP_INSTANCE_ID(input);
  95. UNITY_TRANSFER_INSTANCE_ID(input, output);
  96. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  97. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  98. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  99. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  100. output.posWS.xyz = vertexInput.positionWS;
  101. output.positionCS = vertexInput.positionCS;
  102. #ifdef _NORMALMAP
  103. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  104. output.normal = half4(normalInput.normalWS, viewDirWS.x);
  105. output.tangent = half4(normalInput.tangentWS, viewDirWS.y);
  106. output.bitangent = half4(normalInput.bitangentWS, viewDirWS.z);
  107. #else
  108. output.normal = NormalizeNormalPerVertex(normalInput.normalWS);
  109. #endif
  110. OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  111. #ifdef DYNAMICLIGHTMAP_ON
  112. output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  113. #endif
  114. OUTPUT_SH(output.normal.xyz, output.vertexSH);
  115. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  116. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  117. output.vertexLighting = vertexLight;
  118. #endif
  119. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  120. output.shadowCoord = GetShadowCoord(vertexInput);
  121. #endif
  122. return output;
  123. }
  124. // Used for StandardSimpleLighting shader
  125. FragmentOutput LitPassFragmentSimple(Varyings input)
  126. {
  127. UNITY_SETUP_INSTANCE_ID(input);
  128. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  129. SurfaceData surfaceData;
  130. InitializeSimpleLitSurfaceData(input.uv, surfaceData);
  131. InputData inputData;
  132. InitializeInputData(input, surfaceData.normalTS, inputData);
  133. SETUP_DEBUG_TEXTURE_DATA(inputData, input.uv, _BaseMap);
  134. #ifdef _DBUFFER
  135. ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
  136. #endif
  137. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
  138. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
  139. half4 color = half4(inputData.bakedGI * surfaceData.albedo + surfaceData.emission, surfaceData.alpha);
  140. return SurfaceDataToGbuffer(surfaceData, inputData, color.rgb, kLightingSimpleLit);
  141. };
  142. #endif