Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion libraries/bxdf/genglsl/gltf_pbr.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,73 @@
<input name="base" type="BSDF" nodename="sheen_layer" />
</layer>

<!-- Calculate clearcoat_freshnel used by emission -->
<viewdirection name="viewdirection_world" type="vector3">
<input name="space" type="string" value="world" />
</viewdirection>

<dotproduct name="VdotNc" type="float">
<input name="in1" type="vector3" nodename="viewdirection_world" />
<input name="in2" type="vector3" interfacename="clearcoat_normal" />
</dotproduct>

<absval name="absVdotNc" type="float">
<input name="in" type="float" nodename="VdotNc" />
</absval>

<subtract name="sub_abs_VdotNc" type="float">
<input name="in1" type="float" value="1.0" />
<input name="in2" type="float" nodename="absVdotNc" />
</subtract>

<power name="pow_5" type="float">
<input name="in1" type="float" nodename="sub_abs_VdotNc" />
<input name="in2" type="float" value="5" />
</power>

<constant name="f0_clearcoat" type="float">
<input name="value" type="float" value="0.04" />
</constant>

<subtract name="sub_f0_clearcoat" type="float">
<input name="in1" type="float" value="1" />
<input name="in2" type="float" nodename="f0_clearcoat" />
</subtract>

<multiply name="multiply_float" type="float">
<input name="in2" type="float" nodename="sub_f0_clearcoat" />
<input name="in1" type="float" nodename="pow_5" />
</multiply>

<add name="clearcoat_fresnel" type="float">
<input name="in2" type="float" nodename="f0_clearcoat" />
<input name="in1" type="float" nodename="multiply_float" />
</add>

<!-- Emission -->

<multiply name="clearcoat_contribution" type="float">
<input name="in1" type="float" nodename="clearcoat_fresnel" />
<input name="in2" type="float" interfacename="clearcoat" />
</multiply>

<subtract name="clearcoat_emission_scaler" type="float">
<input name="in1" type="float" value="1.0" />
<input name="in2" type="float" nodename="clearcoat_contribution" />
</subtract>

<multiply name="emission_color" type="color3">
<input name="in1" type="color3" interfacename="emissive" />
<input name="in2" type="float" interfacename="emissive_strength" />
</multiply>

<multiply name="coated_emission" type="color3">
<input name="in2" type="float" nodename="clearcoat_emission_scaler" />
<input name="in1" type="color3" nodename="emission_color" />
</multiply>

<uniform_edf name="emission" type="EDF">
<input name="color" type="color3" nodename="emission_color" />
<input name="color" type="color3" nodename="coated_emission" />
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a thoughtful implementation of the Fresnel effect of the coating, though I wonder if we could leverage the standard generalized_schlick_edf node instead:

https://github.com/AcademySoftwareFoundation/MaterialX/blob/main/documents/Specification/MaterialX.PBRSpec.md#node-generalized-schlick-edf

Copy link
Contributor Author

@bowald bowald Oct 24, 2025

Choose a reason for hiding this comment

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

I agree, that was my first idea, and use multiply_edfC with the emission_color or pipe the uniform_edf into the base-input.

But there are some implementation details of generalize_schlick that I think differ from the gltf_spec formula, as well as not allowing me to supply the normal (as it needs to be the clearcoat normal).

This is the fresnel described by the glTF spec, KHR_materials_clearcoat.

clearcoat_fresnel = 0.04 + (1 - 0.04) * (1 - abs(VdotNc))^5

In the mx_generalized_schlick_edf glsl implementation it seems that NdoV is clamped and not the absolute value.

also the mx_fresnel_schlick function in glsl uses mix with the arguments in another order then expected for clearcoat interaction.

float mx_fresnel_schlick(float cosTheta, float F0, float F90, float exponent)
{
    float x = clamp(1.0 - cosTheta, 0.0, 1.0);
    return mix(F0, F90, pow(x, exponent));
}

According to GLSLangSpec.4.40.pdf, page 146, this is the definition of mix :

mix(x,y,a) = x⋅(1−a)+ y⋅a

to be equalent to glTF implementation the mix should be called with

mix(pow(x, exponent), F90, F0) = 0.04 + (1 - 0.04) * (1 - abs(VdotNc))^5

I should say that I havn't tested the code (because I couldn't figure out how to supply the clearcoat normal to the edf func).

Copy link
Contributor Author

@bowald bowald Oct 24, 2025

Choose a reason for hiding this comment

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

Regarding the use of clamp vs abs as in the spec. It seems like the gltf-sample-renderer uses clamp here as well and not abs as in the spec. Code reference. So that may not be an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also realized that there is a Standard Schlick Fresnel in the mx_microfacet lib. That is what I'm after, it will sort out the "mix"-problem I wrote about in an earlier reply. But I cannot see a way to access it from generalized_schlick_edf, is that possible?

Copy link
Member

Choose a reason for hiding this comment

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

All good points, @bowald, and I'm CC'ing @niklasharrysson for his expertise with the definition and implementation of generalized_schlick_edf.

Our original goal for the generalized_schlick_edf node was to handle the very situation you've encountered here, where the shape of an EDF lobe is influenced by the clearcoat layer above it, so if there are limitations in our current definition I'm hoping we can address them to encompass the needs of gltf_pbr as well.

As a second reference point, here's where we use generalized_schlick_edf in the definition of standard_surface to handle a similar situation:

https://github.com/AcademySoftwareFoundation/MaterialX/blob/main/libraries/bxdf/standard_surface.mtlx#L399

</uniform_edf>

<!-- Alpha -->
Expand Down
59 changes: 58 additions & 1 deletion libraries/bxdf/gltf_pbr.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,72 @@
<input name="base" type="BSDF" nodename="sheen_layer" />
</layer>

<!-- Calculate clearcoat_freshnel used by emission -->
<viewdirection name="viewdirection_world" type="vector3">
<input name="space" type="string" value="world" />
</viewdirection>

<dotproduct name="VdotNc" type="float">
<input name="in1" type="vector3" nodename="viewdirection_world" />
<input name="in2" type="vector3" interfacename="clearcoat_normal" />
</dotproduct>

<absval name="absVdotNc" type="float">
<input name="in" type="float" nodename="VdotNc" />
</absval>

<subtract name="sub_abs_VdotNc" type="float">
<input name="in1" type="float" value="1.0" />
<input name="in2" type="float" nodename="absVdotNc" />
</subtract>

<power name="pow_5" type="float">
<input name="in1" type="float" nodename="sub_abs_VdotNc" />
<input name="in2" type="float" value="5" />
</power>

<constant name="f0_clearcoat" type="float">
<input name="value" type="float" value="0.04" />
</constant>

<subtract name="sub_f0_clearcoat" type="float">
<input name="in1" type="float" value="1" />
<input name="in2" type="float" nodename="f0_clearcoat" />
</subtract>

<multiply name="multiply_float" type="float">
<input name="in2" type="float" nodename="sub_f0_clearcoat" />
<input name="in1" type="float" nodename="pow_5" />
</multiply>

<add name="clearcoat_fresnel" type="float">
<input name="in2" type="float" nodename="f0_clearcoat" />
<input name="in1" type="float" nodename="multiply_float" />
</add>

<!-- Emission -->
<multiply name="clearcoat_contribution" type="float">
<input name="in1" type="float" nodename="clearcoat_fresnel" />
<input name="in2" type="float" interfacename="clearcoat" />
</multiply>

<subtract name="clearcoat_emission_scaler" type="float">
<input name="in1" type="float" value="1.0" />
<input name="in2" type="float" nodename="clearcoat_contribution" />
</subtract>

<multiply name="emission_color" type="color3">
<input name="in1" type="color3" interfacename="emissive" />
<input name="in2" type="float" interfacename="emissive_strength" />
</multiply>

<multiply name="coated_emission" type="color3">
<input name="in2" type="float" nodename="clearcoat_emission_scaler" />
<input name="in1" type="color3" nodename="emission_color" />
</multiply>

<uniform_edf name="emission" type="EDF">
<input name="color" type="color3" nodename="emission_color" />
<input name="color" type="color3" nodename="coated_emission" />
</uniform_edf>

<!-- Alpha -->
Expand Down
Loading