暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomLighting.hlsl 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #ifndef CUSTOM_LIGHTING
  2. #define CUSTOM_LIGHTING
  3. void MainLight_float(float3 worldPos, out float3 direction, out float3 color, out float shadowAtten)
  4. {
  5. #ifdef SHADERGRAPH_PREVIEW
  6. direction = normalize(float3(-0.5,0.5,-0.5));
  7. color = float3(1,1,1);
  8. shadowAtten = 1;
  9. #else
  10. #if defined(UNIVERSAL_PIPELINE_CORE_INCLUDED)
  11. float4 shadowCoord = TransformWorldToShadowCoord(worldPos);
  12. Light mainLight = GetMainLight(shadowCoord);
  13. direction = mainLight.direction;
  14. color = mainLight.color;
  15. shadowAtten = mainLight.shadowAttenuation;
  16. #else
  17. direction = normalize(float3(-0.5, 0.5, -0.5));
  18. color = float3(1, 1, 1);
  19. shadowAtten = 1;
  20. #endif
  21. #endif
  22. }
  23. void MainLight_half(half3 worldPos, out half3 direction, out half3 color, out half shadowAtten)
  24. {
  25. #ifdef SHADERGRAPH_PREVIEW
  26. direction = normalize(half3(-0.5,0.5,0.5));
  27. color = half3(1,1,1);
  28. shadowAtten = 1;
  29. #else
  30. #if defined(UNIVERSAL_PIPELINE_CORE_INCLUDED)
  31. half4 shadowCoord = TransformWorldToShadowCoord(worldPos);
  32. Light mainLight = GetMainLight(shadowCoord);
  33. direction = mainLight.direction;
  34. color = mainLight.color;
  35. shadowAtten = mainLight.shadowAttenuation;
  36. #else
  37. direction = normalize(float3(-0.5, 0.5, -0.5));
  38. color = float3(1, 1, 1);
  39. shadowAtten = 1;
  40. #endif
  41. #endif
  42. }
  43. #ifndef SHADERGRAPH_PREVIEW
  44. #if defined(UNIVERSAL_PIPELINE_CORE_INCLUDED)
  45. // This function gets additional light data and calculates realtime shadows
  46. Light GetAdditionalLightCustom(int pixelLightIndex, float3 worldPosition) {
  47. // Convert the pixel light index to the light data index
  48. #if USE_FORWARD_PLUS
  49. int lightIndex = pixelLightIndex;
  50. #else
  51. int lightIndex = GetPerObjectLightIndex(pixelLightIndex);
  52. #endif
  53. // Call the URP additional light algorithm. This will not calculate shadows, since we don't pass a shadow mask value
  54. Light light = GetAdditionalPerObjectLight(lightIndex, worldPosition);
  55. // Manually set the shadow attenuation by calculating realtime shadows
  56. light.shadowAttenuation = AdditionalLightRealtimeShadow(lightIndex, worldPosition, light.direction);
  57. return light;
  58. }
  59. #endif
  60. #endif
  61. void AddAdditionalLights_float(float Smoothness, float3 WorldPosition, float3 WorldNormal, float3 WorldView,
  62. float MainDiffuse, float3 MainSpecular, float3 MainColor,
  63. out float Diffuse, out float3 Specular, out float3 Color) {
  64. Diffuse = MainDiffuse;
  65. Specular = MainSpecular;
  66. Color = MainColor * (MainDiffuse + MainSpecular);
  67. #ifndef SHADERGRAPH_PREVIEW
  68. #if defined(UNIVERSAL_PIPELINE_CORE_INCLUDED)
  69. uint pixelLightCount = GetAdditionalLightsCount();
  70. #if USE_FORWARD_PLUS
  71. // for Foward+ LIGHT_LOOP_BEGIN macro uses inputData.normalizedScreenSpaceUV and inputData.positionWS
  72. InputData inputData = (InputData)0;
  73. float4 screenPos = ComputeScreenPos(TransformWorldToHClip(WorldPosition));
  74. inputData.normalizedScreenSpaceUV = screenPos.xy / screenPos.w;
  75. inputData.positionWS = WorldPosition;
  76. #endif
  77. LIGHT_LOOP_BEGIN(pixelLightCount)
  78. Light light = GetAdditionalLightCustom(lightIndex, WorldPosition);
  79. float NdotL = saturate(dot(WorldNormal, light.direction));
  80. float atten = light.distanceAttenuation * light.shadowAttenuation;
  81. float thisDiffuse = atten * NdotL;
  82. float3 thisSpecular = LightingSpecular(thisDiffuse, light.direction, WorldNormal, WorldView, 1, Smoothness);
  83. Diffuse += thisDiffuse;
  84. Specular += thisSpecular;
  85. #if defined(_LIGHT_COOKIES)
  86. float3 cookieColor = SampleAdditionalLightCookie(lightIndex, WorldPosition);
  87. light.color *= cookieColor
  88. #endif
  89. Color += light.color * (thisDiffuse + thisSpecular);
  90. LIGHT_LOOP_END
  91. float total = Diffuse + dot(Specular, float3(0.333, 0.333, 0.333));
  92. Color = total <= 0 ? MainColor : Color / total;
  93. #endif
  94. #endif
  95. }
  96. void AddAdditionalLights_half(half Smoothness, half3 WorldPosition, half3 WorldNormal, half3 WorldView,
  97. half MainDiffuse, half3 MainSpecular, half3 MainColor,
  98. out half Diffuse, out half3 Specular, out half3 Color) {
  99. Diffuse = MainDiffuse;
  100. Specular = MainSpecular;
  101. Color = MainColor * (MainDiffuse + MainSpecular);
  102. #ifndef SHADERGRAPH_PREVIEW
  103. #if defined(UNIVERSAL_PIPELINE_CORE_INCLUDED)
  104. uint pixelLightCount = GetAdditionalLightsCount();
  105. #if USE_FORWARD_PLUS
  106. // for Foward+ LIGHT_LOOP_BEGIN macro uses inputData.normalizedScreenSpaceUV and inputData.positionWS
  107. InputData inputData = (InputData)0;
  108. float4 screenPos = ComputeScreenPos(TransformWorldToHClip(WorldPosition));
  109. inputData.normalizedScreenSpaceUV = screenPos.xy / screenPos.w;
  110. inputData.positionWS = WorldPosition;
  111. #endif
  112. LIGHT_LOOP_BEGIN(pixelLightCount)
  113. Light light = GetAdditionalLightCustom(lightIndex, WorldPosition);
  114. half NdotL = saturate(dot(WorldNormal, light.direction));
  115. half atten = light.distanceAttenuation * light.shadowAttenuation;
  116. half thisDiffuse = atten * NdotL;
  117. half3 thisSpecular = LightingSpecular(thisDiffuse * light.color, light.direction, WorldNormal, WorldView, 1, Smoothness);
  118. Diffuse += thisDiffuse;
  119. Specular += thisSpecular;
  120. #if defined(_LIGHT_COOKIES)
  121. half3 cookieColor = SampleAdditionalLightCookie(lightIndex, WorldPosition);
  122. light.color *= cookieColor
  123. #endif
  124. Color += light.color * (thisDiffuse + thisSpecular);
  125. LIGHT_LOOP_END
  126. //needs to be float to avoid precision issues
  127. float total = Diffuse + dot(Specular, half3(0.333, 0.333, 0.333));
  128. Color = total <= 0 ? MainColor : Color / total;
  129. #endif
  130. #endif
  131. }
  132. void AddAdditionalLightsSimple_float(float Smoothness, float3 WorldPosition, float3 WorldNormal, float3 WorldView,
  133. float MainDiffuse, float3 MainSpecular, float3 MainColor,
  134. out float Diffuse, out float3 Specular, out float3 Color) {
  135. Diffuse = MainDiffuse;
  136. Specular = MainSpecular;
  137. Color = MainColor * (MainDiffuse + MainSpecular);
  138. #ifndef SHADERGRAPH_PREVIEW
  139. #if defined(UNIVERSAL_PIPELINE_CORE_INCLUDED)
  140. uint pixelLightCount = GetAdditionalLightsCount();
  141. #if USE_FORWARD_PLUS
  142. // for Foward+ LIGHT_LOOP_BEGIN macro uses inputData.normalizedScreenSpaceUV and inputData.positionWS
  143. InputData inputData = (InputData)0;
  144. float4 screenPos = ComputeScreenPos(TransformWorldToHClip(WorldPosition));
  145. inputData.normalizedScreenSpaceUV = screenPos.xy / screenPos.w;
  146. inputData.positionWS = WorldPosition;
  147. #endif
  148. LIGHT_LOOP_BEGIN(pixelLightCount)
  149. Light light = GetAdditionalLightCustom(lightIndex, WorldPosition);
  150. float NdotL = saturate(dot(WorldNormal, light.direction));
  151. float atten = light.distanceAttenuation * light.shadowAttenuation;
  152. float thisDiffuse = atten * NdotL;
  153. Diffuse += thisDiffuse;
  154. Color += light.color * thisDiffuse;
  155. LIGHT_LOOP_END
  156. float total = Diffuse;
  157. Color = total <= 0 ? MainColor : Color / total;
  158. #endif
  159. #endif
  160. }
  161. void AddAdditionalLightsSimple_half(half Smoothness, half3 WorldPosition, half3 WorldNormal, half3 WorldView,
  162. half MainDiffuse, half3 MainSpecular, half3 MainColor,
  163. out half Diffuse, out half3 Specular, out half3 Color) {
  164. Diffuse = MainDiffuse;
  165. Specular = MainSpecular;
  166. Color = MainColor * (MainDiffuse + MainSpecular);
  167. #ifndef SHADERGRAPH_PREVIEW
  168. #if defined(UNIVERSAL_PIPELINE_CORE_INCLUDED)
  169. uint pixelLightCount = GetAdditionalLightsCount();
  170. #if USE_FORWARD_PLUS
  171. // for Foward+ LIGHT_LOOP_BEGIN macro uses inputData.normalizedScreenSpaceUV and inputData.positionWS
  172. InputData inputData = (InputData)0;
  173. float4 screenPos = ComputeScreenPos(TransformWorldToHClip(WorldPosition));
  174. inputData.normalizedScreenSpaceUV = screenPos.xy / screenPos.w;
  175. inputData.positionWS = WorldPosition;
  176. #endif
  177. LIGHT_LOOP_BEGIN(pixelLightCount)
  178. Light light = GetAdditionalLightCustom(lightIndex, WorldPosition);
  179. half NdotL = saturate(dot(WorldNormal, light.direction));
  180. half atten = light.distanceAttenuation * light.shadowAttenuation;
  181. half thisDiffuse = atten * NdotL;
  182. Diffuse += thisDiffuse;
  183. Color += light.color * thisDiffuse;
  184. LIGHT_LOOP_END
  185. //needs to be float to avoid precision issues
  186. float total = Diffuse;
  187. Color = total <= 0 ? MainColor : Color / total;
  188. #endif
  189. #endif
  190. }
  191. #endif