暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SimpleLitForwardPass.hlsl 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #if defined(LOD_FADE_CROSSFADE)
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl"
  6. #endif
  7. struct Attributes
  8. {
  9. float4 positionOS : POSITION;
  10. float3 normalOS : NORMAL;
  11. float4 tangentOS : TANGENT;
  12. float2 texcoord : TEXCOORD0;
  13. float2 staticLightmapUV : TEXCOORD1;
  14. float2 dynamicLightmapUV : TEXCOORD2;
  15. UNITY_VERTEX_INPUT_INSTANCE_ID
  16. };
  17. struct Varyings
  18. {
  19. float2 uv : TEXCOORD0;
  20. float3 positionWS : TEXCOORD1; // xyz: posWS
  21. #ifdef _NORMALMAP
  22. half4 normalWS : TEXCOORD2; // xyz: normal, w: viewDir.x
  23. half4 tangentWS : TEXCOORD3; // xyz: tangent, w: viewDir.y
  24. half4 bitangentWS : TEXCOORD4; // xyz: bitangent, w: viewDir.z
  25. #else
  26. half3 normalWS : TEXCOORD2;
  27. #endif
  28. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  29. half4 fogFactorAndVertexLight : TEXCOORD5; // x: fogFactor, yzw: vertex light
  30. #else
  31. half fogFactor : TEXCOORD5;
  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.positionWS;
  51. #if defined(DEBUG_DISPLAY)
  52. inputData.positionCS = input.positionCS;
  53. #endif
  54. #ifdef _NORMALMAP
  55. half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  56. inputData.tangentToWorld = half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz);
  57. inputData.normalWS = TransformTangentToWorld(normalTS, inputData.tangentToWorld);
  58. #else
  59. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(inputData.positionWS);
  60. inputData.normalWS = input.normalWS;
  61. #endif
  62. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  63. viewDirWS = SafeNormalize(viewDirWS);
  64. inputData.viewDirectionWS = viewDirWS;
  65. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  66. inputData.shadowCoord = input.shadowCoord;
  67. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  68. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  69. #else
  70. inputData.shadowCoord = float4(0, 0, 0, 0);
  71. #endif
  72. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  73. inputData.fogCoord = InitializeInputDataFog(float4(inputData.positionWS, 1.0), input.fogFactorAndVertexLight.x);
  74. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  75. #else
  76. inputData.fogCoord = InitializeInputDataFog(float4(inputData.positionWS, 1.0), input.fogFactor);
  77. inputData.vertexLighting = half3(0, 0, 0);
  78. #endif
  79. #if defined(DYNAMICLIGHTMAP_ON)
  80. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  81. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  82. #elif !defined(LIGHTMAP_ON) && (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
  83. inputData.bakedGI = SAMPLE_GI(input.vertexSH,
  84. GetAbsolutePositionWS(inputData.positionWS),
  85. inputData.normalWS,
  86. inputData.viewDirectionWS,
  87. input.positionCS.xy,
  88. input.probeOcclusion,
  89. inputData.shadowMask);
  90. #else
  91. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  92. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  93. #endif
  94. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  95. #if defined(DEBUG_DISPLAY)
  96. #if defined(DYNAMICLIGHTMAP_ON)
  97. inputData.dynamicLightmapUV = input.dynamicLightmapUV.xy;
  98. #endif
  99. #if defined(LIGHTMAP_ON)
  100. inputData.staticLightmapUV = input.staticLightmapUV;
  101. #else
  102. inputData.vertexSH = input.vertexSH;
  103. #endif
  104. #endif
  105. }
  106. ///////////////////////////////////////////////////////////////////////////////
  107. // Vertex and Fragment functions //
  108. ///////////////////////////////////////////////////////////////////////////////
  109. // Used in Standard (Simple Lighting) shader
  110. Varyings LitPassVertexSimple(Attributes input)
  111. {
  112. Varyings output = (Varyings)0;
  113. UNITY_SETUP_INSTANCE_ID(input);
  114. UNITY_TRANSFER_INSTANCE_ID(input, output);
  115. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  116. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  117. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  118. #if defined(_FOG_FRAGMENT)
  119. half fogFactor = 0;
  120. #else
  121. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  122. #endif
  123. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  124. output.positionWS.xyz = vertexInput.positionWS;
  125. output.positionCS = vertexInput.positionCS;
  126. #ifdef _NORMALMAP
  127. half3 viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS);
  128. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  129. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  130. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  131. #else
  132. output.normalWS = NormalizeNormalPerVertex(normalInput.normalWS);
  133. #endif
  134. OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  135. #ifdef DYNAMICLIGHTMAP_ON
  136. output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  137. #endif
  138. OUTPUT_SH4(vertexInput.positionWS, output.normalWS.xyz, GetWorldSpaceNormalizeViewDir(vertexInput.positionWS), output.vertexSH, output.probeOcclusion);
  139. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  140. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  141. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  142. #else
  143. output.fogFactor = fogFactor;
  144. #endif
  145. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  146. output.shadowCoord = GetShadowCoord(vertexInput);
  147. #endif
  148. return output;
  149. }
  150. // Used for StandardSimpleLighting shader
  151. void LitPassFragmentSimple(
  152. Varyings input
  153. , out half4 outColor : SV_Target0
  154. #ifdef _WRITE_RENDERING_LAYERS
  155. , out float4 outRenderingLayers : SV_Target1
  156. #endif
  157. )
  158. {
  159. UNITY_SETUP_INSTANCE_ID(input);
  160. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  161. SurfaceData surfaceData;
  162. InitializeSimpleLitSurfaceData(input.uv, surfaceData);
  163. #ifdef LOD_FADE_CROSSFADE
  164. LODFadeCrossFade(input.positionCS);
  165. #endif
  166. InputData inputData;
  167. InitializeInputData(input, surfaceData.normalTS, inputData);
  168. SETUP_DEBUG_TEXTURE_DATA(inputData, UNDO_TRANSFORM_TEX(input.uv, _BaseMap));
  169. #ifdef _DBUFFER
  170. ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
  171. #endif
  172. half4 color = UniversalFragmentBlinnPhong(inputData, surfaceData);
  173. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  174. color.a = OutputAlpha(color.a, IsSurfaceTypeTransparent(_Surface));
  175. outColor = color;
  176. #ifdef _WRITE_RENDERING_LAYERS
  177. uint renderingLayers = GetMeshRenderingLayer();
  178. outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
  179. #endif
  180. }
  181. #endif