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 3 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
70 changes: 64 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.BASEWEIGHT = false;
defines.BASE_WEIGHT = false;
}

if (this._baseDiffuseRoughnessTexture && MaterialFlags.BaseDiffuseRoughnessTextureEnabled) {
PrepareDefinesForMergedUV(this._baseDiffuseRoughnessTexture, defines, "BASE_DIFFUSE_ROUGHNESS");
} else {
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,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
}

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

// Misc
this._lightingInfos.x = this._directIntensity;
Expand Down Expand Up @@ -2346,6 +2387,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 +2529,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 +2605,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 +2674,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 +2758,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
10 changes: 6 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 All @@ -87,6 +88,7 @@
#elif defined(SS_TRANSLUCENCY)
info.diffuse = computeDiffuseAndTransmittedLighting(preInfo, diffuse{X}.rgb, subSurfaceOut.transmittance);
#else
preInfo.roughness = diffuseRoughness;
info.diffuse = computeDiffuseLighting(preInfo, diffuse{X}.rgb);
#endif

Expand Down Expand Up @@ -129,7 +131,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 +180,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 +337,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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct lightingInfo
// Simulate area (small) lights by increasing roughness
float adjustRoughnessFromLightProperties(float roughness, float lightRadius, float lightDistance) {
#if defined(USEPHYSICALLIGHTFALLOFF) || defined(USEGLTFLIGHTFALLOFF)
// At small angle this approximation works.
// At small angle this approximation works.
float lightRoughness = lightRadius / lightDistance;
// Distribution can sum.
float totalRoughness = saturate(lightRoughness + roughness);
Expand All @@ -41,7 +41,7 @@ vec3 computeHemisphericDiffuseLighting(preLightingInfo info, vec3 lightColor, ve
#endif

vec3 computeDiffuseLighting(preLightingInfo info, vec3 lightColor) {
float diffuseTerm = diffuseBRDF_Burley(info.NdotL, info.NdotV, info.VdotH, info.roughness);
float diffuseTerm = diffuseBRDF_Burley(info.NdotL, info.NdotV, info.VdotH, info.diffuseRoughness);
return diffuseTerm * info.attenuation * info.NdotL * lightColor;
}

Expand All @@ -64,7 +64,7 @@ vec3 computeProjectionTextureDiffuseLighting(sampler2D projectionLightSampler, m
float trAdapt = step(0., info.NdotLUnclamped);
vec3 transmittanceNdotL = mix(transmittance * wrapNdotL, vec3(wrapNdotL), trAdapt);

float diffuseTerm = diffuseBRDF_Burley(NdotL, info.NdotV, info.VdotH, info.roughness);
float diffuseTerm = diffuseBRDF_Burley(NdotL, info.NdotV, info.VdotH, info.diffuseRoughness);
return diffuseTerm * transmittanceNdotL * info.attenuation * lightColor;
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ struct preLightingInfo
float NdotLUnclamped;
float NdotL;
float VdotH;

// TODO: the code would probably be leaner with material properties out of the structure.
float roughness;
float diffuseRoughness;

#ifdef IRIDESCENCE
float iridescenceIntensity;
Expand Down Expand Up @@ -91,7 +94,7 @@ preLightingInfo computeHemisphericPreLightingInfo(vec4 lightData, vec3 V, vec3 N
#if defined(AREALIGHTUSED) && defined(AREALIGHTSUPPORTED)
#include<ltcHelperFunctions>

preLightingInfo computeAreaPreLightingInfo(sampler2D ltc1, sampler2D ltc2, vec3 viewDirectionW, vec3 vNormal, vec3 vPosition, vec4 lightData, vec3 halfWidth, vec3 halfHeight, float roughness )
preLightingInfo computeAreaPreLightingInfo(sampler2D ltc1, sampler2D ltc2, vec3 viewDirectionW, vec3 vNormal, vec3 vPosition, vec4 lightData, vec3 halfWidth, vec3 halfHeight, float roughness )
{
preLightingInfo result;
result.lightOffset = lightData.xyz - vPosition;
Expand All @@ -103,7 +106,7 @@ preLightingInfo computeAreaPreLightingInfo(sampler2D ltc1, sampler2D ltc2, vec3

#ifdef SPECULARTERM
result.areaLightFresnel = data.Fresnel;
result.areaLightSpecular = data.Specular;
result.areaLightSpecular = data.Specular;
#endif
result.areaLightDiffuse = data.Diffuse;
return result;
Expand Down
Loading
Loading