TerrainDetailLitPasses.hlsl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  2. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityGBuffer.hlsl"
  4. struct Attributes
  5. {
  6. float4 PositionOS : POSITION;
  7. float2 UV0 : TEXCOORD0;
  8. float2 UV1 : TEXCOORD1;
  9. float3 NormalOS : NORMAL;
  10. half4 Color : COLOR;
  11. UNITY_VERTEX_INPUT_INSTANCE_ID
  12. };
  13. struct Varyings
  14. {
  15. float2 UV01 : TEXCOORD0; // UV0
  16. DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 1);
  17. half4 Color : TEXCOORD2; // Vertex Color
  18. half4 LightingFog : TEXCOORD3; // Vertex Lighting, Fog Factor
  19. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  20. float4 ShadowCoords : TEXCOORD4; // Shadow UVs
  21. #endif
  22. half4 NormalWS : TEXCOORD5;
  23. float3 PositionWS : TEXCOORD6;
  24. float4 PositionCS : SV_POSITION; // Clip Position
  25. UNITY_VERTEX_INPUT_INSTANCE_ID
  26. UNITY_VERTEX_OUTPUT_STEREO
  27. };
  28. void InitializeInputData(Varyings input, out InputData inputData)
  29. {
  30. inputData = (InputData)0;
  31. inputData.positionCS = input.PositionCS;
  32. inputData.normalWS = half3(0, 1, 0);
  33. inputData.viewDirectionWS = half3(0, 0, 1);
  34. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  35. inputData.shadowCoord = input.ShadowCoords;
  36. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  37. inputData.shadowCoord = TransformWorldToShadowCoord(input.PositionWS);
  38. #else
  39. inputData.shadowCoord = float4(0, 0, 0, 0);
  40. #endif
  41. inputData.fogCoord = input.LightingFog.a;
  42. inputData.vertexLighting = input.LightingFog.rgb;
  43. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, input.NormalWS.xyz);
  44. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.PositionCS);
  45. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  46. inputData.positionWS = input.PositionWS;
  47. #if defined(DEBUG_DISPLAY)
  48. inputData.uv = input.UV01;
  49. #endif
  50. }
  51. void InitializeSurfaceData(half3 albedo, half alpha, out SurfaceData surfaceData)
  52. {
  53. surfaceData = (SurfaceData)0;
  54. surfaceData.albedo = albedo;
  55. surfaceData.alpha = alpha;
  56. surfaceData.emission = half3(0, 0, 0);
  57. surfaceData.metallic = 0;
  58. surfaceData.occlusion = 0;
  59. surfaceData.smoothness = 1;
  60. surfaceData.specular = half3(0, 0, 0);
  61. surfaceData.clearCoatMask = 0;
  62. surfaceData.clearCoatSmoothness = 1;
  63. surfaceData.normalTS = half3(0, 0, 1);
  64. }
  65. half4 UniversalTerrainLit(InputData inputData, SurfaceData surfaceData)
  66. {
  67. #if defined(DEBUG_DISPLAY)
  68. half4 debugColor;
  69. if (CanDebugOverrideOutputColor(inputData, surfaceData, debugColor))
  70. {
  71. return debugColor;
  72. }
  73. #endif
  74. #if defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  75. half3 lighting = inputData.vertexLighting * MainLightRealtimeShadow(inputData.shadowCoord);
  76. #else
  77. half3 lighting = inputData.vertexLighting;
  78. #endif
  79. half4 color = half4(surfaceData.albedo, surfaceData.alpha);
  80. if (IsLightingFeatureEnabled(DEBUGLIGHTINGFEATUREFLAGS_GLOBAL_ILLUMINATION))
  81. {
  82. lighting += inputData.bakedGI;
  83. }
  84. color.rgb *= lighting;
  85. return color;
  86. }
  87. half4 UniversalTerrainLit(InputData inputData, half3 albedo, half alpha)
  88. {
  89. SurfaceData surfaceData;
  90. InitializeSurfaceData(albedo, alpha, surfaceData);
  91. return UniversalTerrainLit(inputData, surfaceData);
  92. }
  93. Varyings TerrainLitVertex(Attributes input)
  94. {
  95. Varyings output = (Varyings)0;
  96. UNITY_SETUP_INSTANCE_ID(input);
  97. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  98. // Vertex attributes
  99. output.UV01 = TRANSFORM_TEX(input.UV0, _MainTex);
  100. OUTPUT_LIGHTMAP_UV(input.UV1, unity_LightmapST, output.staticLightmapUV);
  101. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.PositionOS.xyz);
  102. output.Color = input.Color;
  103. output.PositionCS = vertexInput.positionCS;
  104. // Shadow Coords
  105. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  106. output.ShadowCoords = GetShadowCoord(vertexInput);
  107. #endif
  108. // Vertex Lighting
  109. half3 NormalWS = input.NormalOS;
  110. OUTPUT_SH(NormalWS, output.vertexSH);
  111. Light mainLight = GetMainLight();
  112. half3 attenuatedLightColor = mainLight.color * mainLight.distanceAttenuation;
  113. half3 diffuseColor = half3(0, 0, 0);
  114. if (IsLightingFeatureEnabled(DEBUGLIGHTINGFEATUREFLAGS_MAIN_LIGHT))
  115. {
  116. diffuseColor += LightingLambert(attenuatedLightColor, mainLight.direction, NormalWS);
  117. }
  118. #if defined(_ADDITIONAL_LIGHTS) || defined(_ADDITIONAL_LIGHTS_VERTEX)
  119. if (IsLightingFeatureEnabled(DEBUGLIGHTINGFEATUREFLAGS_ADDITIONAL_LIGHTS))
  120. {
  121. int pixelLightCount = GetAdditionalLightsCount();
  122. for (int i = 0; i < pixelLightCount; ++i)
  123. {
  124. Light light = GetAdditionalLight(i, vertexInput.positionWS);
  125. half3 attenuatedLightColor = light.color * light.distanceAttenuation;
  126. diffuseColor += LightingLambert(attenuatedLightColor, light.direction, NormalWS);
  127. }
  128. }
  129. #endif
  130. output.LightingFog.xyz = diffuseColor;
  131. // Fog factor
  132. output.LightingFog.w = ComputeFogFactor(output.PositionCS.z);
  133. output.NormalWS.xyz = NormalWS;
  134. output.PositionWS = vertexInput.positionWS;
  135. return output;
  136. }
  137. half4 TerrainLitForwardFragment(Varyings input) : SV_Target
  138. {
  139. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  140. InputData inputData;
  141. InitializeInputData(input, inputData);
  142. half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.UV01);
  143. half4 color = UniversalTerrainLit(inputData, tex.rgb, tex.a);
  144. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  145. return color;
  146. }
  147. FragmentOutput TerrainLitGBufferFragment(Varyings input)
  148. {
  149. UNITY_SETUP_INSTANCE_ID(input);
  150. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  151. half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.UV01);
  152. InputData inputData;
  153. InitializeInputData(input, inputData);
  154. SurfaceData surfaceData;
  155. InitializeSurfaceData(tex.rgb, tex.a, surfaceData);
  156. half4 color = UniversalTerrainLit(inputData, tex.rgb, tex.a);
  157. return SurfaceDataToGbuffer(surfaceData, inputData, color.rgb, kLightingInvalid);
  158. }