diff --git a/com.unity.render-pipelines.universal/CHANGELOG.md b/com.unity.render-pipelines.universal/CHANGELOG.md index a2b8c1ccbe0..2c309b60132 100644 --- a/com.unity.render-pipelines.universal/CHANGELOG.md +++ b/com.unity.render-pipelines.universal/CHANGELOG.md @@ -51,6 +51,7 @@ The version number for this package has increased due to a version update of a r - Converted XR automated tests to use MockHMD. ### Fixed +- Fixed an issue where additional lights would not render with WebGL 1 - Fixed an issue where the 2D Renderer was incorrectly rendering transparency with normal maps on an empty background. - Fixed an issue where Sprites on one Sorting Layer were fully lit even when there's no 2D light targeting that layer. - Fixed an issue where null reference exception was thrown when creating a 2D Renderer Data asset while scripts are compiling. [case 1263040](https://issuetracker.unity3d.com/issues/urp-nullreferenceexception-error-is-thrown-on-creating-2d-renderer-asset) diff --git a/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl b/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl index 5a3d994e4db..306090a157a 100644 --- a/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl +++ b/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl @@ -54,6 +54,28 @@ struct Light half shadowAttenuation; }; +// WebGL1 does not support the variable conditioned for loops used for additional lights +#if !defined(_USE_WEBGL1_LIGHTS) && defined(UNITY_PLATFORM_WEBGL) && !defined(SHADER_API_GLES3) + #define _USE_WEBGL1_LIGHTS 1 + #define _WEBGL1_MAX_LIGHTS 8 +#else + #define _USE_WEBGL1_LIGHTS 0 +#endif + +#if !_USE_WEBGL1_LIGHTS + #define LIGHT_LOOP_BEGIN(lightCount) \ + for (uint lightIndex = 0u; lightIndex < lightCount; ++lightIndex) { + + #define LIGHT_LOOP_END } +#else + // WebGL 1 doesn't support variable for loop conditions + #define LIGHT_LOOP_BEGIN(lightCount) \ + for (int lightIndex = 0; lightIndex < _WEBGL1_MAX_LIGHTS; ++lightIndex) { \ + if (lightIndex >= (int)lightCount) break; + + #define LIGHT_LOOP_END } +#endif + /////////////////////////////////////////////////////////////////////////////// // Attenuation Functions / /////////////////////////////////////////////////////////////////////////////// @@ -802,12 +824,11 @@ half3 VertexLighting(float3 positionWS, half3 normalWS) #ifdef _ADDITIONAL_LIGHTS_VERTEX uint lightsCount = GetAdditionalLightsCount(); - for (uint lightIndex = 0u; lightIndex < lightsCount; ++lightIndex) - { + LIGHT_LOOP_BEGIN(lightsCount) Light light = GetAdditionalLight(lightIndex, positionWS); half3 lightColor = light.color * light.distanceAttenuation; vertexLightColor += LightingLambert(lightColor, light.direction, normalWS); - } + LIGHT_LOOP_END #endif return vertexLightColor; @@ -864,8 +885,7 @@ half4 UniversalFragmentPBR(InputData inputData, SurfaceData surfaceData) #ifdef _ADDITIONAL_LIGHTS uint pixelLightCount = GetAdditionalLightsCount(); - for (uint lightIndex = 0u; lightIndex < pixelLightCount; ++lightIndex) - { + LIGHT_LOOP_BEGIN(pixelLightCount) Light light = GetAdditionalLight(lightIndex, inputData.positionWS, shadowMask); #if defined(_SCREEN_SPACE_OCCLUSION) light.color *= aoFactor.directAmbientOcclusion; @@ -874,7 +894,7 @@ half4 UniversalFragmentPBR(InputData inputData, SurfaceData surfaceData) light, inputData.normalWS, inputData.viewDirectionWS, surfaceData.clearCoatMask, specularHighlightsOff); - } + LIGHT_LOOP_END #endif #ifdef _ADDITIONAL_LIGHTS_VERTEX @@ -929,8 +949,7 @@ half4 UniversalFragmentBlinnPhong(InputData inputData, half3 diffuse, half4 spec #ifdef _ADDITIONAL_LIGHTS uint pixelLightCount = GetAdditionalLightsCount(); - for (uint lightIndex = 0u; lightIndex < pixelLightCount; ++lightIndex) - { + LIGHT_LOOP_BEGIN(pixelLightCount) Light light = GetAdditionalLight(lightIndex, inputData.positionWS, shadowMask); #if defined(_SCREEN_SPACE_OCCLUSION) light.color *= aoFactor.directAmbientOcclusion; @@ -938,7 +957,7 @@ half4 UniversalFragmentBlinnPhong(InputData inputData, half3 diffuse, half4 spec half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation); diffuseColor += LightingLambert(attenuatedLightColor, light.direction, inputData.normalWS); specularColor += LightingSpecular(attenuatedLightColor, light.direction, inputData.normalWS, inputData.viewDirectionWS, specularGloss, smoothness); - } + LIGHT_LOOP_END #endif #ifdef _ADDITIONAL_LIGHTS_VERTEX