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 OpenPBR's base_weight material parameter #16085

Merged
merged 13 commits into from
Jan 24, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -974,10 +974,11 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
,vec4${state.fSuffix}(1.)
,vec2${state.fSuffix}(1., 1.)
#endif
,1. // Base Weight
#ifdef OPACITY
,vec4${state.fSuffix}(${opacity})
,vec2${state.fSuffix}(1., 1.)
#endif
#endif
);

${state._declareLocalVar("surfaceAlbedo", NodeMaterialBlockConnectionPointTypes.Vector3)} = albedoOpacityOut.surfaceAlbedo;
Expand Down
59 changes: 59 additions & 0 deletions packages/dev/core/src/Materials/PBR/pbrBaseMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ export class PBRMaterialDefines extends MaterialDefines implements IImageProcess
public ALBEDODIRECTUV = 0;
public VERTEXCOLOR = false;

public BASEWEIGHT = false;
public BASEWEIGHTDIRECTUV = 0;

public BAKED_VERTEX_ANIMATION_TEXTURE = false;

public AMBIENT = false;
Expand Down Expand Up @@ -417,6 +420,12 @@ export abstract class PBRBaseMaterial extends PushMaterial {
*/
public _albedoTexture: Nullable<BaseTexture> = null;

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

/**
* AKA Occlusion Texture in other nomenclature.
* @internal
Expand Down Expand Up @@ -559,6 +568,12 @@ export abstract class PBRBaseMaterial extends PushMaterial {
*/
public _albedoColor = new Color3(1, 1, 1);

/**
* OpenPBR Base Weight (multiplier to the diffuse and metal lobes).
* @internal
*/
public _baseWeight = 1;

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

if (this._baseWeightTexture && MaterialFlags.BaseWeightTextureEnabled) {
if (!this._baseWeightTexture.isReadyOrNotBlocking()) {
return false;
}
}

if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
if (!this._ambientTexture.isReadyOrNotBlocking()) {
return false;
Expand Down Expand Up @@ -1413,6 +1434,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
"vLightsType",
"vAmbientColor",
"vAlbedoColor",
"baseWeight",
"vReflectivityColor",
"vMetallicReflectanceFactors",
"vEmissiveColor",
Expand All @@ -1422,6 +1444,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
"vFogColor",
"pointSize",
"vAlbedoInfos",
"vBaseWeightInfos",
"vAmbientInfos",
"vOpacityInfos",
"vReflectionInfos",
Expand All @@ -1437,6 +1460,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
"vLightmapInfos",
"mBones",
"albedoMatrix",
"baseWeightMatrix",
"ambientMatrix",
"opacityMatrix",
"reflectionMatrix",
Expand Down Expand Up @@ -1478,6 +1502,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {

const samplers = [
"albedoSampler",
"baseWeightSampler",
"reflectivitySampler",
"ambientSampler",
"emissiveSampler",
Expand Down Expand Up @@ -1612,6 +1637,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
}
if (scene.texturesEnabled) {
defines.ALBEDODIRECTUV = 0;
defines.BASEWEIGHTDIRECTUV = 0;
defines.AMBIENTDIRECTUV = 0;
defines.OPACITYDIRECTUV = 0;
defines.EMISSIVEDIRECTUV = 0;
Expand All @@ -1633,6 +1659,12 @@ export abstract class PBRBaseMaterial extends PushMaterial {
defines.ALBEDO = false;
}

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

if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
PrepareDefinesForMergedUV(this._ambientTexture, defines, "AMBIENT");
defines.AMBIENTINGRAYSCALE = this._useAmbientInGrayScale;
Expand Down Expand Up @@ -1981,6 +2013,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
// Order is important !
const ubo = this._uniformBuffer;
ubo.addUniform("vAlbedoInfos", 2);
ubo.addUniform("vBaseWeightInfos", 2);
ubo.addUniform("vAmbientInfos", 4);
ubo.addUniform("vOpacityInfos", 2);
ubo.addUniform("vEmissiveInfos", 2);
Expand All @@ -1993,6 +2026,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
ubo.addUniform("vReflectionSize", 3);
ubo.addUniform("vBumpInfos", 3);
ubo.addUniform("albedoMatrix", 16);
ubo.addUniform("baseWeightMatrix", 16);
ubo.addUniform("ambientMatrix", 16);
ubo.addUniform("opacityMatrix", 16);
ubo.addUniform("emissiveMatrix", 16);
Expand All @@ -2005,6 +2039,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {

ubo.addUniform("vReflectionColor", 3);
ubo.addUniform("vAlbedoColor", 4);
ubo.addUniform("baseWeight", 1);
ubo.addUniform("vLightingIntensity", 4);

ubo.addUniform("vReflectionMicrosurfaceInfos", 3);
Expand Down Expand Up @@ -2107,6 +2142,11 @@ export abstract class PBRBaseMaterial extends PushMaterial {
BindTextureMatrix(this._albedoTexture, ubo, "albedo");
}

if (this._baseWeightTexture && MaterialFlags.BaseWeightTextureEnabled) {
ubo.updateFloat2("vBaseWeightInfos", this._baseWeightTexture.coordinatesIndex, this._baseWeightTexture.level);
BindTextureMatrix(this._baseWeightTexture, ubo, "baseWeight");
}

if (this._ambientTexture && MaterialFlags.AmbientTextureEnabled) {
ubo.updateFloat4(
"vAmbientInfos",
Expand Down Expand Up @@ -2268,6 +2308,8 @@ export abstract class PBRBaseMaterial extends PushMaterial {
ubo.updateColor4("vAlbedoColor", this._albedoColor, this.alpha);
}

ubo.updateFloat("baseWeight", this._baseWeight);

// Misc
this._lightingInfos.x = this._directIntensity;
this._lightingInfos.y = this._emissiveIntensity;
Expand All @@ -2290,6 +2332,10 @@ export abstract class PBRBaseMaterial extends PushMaterial {
ubo.setTexture("albedoSampler", this._albedoTexture);
}

if (this._baseWeightTexture && MaterialFlags.BaseWeightTextureEnabled) {
ubo.setTexture("baseWeightSampler", this._baseWeightTexture);
}

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

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

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

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

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

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

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

this._albedoTexture?.dispose();
this._baseWeightTexture?.dispose();
this._ambientTexture?.dispose();
this._opacityTexture?.dispose();
this._reflectionTexture?.dispose();
Expand Down
14 changes: 14 additions & 0 deletions packages/dev/core/src/Materials/PBR/pbrMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ export class PBRMaterial extends PBRBaseMaterial {
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
public albedoTexture: Nullable<BaseTexture>;

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

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

/**
* OpenPBR Base Weight (multiplier to the diffuse and metal lobes).
*/
@serialize("baseWeight")
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
public baseWeight = 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 @@ -22,6 +22,22 @@ export class MaterialFlags {
AbstractEngine.MarkAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag);
}

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

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

private static _DetailTextureEnabled = true;
/**
* Are detail textures enabled in the application.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ albedoOpacityOutParams albedoOpacityBlock(
#ifdef ALBEDO
,in vec4 albedoTexture
,in vec2 albedoInfos
#endif
, in float baseWeight
#ifdef BASEWEIGHT
, in vec4 baseWeightTexture
, in vec2 vBaseWeightInfos
#endif
#ifdef OPACITY
,in vec4 opacityMap
Expand All @@ -22,7 +27,7 @@ albedoOpacityOutParams albedoOpacityBlock(
#ifdef DECAL
,in vec4 decalColor
,in vec4 vDecalInfos
#endif
#endif
)
{
albedoOpacityOutParams outParams;
Expand Down Expand Up @@ -63,6 +68,17 @@ albedoOpacityOutParams albedoOpacityBlock(

#define CUSTOM_FRAGMENT_UPDATE_ALBEDO

// According to OpenPBR:
// - for metals, base_weight is a factor to the base_color (F0, thus surfaceAlbedo in
// Babylons.js).
// - for dielectrics, base_weight is a factor to the diffuse BRDF (i.e. it should be
// applied in computeDiffuseLighting), but with the diffuse model *currently* used
// in Babylon.js, factoring it into the surfaceAlbedo is equivalent.
surfaceAlbedo *= baseWeight;
#if BASEWEIGHT
surfaceAlbedo *= baseWeightTexture.r;
#endif

// _____________________________ Alpha Information _______________________________
#ifdef OPACITY
#ifdef OPACITYRGB
Expand All @@ -79,7 +95,7 @@ albedoOpacityOutParams albedoOpacityBlock(
#endif

#if !defined(SS_LINKREFRACTIONTOTRANSPARENCY) && !defined(ALPHAFRESNEL)
#ifdef ALPHATEST
#ifdef ALPHATEST
#if DEBUGMODE != 88
if (alpha < ALPHATESTVALUE)
discard;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ uniform vec4 vEyePosition;

uniform vec3 vReflectionColor;
uniform vec4 vAlbedoColor;
uniform float baseWeight;

// CUSTOM CONTROLS
uniform vec4 vLightingIntensity;
Expand All @@ -19,6 +20,10 @@ uniform vec3 vAmbientColor;
uniform vec2 vAlbedoInfos;
#endif

#ifdef BASEWEIGHT
uniform vec2 vBaseWeightInfos;
#endif

#ifdef AMBIENT
uniform vec4 vAmbientInfos;
#endif
Expand Down Expand Up @@ -64,14 +69,14 @@ uniform mat4 view;

#if defined(USE_LOCAL_REFLECTIONMAP_CUBIC) && defined(REFLECTIONMAP_CUBIC)
uniform vec3 vReflectionPosition;
uniform vec3 vReflectionSize;
uniform vec3 vReflectionSize;
#endif
#endif

// Refraction
#if defined(SS_REFRACTION) && defined(SS_USE_LOCAL_REFRACTIONMAP_CUBIC)
uniform vec3 vRefractionPosition;
uniform vec3 vRefractionSize;
uniform vec3 vRefractionSize;
#endif

// Clear Coat
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include<samplerFragmentDeclaration>(_DEFINENAME_,ALBEDO,_VARYINGNAME_,Albedo,_SAMPLERNAME_,albedo)
#include<samplerFragmentDeclaration>(_DEFINENAME_,BASEWEIGHT,_VARYINGNAME_,BaseWeight,_SAMPLERNAME_,baseWeight)
#include<samplerFragmentDeclaration>(_DEFINENAME_,AMBIENT,_VARYINGNAME_,Ambient,_SAMPLERNAME_,ambient)
#include<samplerFragmentDeclaration>(_DEFINENAME_,OPACITY,_VARYINGNAME_,Opacity,_SAMPLERNAME_,opacity)
#include<samplerFragmentDeclaration>(_DEFINENAME_,EMISSIVE,_VARYINGNAME_,Emissive,_SAMPLERNAME_,emissive)
Expand Down Expand Up @@ -42,7 +43,7 @@
#define sampleReflection(s, c) textureCube(s, c)

uniform samplerCube reflectionSampler;

#ifdef LODBASEDMICROSFURACE
#define sampleReflectionLod(s, c, l) textureCubeLodEXT(s, c, l)
#else
Expand Down Expand Up @@ -88,7 +89,7 @@
#ifdef SS_REFRACTION
#ifdef SS_REFRACTIONMAP_3D
#define sampleRefraction(s, c) textureCube(s, c)

uniform samplerCube refractionSampler;

#ifdef LODBASEDMICROSFURACE
Expand All @@ -99,7 +100,7 @@
#endif
#else
#define sampleRefraction(s, c) texture2D(s, c)

uniform sampler2D refractionSampler;

#ifdef LODBASEDMICROSFURACE
Expand Down
Loading
Loading