diff --git a/com.unity.toonshader/Runtime/Integrated/Shaders/UnityToon.shader b/com.unity.toonshader/Runtime/Integrated/Shaders/UnityToon.shader index b0650f391..0aefc07e5 100644 --- a/com.unity.toonshader/Runtime/Integrated/Shaders/UnityToon.shader +++ b/com.unity.toonshader/Runtime/Integrated/Shaders/UnityToon.shader @@ -1205,7 +1205,7 @@ Shader "Toon" { #endif #pragma multi_compile_fragment _ _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3 - #pragma multi_compile_fragment _ _LIGHT_LAYERS + #pragma multi_compile _ _LIGHT_LAYERS #pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE // ------------------------------------- diff --git a/com.unity.toonshader/Runtime/Integrated/Shaders/UnityToonTessellation.shader b/com.unity.toonshader/Runtime/Integrated/Shaders/UnityToonTessellation.shader index 98e3f09f9..67055fab6 100644 --- a/com.unity.toonshader/Runtime/Integrated/Shaders/UnityToonTessellation.shader +++ b/com.unity.toonshader/Runtime/Integrated/Shaders/UnityToonTessellation.shader @@ -1259,7 +1259,7 @@ Shader "Toon(Tessellation)" { #endif #pragma multi_compile_fragment _ _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3 - #pragma multi_compile_fragment _ _LIGHT_LAYERS + #pragma multi_compile _ _LIGHT_LAYERS #pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE // ------------------------------------- diff --git a/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBody.hlsl b/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBody.hlsl index 3414e99f5..d52cac8e4 100644 --- a/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBody.hlsl +++ b/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBody.hlsl @@ -348,7 +348,7 @@ half4 distanceAndSpotAttenuation = _AdditionalLightsBuffer[perObjectLightIndex].attenuation; half4 spotDirection = _AdditionalLightsBuffer[perObjectLightIndex].spotDirection; #ifdef _LIGHT_LAYERS - uint lightLayerMask = _AdditionalLightsBuffer[perObjectLightIndex].layerMask; + uint lightLayerMask = asuint(_AdditionalLightsBuffer[perObjectLightIndex].layerMask); #endif half4 lightOcclusionProbeInfo = _AdditionalLightsBuffer[perObjectLightIndex].occlusionProbeChannels; #else @@ -429,12 +429,19 @@ } +#ifdef _LIGHT_LAYERS + #define INIT_LIGHT_LAYER(utslight) utslight.layerMask = 0 +#else + #define INIT_LIGHT_LAYER(utslight) +#endif + #define INIT_UTSLIGHT(utslight) \ utslight.direction = 0; \ utslight.color = 0; \ utslight.distanceAttenuation = 0; \ utslight.shadowAttenuation = 0; \ - utslight.type = 0 + utslight.type = 0; \ + INIT_LIGHT_LAYER(utslight); int DetermineUTS_MainLightIndex(float3 posW, float4 shadowCoord, float4 positionCS) @@ -444,6 +451,27 @@ int mainLightIndex = MAINLIGHT_NOT_FOUND; UtsLight nextLight = GetUrpMainUtsLight(shadowCoord, positionCS); + +#ifdef _LIGHT_LAYERS + if (IsMatchingLightLayer(nextLight.layerMask, GetMeshRenderingLayer()) && !IsMatchingLightLayer(mainLight.layerMask, GetMeshRenderingLayer()) ) + { + mainLight = nextLight; + mainLightIndex = MAINLIGHT_IS_MAINLIGHT; + } + int lightCount = _AdditionalLightsCount.x; + + for (int ii = 0; ii < lightCount; ++ii) + { + nextLight = GetAdditionalUtsLight(ii, posW, positionCS); + if (IsMatchingLightLayer(nextLight.layerMask, GetMeshRenderingLayer()) && !IsMatchingLightLayer(mainLight.layerMask, GetMeshRenderingLayer()) ) + { + + mainLight = nextLight; + mainLightIndex = ii; + break; + } + } +#else if (nextLight.distanceAttenuation > mainLight.distanceAttenuation && nextLight.type == 0) { mainLight = nextLight; @@ -453,13 +481,14 @@ for (int ii = 0; ii < lightCount; ++ii) { nextLight = GetAdditionalUtsLight(ii, posW, positionCS); - if (nextLight.distanceAttenuation > mainLight.distanceAttenuation && nextLight.type == 0) + if (mainLightIndex == MAINLIGHT_NOT_FOUND ? 1: nextLight.distanceAttenuation > mainLight.distanceAttenuation && nextLight.type == 0 ) { + mainLight = nextLight; mainLightIndex = ii; } } - +#endif return mainLightIndex; } diff --git a/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBodyDoubleShadeWithFeather.hlsl b/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBodyDoubleShadeWithFeather.hlsl index 6e34025fe..7531018a5 100644 --- a/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBodyDoubleShadeWithFeather.hlsl +++ b/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBodyDoubleShadeWithFeather.hlsl @@ -91,7 +91,7 @@ UtsLight mainLight = GetMainUtsLightByID(i.mainLightID, i.posWorld.xyz, inputData.shadowCoord, i.positionCS); #if defined(_LIGHT_LAYERS) - uint meshRenderingLayers = GetMeshRenderingLayer(); + const uint meshRenderingLayers = GetMeshRenderingLayer(); #endif half3 mainLightColor = GetLightColor( @@ -281,7 +281,9 @@ { FORWARD_PLUS_SUBTRACTIVE_LIGHT_CHECK int iLight = lightIndex; - // if (iLight != i.mainLightID) +#if _LIGHT_LAYERS + if (iLight != i.mainLightID) +#endif { float notDirectional = 1.0f; //_WorldSpaceLightPos0.w of the legacy code. diff --git a/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBodyShadingGradeMap.hlsl b/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBodyShadingGradeMap.hlsl index ec0b2651c..e0ec67d76 100644 --- a/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBodyShadingGradeMap.hlsl +++ b/com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBodyShadingGradeMap.hlsl @@ -344,7 +344,9 @@ for (uint loopCounter = 0; loopCounter < min(URP_FP_DIRECTIONAL_LIGHTS_COUNT, MAX_VISIBLE_LIGHTS); loopCounter++) { int iLight = loopCounter; - // if (iLight != i.mainLightID) +#if _LIGHT_LAYERS + if (iLight != i.mainLightID) +#endif { float notDirectional = 1.0f; //_WorldSpaceLightPos0.w of the legacy code. UtsLight additionalLight = GetUrpMainUtsLight(0,0);