Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for glTF KHR_materials_clearcoat extension #11995

Closed
jjhembd opened this issue May 18, 2024 · 1 comment · Fixed by #12006
Closed

Add support for glTF KHR_materials_clearcoat extension #11995

jjhembd opened this issue May 18, 2024 · 1 comment · Fixed by #12006

Comments

@jjhembd
Copy link
Contributor

jjhembd commented May 18, 2024

Feature

The KHR_materials_clearcoat extension extends the PBR metallic roughness model to enable layering of a clear coating on top of an existing material. This clear coating can provide more realistic rendering of objects such as cars with a two-layered coating, where the color comes from the underlying layer.

CesiumJS does not currently recognize the extension parameters in a glTF. Here is how the clearcoat test model is currently rendered. Note how the first two columns appear identical:
image

Compare to the result from the reference implementation with the extension enabled:
image

@jjhembd
Copy link
Contributor Author

jjhembd commented May 19, 2024

To implement this extension, our PBR shaders will require a fairly significant refactoring.

Current flow

LightingStageFS does the following steps:

  1. Initialize color with the material's diffuse color. But if the model has normals, we then:
  2. Replace color with the result of czm_pbrLighting (in pbrLighting.glsl)
  3. Add a calculated color from ImageBasedLightingStageFS. Both this and the previous step use the same light color / intensity.
  4. Scale down by the material's pre-computed ambient occlusion
  5. Add the material's emissive color
  6. Apply tone mapping

Steps 2 and 3 each compute 3 parts:

  1. Specular component (the product of a "microfacet distribution function" and a geometric attenuation/shadowing factor)
  2. Diffuse component (a simple Lambertian scaling)
  3. Fresnel reflection coefficient (used to scale and combine the specular and diffuse components)

The specular component has different paths for the anisotropic and isotropic cases.

Note that Step 3 is problematic for image-based lighting. If a user supplies environment maps, they probably expect those to be the only light source. Or if the user does want both point and environment lighting, they need to be able to scale them separately.

Step 4 applies the ambient occlusion to both image-based and point lighting. This is different from the reference implementation, which does not apply occlusion to the point lighting specular component.

Problems for clearcoat implementation

The clearcoat is layered on top of the base material. It contributes light via a specular component only. It also contributes a second Fresnel coefficient for use in combining the layers.

The obvious way to implement clearcoat is to re-use the specular and Fresnel calculations in both czm_pbrLighting and ImageBasedLightingFS to compute the clearcoat specular contribution and combine it with the base layer. There are 2 main complications:

  1. The specular and diffuse calculations are currently combined into the same functions. (clearcoat needs specular only)
  2. The clearcoat Fresnel coefficient scales down the emissive color, but the PBR functions do not return this value to LightingStageFS where the emissive color is added.

Possible new code structure

czm_pbrLighting and ImageBasedLightingFS would be split into separate functions to compute specular and diffuse components and Fresnel reflection coefficients.

The refactored LightingStageFS would then:

  1. Compute base layer specular components, for both point lighting and IBL (with separate light colors/intensities). This would call different functions depending on whether the material is anisotropic.
  2. Compute diffuse components (again with separate light colors for point lighting and IBL)
  3. Compute Fresnel reflection coefficients and apply to both specular and diffuse components
  4. Compute clearcoat contribution and Fresnel coefficient
  5. Scale diffuse components and IBL specular components (including clearcoat IBL) by ambient occlusion
  6. Combine all base layer components, add emissive contribution
  7. Scale by clearcoat Fresnel factor
  8. Add clearcoat contribution
  9. Apply tonemapping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant