Guest User

Custom Lightning Unity URP for Shader Graph 10.5.1

a guest
Aug 20th, 2021
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. #ifndef CUSTOM_LIGHTING_INCLUDED
  2. #define CUSTOM_LIGHTING_INCLUDED
  3.  
  4. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  5. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  6.  
  7. void MainLight_float(float3 WorldPos, out float3 Direction, out float3 Color, out float DistanceAtten, out float ShadowAtten)
  8. {
  9. #if defined(SHADERGRAPH_PREVIEW)
  10.     Direction = float3(0.5, 0.5, 0);
  11.     Color = 1;
  12.     DistanceAtten = 1;
  13.     ShadowAtten = 1;
  14. #else
  15. #if SHADOWS_SCREEN
  16.     float4 clipPos = TransformWorldToHClip(WorldPos);
  17.     float4 shadowCoord = ComputeScreenPos(clipPos);
  18. #else
  19.     float4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
  20. #endif
  21.     Light mainLight = GetMainLight(shadowCoord);
  22.     Direction = mainLight.direction;
  23.     Color = mainLight.color;
  24.     DistanceAtten = mainLight.distanceAttenuation;
  25.     ShadowAtten = mainLight.shadowAttenuation;
  26. #endif
  27. }
  28.  
  29. void MainLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
  30. {
  31. #if defined(SHADERGRAPH_PREVIEW)
  32.     Direction = half3(0.5, 0.5, 0);
  33.     Color = 1;
  34.     DistanceAtten = 1;
  35.     ShadowAtten = 1;
  36. #else
  37. #if SHADOWS_SCREEN
  38.     half4 clipPos = TransformWorldToHClip(WorldPos);
  39.     half4 shadowCoord = ComputeScreenPos(clipPos);
  40. #else
  41.     half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
  42. #endif
  43.     Light mainLight = GetMainLight(shadowCoord);
  44.     Direction = mainLight.direction;
  45.     Color = mainLight.color;
  46.     DistanceAtten = mainLight.distanceAttenuation;
  47.     ShadowAtten = mainLight.shadowAttenuation;
  48. #endif
  49. }
  50.  
  51. void DirectSpecular_float(float3 Specular, float Smoothness, float3 Direction, float3 Color, float3 WorldNormal, float3 WorldView, out float3 Out)
  52. {
  53. #if defined(SHADERGRAPH_PREVIEW)
  54.     Out = 0;
  55. #else
  56.     Smoothness = exp2(10 * Smoothness + 1);
  57.     WorldNormal = normalize(WorldNormal);
  58.     WorldView = SafeNormalize(WorldView);
  59.     Out = LightingSpecular(Color, Direction, WorldNormal, WorldView, float4(Specular, 0), Smoothness);
  60. #endif
  61. }
  62.  
  63. void DirectSpecular_half(half3 Specular, half Smoothness, half3 Direction, half3 Color, half3 WorldNormal, half3 WorldView, out half3 Out)
  64. {
  65. #if defined(SHADERGRAPH_PREVIEW)
  66.     Out = 0;
  67. #else
  68.     Smoothness = exp2(10 * Smoothness + 1);
  69.     WorldNormal = normalize(WorldNormal);
  70.     WorldView = SafeNormalize(WorldView);
  71.     Out = LightingSpecular(Color, Direction, WorldNormal, WorldView,half4(Specular, 0), Smoothness);
  72. #endif
  73. }
  74.  
  75. void AdditionalLights_float(float3 SpecColor, float Smoothness, float3 WorldPosition, float3 WorldNormal, float3 WorldView, out float3 Diffuse, out float3 Specular)
  76. {
  77.     float3 diffuseColor = 0;
  78.     float3 specularColor = 0;
  79.  
  80. #if !defined(SHADERGRAPH_PREVIEW)
  81.     Smoothness = exp2(10 * Smoothness + 1);
  82.     WorldNormal = normalize(WorldNormal);
  83.     WorldView = SafeNormalize(WorldView);
  84.     int pixelLightCount = GetAdditionalLightsCount();
  85.     for (int i = 0; i < pixelLightCount; ++i)
  86.     {
  87.         Light light = GetAdditionalLight(i, WorldPosition);
  88.         half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
  89.         diffuseColor += LightingLambert(attenuatedLightColor, light.direction, WorldNormal);
  90.         specularColor += LightingSpecular(attenuatedLightColor, light.direction, WorldNormal, WorldView, float4(SpecColor, 0), Smoothness);
  91.     }
  92. #endif
  93.  
  94.     Diffuse = diffuseColor;
  95.     Specular = specularColor;
  96. }
  97.  
  98. void AdditionalLights_half(half3 SpecColor, half Smoothness, half3 WorldPosition, half3 WorldNormal, half3 WorldView, out half3 Diffuse, out half3 Specular)
  99. {
  100.     half3 diffuseColor = 0;
  101.     half3 specularColor = 0;
  102.  
  103. #if !defined(SHADERGRAPH_PREVIEW)
  104.     Smoothness = exp2(10 * Smoothness + 1);
  105.     WorldNormal = normalize(WorldNormal);
  106.     WorldView = SafeNormalize(WorldView);
  107.     int pixelLightCount = GetAdditionalLightsCount();
  108.     for (int i = 0; i < pixelLightCount; ++i)
  109.     {
  110.         Light light = GetAdditionalLight(i, WorldPosition);
  111.         half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
  112.         diffuseColor += LightingLambert(attenuatedLightColor, light.direction, WorldNormal);
  113.         specularColor += LightingSpecular(attenuatedLightColor, light.direction, WorldNormal, WorldView, half4(SpecColor, 0), Smoothness);
  114.     }
  115. #endif
  116.  
  117.     Diffuse = diffuseColor;
  118.     Specular = specularColor;
  119. }
  120.  
  121. #endif
  122.  
Advertisement
Add Comment
Please, Sign In to add comment