Shader compilation issue with clear coat tint

It might be the same issue as Clear coat breaks shader in ios - #7 by Leon, but I don’t have a way to know for sure.

Either way: in certain situations we are getting a shader compilation error in the console and so far we could not figure out a reason for the error, nor a minimal playground reproduction, but since we’ve been extending shaders in quite a hacky way we assumed we are at fault here.

However, now that we’ve digged into Babylon’s shader code, we suspect it’s a Babylon issue. Since we don’t have a repro playground, we’d appreciate if you could follow along the logic in the following description. The code positions are linked to the 4.1.0-beta26, which is what we currently on.

The error we are dealing with is:

Error: FRAGMENT SHADER ERROR: 0:638: ‘absorption’ : undeclared identifier
ERROR: 0:638: ‘clearCoatNdotVRefract’ : undeclared identifier
ERROR: 0:638: ‘assign’ : l-value required (can’t modify a const)
ERROR: 0:638: ‘=’ : dimension mismatch
ERROR: 0:638: ‘assign’ : cannot convert from ‘highp 3-component vector of float’ to ‘const highp float’
ERROR: 0:639: ‘absorption’ : undeclared identifier
ERROR: 0:640: ‘absorption’ : undeclared identifier

The line in question is

absorption = computeClearCoatLightingAbsorption(
  clearCoatNdotVRefract, preInfo.L, clearCoatNormalW, clearCoatColor,
  clearCoatThickness, clearCoatIntensity);

This line appears in lightFragment.fx and is guarded by ifdefs CLEARCOAT_TINT, CLEARCOAT, PBR and LIGHT.

However, the definition of absorption and clearCoatNdotVRefract is in pbr.fragment.fx and is guarded by CLEARCOAT_TINT, REFLECTION, CLEARCOAT and negative UNLIT.

While we probably can safely assume without looking that pbr.fragment.fx is itself guarded by PBR and probably LIGHT, there is still a missing REFLECTION, so that if there is no reflection texture but a clear coat tint, absorption would be not defined, but referenced, just like the error message says.

We hope we haven’t missed anything obvious in describing the problem and would greately appreciate a fix :slight_smile:

Thanks!

1 Like

@rassie Here’s a repro based on the information you provided: Babylon.js Playground

Going to make a PR, thanks for reporting!

1 Like

Excellent, thank you very much!

I would say in general to really benefit from clear coat, you should use a reflection texture or environment texture: Use a HDR environment (for PBR) - Babylon.js Documentation

The fix will come ASAP.

Fix is in, it will be available in the playground with the next nightly: Fix Clear Coat tint without reflection by sebavan · Pull Request #7662 · BabylonJS/Babylon.js · GitHub

2 Likes

Thanks! We are using HDR environments, we just didn’t need reflection texture. Will test the next beta when it’s released.

You must not use the environment texture, else it would work: the system gets the reflection texture defined directly on the material if available, else it takes the global environment texture defined on the scene. Only if no reflection texture defined on material + scene leads to REFLECTION = false.