Skip to content

[Shaders] Fix clustered lighting shader error when using anisotropy without clearcoat#18237

Merged
sebavan merged 1 commit intoBabylonJS:masterfrom
Popov72:fix-clustered-anisotropy
Apr 7, 2026
Merged

[Shaders] Fix clustered lighting shader error when using anisotropy without clearcoat#18237
sebavan merged 1 commit intoBabylonJS:masterfrom
Popov72:fix-clustered-anisotropy

Conversation

@Popov72
Copy link
Copy Markdown
Contributor

@Popov72 Popov72 commented Apr 5, 2026

Description

The computeClusteredLighting function in the PBR clustered lighting shader unconditionally accessed clearcoatOut.specularEnvironmentR0 in the ANISOTROPIC code path, but clearcoatOut is only passed as a parameter when CLEARCOAT is defined. This caused a fragment shader compilation error:

FRAGMENT SHADER ERROR: 0:863: 'clearcoatOut' : undeclared identifier

The error occurs when using PBR anisotropy with ClusteredLightContainer and no clearcoat — on both WebGL2 and WebGPU.

Fix

Use the local specularEnvironmentR0 variable instead of reading it from clearcoatOut. This variable (from the pbrBlockReflectance0 include) already contains the correct value: it is overwritten from clearcoatOut when CLEARCOAT is enabled, and retains the base reflectance value otherwise. This is consistent with how the adjacent non-ANISOTROPIC code path already works.

Applied to both GLSL and WGSL shaders.

Repro

Playground #QAD7FB#0

Forum post

https://forum.babylonjs.com/t/anisotropy-with-clusteredlightcontainer-bug/63040

…ithout clearcoat

The computeClusteredLighting function referenced clearcoatOut.specularEnvironmentR0
in the ANISOTROPIC code path, but clearcoatOut is only passed as a parameter when
CLEARCOAT is defined. This caused a shader compilation error when using PBR
anisotropy with ClusteredLightContainer and no clearcoat.

Fixed by using the local specularEnvironmentR0 variable instead, which already
contains the correct value (overwritten from clearcoatOut when clearcoat is
enabled). Applied to both GLSL and WGSL shaders.

Forum: https://forum.babylonjs.com/t/63040

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 5, 2026 17:07
@bjsplat
Copy link
Copy Markdown
Collaborator

bjsplat commented Apr 5, 2026

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a shader compilation error in PBR clustered lighting when ANISOTROPIC is enabled without CLEARCOAT, by avoiding an unconditional reference to clearcoatOut in the anisotropy specular path. This aligns the anisotropic path with the already-correct non-anisotropic path by using specularEnvironmentR0.

Changes:

  • WGSL: Use specularEnvironmentR0 instead of clearcoatOut.specularEnvironmentR0 in the anisotropic clustered specular call.
  • GLSL: Apply the same fix to the clustered lighting include.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/dev/core/src/ShadersWGSL/ShadersInclude/pbrDirectLightingFunctions.fx Fixes clustered anisotropic specular path to use specularEnvironmentR0 so it compiles when CLEARCOAT is off.
packages/dev/core/src/Shaders/ShadersInclude/pbrClusteredLightingFunctions.fx Same fix for GLSL clustered lighting include to prevent clearcoatOut undeclared errors.

@bjsplat
Copy link
Copy Markdown
Collaborator

bjsplat commented Apr 5, 2026

Snapshot stored with reference name:
refs/pull/18237/merge

Test environment:
https://snapshots-cvgtc2eugrd3cgfd.z01.azurefd.net/refs/pull/18237/merge/index.html

To test a playground add it to the URL, for example:

https://snapshots-cvgtc2eugrd3cgfd.z01.azurefd.net/refs/pull/18237/merge/index.html#WGZLGJ#4600

Links to test your changes to core in the published versions of the Babylon tools (does not contain changes you made to the tools themselves):

https://playground.babylonjs.com/?snapshot=refs/pull/18237/merge
https://sandbox.babylonjs.com/?snapshot=refs/pull/18237/merge
https://gui.babylonjs.com/?snapshot=refs/pull/18237/merge
https://nme.babylonjs.com/?snapshot=refs/pull/18237/merge

To test the snapshot in the playground with a playground ID add it after the snapshot query string:

https://playground.babylonjs.com/?snapshot=refs/pull/18237/merge#BCU1XR#0

If you made changes to the sandbox or playground in this PR, additional comments will be generated soon containing links to the dev versions of those tools.

@bjsplat
Copy link
Copy Markdown
Collaborator

bjsplat commented Apr 5, 2026

@bjsplat
Copy link
Copy Markdown
Collaborator

bjsplat commented Apr 5, 2026

@bjsplat
Copy link
Copy Markdown
Collaborator

bjsplat commented Apr 5, 2026

@sebavan sebavan merged commit 8e84ac7 into BabylonJS:master Apr 7, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants