説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SimpleLitGBufferPass.hlsl 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #if defined(LOD_FADE_CROSSFADE)
  6. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl"
  7. #endif
  8. // keep this file in sync with LitForwardPass.hlsl
  9. struct Attributes
  10. {
  11. float4 positionOS : POSITION;
  12. float3 normalOS : NORMAL;
  13. float4 tangentOS : TANGENT;
  14. float2 texcoord : TEXCOORD0;
  15. float2 staticLightmapUV : TEXCOORD1;
  16. float2 dynamicLightmapUV : TEXCOORD2;
  17. UNITY_VERTEX_INPUT_INSTANCE_ID
  18. };
  19. struct Varyings
  20. {
  21. float2 uv : TEXCOORD0;
  22. float3 posWS : TEXCOORD1; // xyz: posWS
  23. #ifdef _NORMALMAP
  24. half4 normal : TEXCOORD2; // xyz: normal, w: viewDir.x
  25. half4 tangent : TEXCOORD3; // xyz: tangent, w: viewDir.y
  26. half4 bitangent : TEXCOORD4; // xyz: bitangent, w: viewDir.z
  27. #else
  28. half3 normal : TEXCOORD2;
  29. #endif
  30. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  31. half3 vertexLighting : TEXCOORD5; // xyz: vertex light
  32. #endif
  33. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  34. float4 shadowCoord : TEXCOORD6;
  35. #endif
  36. DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 7);
  37. #ifdef DYNAMICLIGHTMAP_ON
  38. float2 dynamicLightmapUV : TEXCOORD8; // Dynamic lightmap UVs
  39. #endif
  40. #ifdef USE_APV_PROBE_OCCLUSION
  41. float4 probeOcclusion : TEXCOORD9;
  42. #endif
  43. float4 positionCS : SV_POSITION;
  44. UNITY_VERTEX_INPUT_INSTANCE_ID
  45. UNITY_VERTEX_OUTPUT_STEREO
  46. };
  47. void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
  48. {
  49. inputData = (InputData)0;
  50. inputData.positionWS = input.posWS;
  51. inputData.positionCS = input.positionCS;
  52. #ifdef _NORMALMAP
  53. half3 viewDirWS = half3(input.normal.w, input.tangent.w, input.bitangent.w);
  54. inputData.normalWS = TransformTangentToWorld(normalTS,half3x3(input.tangent.xyz, input.bitangent.xyz, input.normal.xyz));
  55. #else
  56. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(inputData.positionWS);
  57. inputData.normalWS = input.normal;
  58. #endif
  59. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  60. viewDirWS = SafeNormalize(viewDirWS);
  61. inputData.viewDirectionWS = viewDirWS;
  62. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  63. inputData.shadowCoord = input.shadowCoord;
  64. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  65. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  66. #else
  67. inputData.shadowCoord = float4(0, 0, 0, 0);
  68. #endif
  69. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  70. inputData.vertexLighting = input.vertexLighting.xyz;
  71. #else
  72. inputData.vertexLighting = half3(0, 0, 0);
  73. #endif
  74. inputData.fogCoord = 0; // we don't apply fog in the gbuffer pass
  75. #if defined(DYNAMICLIGHTMAP_ON)
  76. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  77. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  78. #elif !defined(LIGHTMAP_ON) && (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
  79. inputData.bakedGI = SAMPLE_GI(input.vertexSH,
  80. GetAbsolutePositionWS(inputData.positionWS),
  81. inputData.normalWS,
  82. inputData.viewDirectionWS,
  83. inputData.positionCS.xy,
  84. input.probeOcclusion,
  85. inputData.shadowMask);
  86. #else
  87. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  88. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  89. #endif
  90. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  91. #if defined(DEBUG_DISPLAY)
  92. #if defined(DYNAMICLIGHTMAP_ON)
  93. inputData.dynamicLightmapUV = input.dynamicLightmapUV;
  94. #endif
  95. #if defined(LIGHTMAP_ON)
  96. inputData.staticLightmapUV = input.staticLightmapUV;
  97. #else
  98. inputData.vertexSH = input.vertexSH;
  99. #endif
  100. #endif
  101. }
  102. ///////////////////////////////////////////////////////////////////////////////
  103. // Vertex and Fragment functions //
  104. ///////////////////////////////////////////////////////////////////////////////
  105. // Used in Standard (Simple Lighting) shader
  106. Varyings LitPassVertexSimple(Attributes input)
  107. {
  108. Varyings output = (Varyings)0;
  109. UNITY_SETUP_INSTANCE_ID(input);
  110. UNITY_TRANSFER_INSTANCE_ID(input, output);
  111. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  112. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  113. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  114. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  115. output.posWS.xyz = vertexInput.positionWS;
  116. output.positionCS = vertexInput.positionCS;
  117. #ifdef _NORMALMAP
  118. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  119. output.normal = half4(normalInput.normalWS, viewDirWS.x);
  120. output.tangent = half4(normalInput.tangentWS, viewDirWS.y);
  121. output.bitangent = half4(normalInput.bitangentWS, viewDirWS.z);
  122. #else
  123. output.normal = NormalizeNormalPerVertex(normalInput.normalWS);
  124. #endif
  125. OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  126. #ifdef DYNAMICLIGHTMAP_ON
  127. output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  128. #endif
  129. OUTPUT_SH4(vertexInput.positionWS, output.normal.xyz, GetWorldSpaceNormalizeViewDir(vertexInput.positionWS), output.vertexSH, output.probeOcclusion);
  130. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  131. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  132. output.vertexLighting = vertexLight;
  133. #endif
  134. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  135. output.shadowCoord = GetShadowCoord(vertexInput);
  136. #endif
  137. return output;
  138. }
  139. // Used for StandardSimpleLighting shader
  140. FragmentOutput LitPassFragmentSimple(Varyings input)
  141. {
  142. UNITY_SETUP_INSTANCE_ID(input);
  143. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  144. SurfaceData surfaceData;
  145. InitializeSimpleLitSurfaceData(input.uv, surfaceData);
  146. #ifdef LOD_FADE_CROSSFADE
  147. LODFadeCrossFade(input.positionCS);
  148. #endif
  149. InputData inputData;
  150. InitializeInputData(input, surfaceData.normalTS, inputData);
  151. SETUP_DEBUG_TEXTURE_DATA(inputData, UNDO_TRANSFORM_TEX(input.uv, _BaseMap));
  152. #ifdef _DBUFFER
  153. ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
  154. #endif
  155. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
  156. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
  157. half4 color = half4(inputData.bakedGI * surfaceData.albedo + surfaceData.emission, surfaceData.alpha);
  158. return SurfaceDataToGbuffer(surfaceData, inputData, color.rgb, kLightingSimpleLit);
  159. };
  160. #endif