Skip to content

Add OpenPBR's base_diffuse_roughness material parameter #16183

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,11 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
#ifdef METALLICWORKFLOW
, surfaceAlbedo
, ${(isWebGPU ? "uniforms." : "") + this._vMetallicReflectanceFactorsName}
#endif
, 0.
#ifdef BASE_DIFFUSE_ROUGHNESS
, 0.
, vec2(0., 0.)
Comment on lines +1021 to +1025
Copy link
Contributor Author

Choose a reason for hiding this comment

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

What should be done here?

#endif
#ifdef REFLECTIVITY
, vec3${state.fSuffix}(0., 0., ${aoIntensity})
Expand Down
74 changes: 68 additions & 6 deletions packages/dev/core/src/Materials/PBR/pbrBaseMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ export class PBRMaterialDefines extends MaterialDefines implements IImageProcess
public ALBEDODIRECTUV = 0;
public VERTEXCOLOR = false;

public BASEWEIGHT = false;
public BASEWEIGHTDIRECTUV = 0;
public BASE_WEIGHT = false;
public BASE_WEIGHTDIRECTUV = 0;
public BASE_DIFFUSE_ROUGHNESS = false;
public BASE_DIFFUSE_ROUGHNESSDIRECTUV = 0;

public BAKED_VERTEX_ANIMATION_TEXTURE = false;

Expand Down Expand Up @@ -422,11 +424,17 @@ export abstract class PBRBaseMaterial extends PushMaterial {
public _albedoTexture: Nullable<BaseTexture> = null;

/**
* OpenPBR Base Weight (multiplier to the diffuse and metal lobes).
* OpenPBR Base Weight texture (multiplier to the diffuse and metal lobes).
* @internal
*/
public _baseWeightTexture: Nullable<BaseTexture> = null;

/**
* OpenPBR Base Diffuse Roughness texture (roughness of the diffuse lobe).
* @internal
*/
public _baseDiffuseRoughnessTexture: Nullable<BaseTexture> = null;

/**
* AKA Occlusion Texture in other nomenclature.
* @internal
Expand Down Expand Up @@ -575,6 +583,13 @@ export abstract class PBRBaseMaterial extends PushMaterial {
*/
public _baseWeight = 1;

/**
* OpenPBR Base Diffuse Roughness (roughness of the diffuse lobe).
* Can also be used to scale the corresponding texture.
* @internal
*/
public _baseDiffuseRoughness: Nullable<number> = null;

/**
* AKA Specular Color in other nomenclature.
* @internal
Expand Down Expand Up @@ -1131,6 +1146,12 @@ export abstract class PBRBaseMaterial extends PushMaterial {
}
}

if (this._baseDiffuseRoughnessTexture && MaterialFlags.BaseDiffuseRoughnessTextureEnabled) {
if (!this._baseDiffuseRoughnessTexture.isReadyOrNotBlocking()) {
return false;
}
}

if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
if (!this._ambientTexture.isReadyOrNotBlocking()) {
return false;
Expand Down Expand Up @@ -1443,6 +1464,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
"vAmbientColor",
"vAlbedoColor",
"baseWeight",
"baseDiffuseRoughness",
"vReflectivityColor",
"vMetallicReflectanceFactors",
"vEmissiveColor",
Expand All @@ -1453,6 +1475,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
"pointSize",
"vAlbedoInfos",
"vBaseWeightInfos",
"vBaseDiffuseRoughnessInfos",
"vAmbientInfos",
"vOpacityInfos",
"vReflectionInfos",
Expand All @@ -1469,6 +1492,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
"mBones",
"albedoMatrix",
"baseWeightMatrix",
"baseDiffuseRoughnessMatrix",
"ambientMatrix",
"opacityMatrix",
"reflectionMatrix",
Expand Down Expand Up @@ -1511,6 +1535,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
const samplers = [
"albedoSampler",
"baseWeightSampler",
"baseDiffuseRoughnessSampler",
"reflectivitySampler",
"ambientSampler",
"emissiveSampler",
Expand Down Expand Up @@ -1647,7 +1672,8 @@ export abstract class PBRBaseMaterial extends PushMaterial {
}
if (scene.texturesEnabled) {
defines.ALBEDODIRECTUV = 0;
defines.BASEWEIGHTDIRECTUV = 0;
defines.BASE_WEIGHTDIRECTUV = 0;
defines.BASE_DIFFUSE_ROUGHNESSDIRECTUV = 0;
defines.AMBIENTDIRECTUV = 0;
defines.OPACITYDIRECTUV = 0;
defines.EMISSIVEDIRECTUV = 0;
Expand All @@ -1670,9 +1696,15 @@ export abstract class PBRBaseMaterial extends PushMaterial {
}

if (this._baseWeightTexture && MaterialFlags.BaseWeightTextureEnabled) {
PrepareDefinesForMergedUV(this._baseWeightTexture, defines, "BASEWEIGHT");
PrepareDefinesForMergedUV(this._baseWeightTexture, defines, "BASE_WEIGHT");
} else {
defines.BASE_WEIGHT = false;
}

if (this._baseDiffuseRoughnessTexture && MaterialFlags.BaseDiffuseRoughnessTextureEnabled) {
PrepareDefinesForMergedUV(this._baseDiffuseRoughnessTexture, defines, "BASE_DIFFUSE_ROUGHNESS");
} else {
defines.BASEWEIGHT = false;
defines.BASE_DIFFUSE_ROUGHNESS = false;
}

if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
Expand Down Expand Up @@ -2024,6 +2056,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
const ubo = this._uniformBuffer;
ubo.addUniform("vAlbedoInfos", 2);
ubo.addUniform("vBaseWeightInfos", 2);
ubo.addUniform("vBaseDiffuseRoughnessInfos", 2);
ubo.addUniform("vAmbientInfos", 4);
ubo.addUniform("vOpacityInfos", 2);
ubo.addUniform("vEmissiveInfos", 2);
Expand All @@ -2037,6 +2070,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
ubo.addUniform("vBumpInfos", 3);
ubo.addUniform("albedoMatrix", 16);
ubo.addUniform("baseWeightMatrix", 16);
ubo.addUniform("baseDiffuseRoughnessMatrix", 16);
ubo.addUniform("ambientMatrix", 16);
ubo.addUniform("opacityMatrix", 16);
ubo.addUniform("emissiveMatrix", 16);
Expand All @@ -2050,6 +2084,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
ubo.addUniform("vReflectionColor", 3);
ubo.addUniform("vAlbedoColor", 4);
ubo.addUniform("baseWeight", 1);
ubo.addUniform("baseDiffuseRoughness", 1);
ubo.addUniform("vLightingIntensity", 4);

ubo.addUniform("vReflectionMicrosurfaceInfos", 3);
Expand Down Expand Up @@ -2157,6 +2192,11 @@ export abstract class PBRBaseMaterial extends PushMaterial {
BindTextureMatrix(this._baseWeightTexture, ubo, "baseWeight");
}

if (this._baseDiffuseRoughnessTexture && MaterialFlags.BaseDiffuseRoughnessTextureEnabled) {
ubo.updateFloat2("vBaseDiffuseRoughnessInfos", this._baseDiffuseRoughnessTexture.coordinatesIndex, this._baseDiffuseRoughnessTexture.level);
BindTextureMatrix(this._baseDiffuseRoughnessTexture, ubo, "baseDiffuseRoughness");
}

if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
ubo.updateFloat4(
"vAmbientInfos",
Expand Down Expand Up @@ -2319,6 +2359,11 @@ export abstract class PBRBaseMaterial extends PushMaterial {
}

ubo.updateFloat("baseWeight", this._baseWeight);
if (this._baseDiffuseRoughness !== null && this._baseDiffuseRoughness !== undefined) {
ubo.updateFloat("baseDiffuseRoughness", this._baseDiffuseRoughness);
Copy link
Member

Choose a reason for hiding this comment

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

ubo.updateFloat("baseDiffuseRoughness", this._baseDiffuseRoughness ?? this._roughness);

} else if (this._roughness !== null && this._roughness !== undefined) {
ubo.updateFloat("baseDiffuseRoughness", this._roughness);
}

// Misc
this._lightingInfos.x = this._directIntensity;
Expand Down Expand Up @@ -2346,6 +2391,10 @@ export abstract class PBRBaseMaterial extends PushMaterial {
ubo.setTexture("baseWeightSampler", this._baseWeightTexture);
}

if (this._baseDiffuseRoughnessTexture && MaterialFlags.BaseDiffuseRoughnessTextureEnabled) {
ubo.setTexture("baseDiffuseRoughnessSampler", this._baseDiffuseRoughnessTexture);
}

if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
ubo.setTexture("ambientSampler", this._ambientTexture);
}
Expand Down Expand Up @@ -2484,6 +2533,10 @@ export abstract class PBRBaseMaterial extends PushMaterial {
results.push(this._baseWeightTexture);
}

if (this._baseDiffuseRoughnessTexture && this._baseDiffuseRoughnessTexture.animations && this._baseDiffuseRoughnessTexture.animations.length > 0) {
results.push(this._baseDiffuseRoughnessTexture);
}

if (this._ambientTexture && this._ambientTexture.animations && this._ambientTexture.animations.length > 0) {
results.push(this._ambientTexture);
}
Expand Down Expand Up @@ -2556,6 +2609,10 @@ export abstract class PBRBaseMaterial extends PushMaterial {
activeTextures.push(this._baseWeightTexture);
}

if (this._baseDiffuseRoughnessTexture) {
activeTextures.push(this._baseDiffuseRoughnessTexture);
}

if (this._ambientTexture) {
activeTextures.push(this._ambientTexture);
}
Expand Down Expand Up @@ -2621,6 +2678,10 @@ export abstract class PBRBaseMaterial extends PushMaterial {
return true;
}

if (this._baseDiffuseRoughnessTexture === texture) {
return true;
}

if (this._ambientTexture === texture) {
return true;
}
Expand Down Expand Up @@ -2701,6 +2762,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {

this._albedoTexture?.dispose();
this._baseWeightTexture?.dispose();
this._baseDiffuseRoughnessTexture?.dispose();
this._ambientTexture?.dispose();
this._opacityTexture?.dispose();
this._reflectionTexture?.dispose();
Expand Down
16 changes: 15 additions & 1 deletion packages/dev/core/src/Materials/PBR/pbrMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,19 @@ export class PBRMaterial extends PBRBaseMaterial {
public albedoTexture: Nullable<BaseTexture>;

/**
* OpenPBR Base Weight (multiplier to the diffuse and metal lobes).
* OpenPBR Base Weight texture (multiplier to the diffuse and metal lobes).
*/
@serializeAsTexture()
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
public baseWeightTexture: Nullable<BaseTexture>;

/**
* OpenPBR Base Diffuse Roughness texture (roughness of the diffuse lobe).
*/
@serializeAsTexture()
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
public baseDiffuseRoughnessTexture: Nullable<BaseTexture>;

/**
* AKA Occlusion Texture in other nomenclature.
*/
Expand Down Expand Up @@ -284,6 +291,13 @@ export class PBRMaterial extends PBRBaseMaterial {
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
public baseWeight = 1;

/**
* OpenPBR Base Diffuse Roughness (roughness of the diffuse lobe).
*/
@serialize("baseDiffuseRoughness")
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
public baseDiffuseRoughness = 1;

/**
* AKA Specular Color in other nomenclature.
*/
Expand Down
16 changes: 16 additions & 0 deletions packages/dev/core/src/Materials/materialFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ export class MaterialFlags {
AbstractEngine.MarkAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag);
}

private static _BaseDiffuseRoughnessTextureEnabled = true;
/**
* Is the OpenPBR Base Diffuse Roughness texture enabled in the application.
*/
public static get BaseDiffuseRoughnessTextureEnabled(): boolean {
return this._BaseDiffuseRoughnessTextureEnabled;
}
public static set BaseDiffuseRoughnessTextureEnabled(value: boolean) {
if (this._BaseDiffuseRoughnessTextureEnabled === value) {
return;
}

this._BaseDiffuseRoughnessTextureEnabled = value;
AbstractEngine.MarkAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag);
}

private static _DetailTextureEnabled = true;
/**
* Are detail textures enabled in the application.
Expand Down
9 changes: 5 additions & 4 deletions packages/dev/core/src/Shaders/ShadersInclude/lightFragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
preInfo.attenuation *= computeDirectionalLightFalloff_IES(light{X}.vLightDirection.xyz, preInfo.L, iesLightTexture{X});
#else
preInfo.attenuation *= computeDirectionalLightFalloff(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightDirection.w, light{X}.vLightData.w, light{X}.vLightFalloff.z, light{X}.vLightFalloff.w);
#endif
#endif
#endif
#elif defined(POINTLIGHT{X})
#ifdef LIGHT_FALLOFF_GLTF{X}
Expand All @@ -74,6 +74,7 @@
#else
preInfo.roughness = adjustRoughnessFromLightProperties(roughness, light{X}.vLightSpecular.a, preInfo.lightDistance);
#endif
preInfo.diffuseRoughness = diffuseRoughness;

#ifdef IRIDESCENCE
preInfo.iridescenceIntensity = iridescenceIntensity;
Expand Down Expand Up @@ -129,7 +130,7 @@
#endif

info.clearCoat = computeClearCoatLighting(preInfo, clearcoatOut.clearCoatNormalW, clearcoatOut.clearCoatAARoughnessFactors.x, clearcoatOut.clearCoatIntensity, diffuse{X}.rgb);

#ifdef CLEARCOAT_TINT
// Absorption
absorption = computeClearCoatLightingAbsorption(clearcoatOut.clearCoatNdotVRefract, preInfo.L, clearcoatOut.clearCoatNormalW, clearcoatOut.clearCoatColor, clearcoatOut.clearCoatThickness, clearcoatOut.clearCoatIntensity);
Expand Down Expand Up @@ -178,7 +179,7 @@

#ifdef SHADOW{X}
#ifdef SHADOWCSM{X}
for (int i = 0; i < SHADOWCSMNUM_CASCADES{X}; i++)
for (int i = 0; i < SHADOWCSMNUM_CASCADES{X}; i++)
{
#ifdef SHADOWCSM_RIGHTHANDED{X}
diff{X} = viewFrustumZ{X}[i] + vPositionFromCamera{X}.z;
Expand Down Expand Up @@ -335,7 +336,7 @@
#else
#ifdef SHADOWCSMDEBUG{X}
diffuseBase += info.diffuse * shadowDebug{X};
#else
#else
diffuseBase += info.diffuse * shadow;
#endif
#ifdef SPECULARTERM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ albedoOpacityOutParams albedoOpacityBlock(
,in vec2 albedoInfos
#endif
, in float baseWeight
#ifdef BASEWEIGHT
#ifdef BASE_WEIGHT
, in vec4 baseWeightTexture
, in vec2 vBaseWeightInfos
#endif
Expand Down Expand Up @@ -75,7 +75,7 @@ albedoOpacityOutParams albedoOpacityBlock(
// applied in computeDiffuseLighting), but with the diffuse model *currently* used
// in Babylon.js, factoring it into the surfaceAlbedo is equivalent.
surfaceAlbedo *= baseWeight;
#ifdef BASEWEIGHT
#ifdef BASE_WEIGHT
surfaceAlbedo *= baseWeightTexture.r;
#endif

Expand Down
Loading
Loading