Skip to content

Commit

Permalink
Fix light for-loops when running on WebGL1 (#2939)
Browse files Browse the repository at this point in the history
* Fix light for-loops when running on WebGL1

* Updated Changelog.
  • Loading branch information
brendan-duncan authored Jan 11, 2021
1 parent b08fb4f commit 2f5b085
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions com.unity.render-pipelines.universal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 28 additions & 9 deletions com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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 /
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -929,16 +949,15 @@ 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;
#endif
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
Expand Down

0 comments on commit 2f5b085

Please sign in to comment.