Skip to content

Commit

Permalink
Fix UV Animations Remove checkTransformsAreIdentical (#14947)
Browse files Browse the repository at this point in the history
* Fix UV Animations Remove checkTransformsAreIdentical

* fix
  • Loading branch information
sebavan committed Apr 5, 2024
1 parent 81cd742 commit d190219
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 120 deletions.
8 changes: 1 addition & 7 deletions packages/dev/core/src/Materials/PBR/pbrBaseMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1739,12 +1739,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
}

if (this._metallicReflectanceTexture || this._reflectanceTexture) {
const identicalTextures =
this._metallicReflectanceTexture !== null &&
this._metallicReflectanceTexture._texture === this._reflectanceTexture?._texture &&
this._metallicReflectanceTexture.checkTransformsAreIdentical(this._reflectanceTexture);

defines.METALLIC_REFLECTANCE_USE_ALPHA_ONLY = this._useOnlyMetallicFromMetallicReflectanceTexture && !identicalTextures;
defines.METALLIC_REFLECTANCE_USE_ALPHA_ONLY = this._useOnlyMetallicFromMetallicReflectanceTexture;
if (this._metallicReflectanceTexture) {
PrepareDefinesForMergedUV(this._metallicReflectanceTexture, defines, "METALLIC_REFLECTANCE");
defines.METALLIC_REFLECTANCE_GAMMA = this._metallicReflectanceTexture.gammaSpace;
Expand All @@ -1753,7 +1748,6 @@ export abstract class PBRBaseMaterial extends PushMaterial {
}
if (
this._reflectanceTexture &&
!identicalTextures &&
(!this._metallicReflectanceTexture || (this._metallicReflectanceTexture && this._useOnlyMetallicFromMetallicReflectanceTexture))
) {
PrepareDefinesForMergedUV(this._reflectanceTexture, defines, "REFLECTANCE");
Expand Down
15 changes: 3 additions & 12 deletions packages/dev/core/src/Materials/PBR/pbrClearCoatConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class MaterialClearCoatDefines extends MaterialDefines {
public CLEARCOAT_BUMP = false;
public CLEARCOAT_BUMPDIRECTUV = 0;
public CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE = false;
public CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL = false;
public CLEARCOAT_REMAP_F0 = false;

public CLEARCOAT_TINT = false;
Expand Down Expand Up @@ -224,8 +223,6 @@ export class PBRClearCoatConfiguration extends MaterialPluginBase {
if (this._isEnabled) {
defines.CLEARCOAT = true;
defines.CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE = this._useRoughnessFromMainTexture;
defines.CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL =
this._texture !== null && this._texture._texture === this._textureRoughness?._texture && this._texture.checkTransformsAreIdentical(this._textureRoughness);
defines.CLEARCOAT_REMAP_F0 = this._remapF0OnInterfaceChange;

if (defines._areTexturesDirty) {
Expand Down Expand Up @@ -272,7 +269,6 @@ export class PBRClearCoatConfiguration extends MaterialPluginBase {
defines.CLEARCOAT_TINT = false;
defines.CLEARCOAT_TINT_TEXTURE = false;
defines.CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE = false;
defines.CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL = false;
defines.CLEARCOAT_DEFAULTIOR = false;
defines.CLEARCOAT_TEXTUREDIRECTUV = 0;
defines.CLEARCOAT_TEXTURE_ROUGHNESSDIRECTUV = 0;
Expand All @@ -296,13 +292,8 @@ export class PBRClearCoatConfiguration extends MaterialPluginBase {
const invertNormalMapX = this._material._invertNormalMapX;
const invertNormalMapY = this._material._invertNormalMapY;

const identicalTextures = defines.CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL;

if (!uniformBuffer.useUbo || !isFrozen || !uniformBuffer.isSync) {
if (identicalTextures && MaterialFlags.ClearCoatTextureEnabled) {
uniformBuffer.updateFloat4("vClearCoatInfos", this._texture!.coordinatesIndex, this._texture!.level, -1, -1);
BindTextureMatrix(this._texture!, uniformBuffer, "clearCoat");
} else if ((this._texture || this._textureRoughness) && MaterialFlags.ClearCoatTextureEnabled) {
if ((this._texture || this._textureRoughness) && MaterialFlags.ClearCoatTextureEnabled) {
uniformBuffer.updateFloat4(
"vClearCoatInfos",
this._texture?.coordinatesIndex ?? 0,
Expand All @@ -313,7 +304,7 @@ export class PBRClearCoatConfiguration extends MaterialPluginBase {
if (this._texture) {
BindTextureMatrix(this._texture, uniformBuffer, "clearCoat");
}
if (this._textureRoughness && !identicalTextures && !defines.CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE) {
if (this._textureRoughness && !defines.CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE) {
BindTextureMatrix(this._textureRoughness, uniformBuffer, "clearCoatRoughness");
}
}
Expand Down Expand Up @@ -356,7 +347,7 @@ export class PBRClearCoatConfiguration extends MaterialPluginBase {
uniformBuffer.setTexture("clearCoatSampler", this._texture);
}

if (this._textureRoughness && !identicalTextures && !defines.CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE && MaterialFlags.ClearCoatTextureEnabled) {
if (this._textureRoughness && !defines.CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE && MaterialFlags.ClearCoatTextureEnabled) {
uniformBuffer.setTexture("clearCoatRoughnessSampler", this._textureRoughness);
}

Expand Down
23 changes: 5 additions & 18 deletions packages/dev/core/src/Materials/PBR/pbrIridescenceConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import { MaterialFlags } from "../materialFlags";
import type { UniformBuffer } from "../../Materials/uniformBuffer";
import type { IAnimatable } from "../../Animations/animatable.interface";
import type { EffectFallbacks } from "../effectFallbacks";
import type { SubMesh } from "../../Meshes/subMesh";
import { Constants } from "../../Engines/constants";
import { MaterialPluginBase } from "../materialPluginBase";
import { MaterialDefines } from "../materialDefines";

import type { Engine } from "../../Engines/engine";
import type { Scene } from "../../scene";
import type { PBRBaseMaterial } from "./pbrBaseMaterial";
import { BindTextureMatrix, PrepareDefinesForMergedUV } from "../materialHelper.functions";
Expand All @@ -25,7 +23,6 @@ export class MaterialIridescenceDefines extends MaterialDefines {
public IRIDESCENCE_TEXTUREDIRECTUV = 0;
public IRIDESCENCE_THICKNESS_TEXTURE = false;
public IRIDESCENCE_THICKNESS_TEXTUREDIRECTUV = 0;
public IRIDESCENCE_USE_THICKNESS_FROM_MAINTEXTURE = false;
}

/**
Expand Down Expand Up @@ -145,8 +142,6 @@ export class PBRIridescenceConfiguration extends MaterialPluginBase {
public prepareDefinesBeforeAttributes(defines: MaterialIridescenceDefines, scene: Scene): void {
if (this._isEnabled) {
defines.IRIDESCENCE = true;
defines.IRIDESCENCE_USE_THICKNESS_FROM_MAINTEXTURE =
this._texture !== null && this._texture._texture === this._thicknessTexture?._texture && this._texture.checkTransformsAreIdentical(this._thicknessTexture);

if (defines._areTexturesDirty) {
if (scene.texturesEnabled) {
Expand All @@ -156,7 +151,7 @@ export class PBRIridescenceConfiguration extends MaterialPluginBase {
defines.IRIDESCENCE_TEXTURE = false;
}

if (!defines.IRIDESCENCE_USE_THICKNESS_FROM_MAINTEXTURE && this._thicknessTexture && MaterialFlags.IridescenceTextureEnabled) {
if (this._thicknessTexture && MaterialFlags.IridescenceTextureEnabled) {
PrepareDefinesForMergedUV(this._thicknessTexture, defines, "IRIDESCENCE_THICKNESS_TEXTURE");
} else {
defines.IRIDESCENCE_THICKNESS_TEXTURE = false;
Expand All @@ -167,28 +162,20 @@ export class PBRIridescenceConfiguration extends MaterialPluginBase {
defines.IRIDESCENCE = false;
defines.IRIDESCENCE_TEXTURE = false;
defines.IRIDESCENCE_THICKNESS_TEXTURE = false;
defines.IRIDESCENCE_USE_THICKNESS_FROM_MAINTEXTURE = false;
defines.IRIDESCENCE_TEXTUREDIRECTUV = 0;
defines.IRIDESCENCE_THICKNESS_TEXTUREDIRECTUV = 0;
}
}

public bindForSubMesh(uniformBuffer: UniformBuffer, scene: Scene, engine: Engine, subMesh: SubMesh): void {
public bindForSubMesh(uniformBuffer: UniformBuffer, scene: Scene): void {
if (!this._isEnabled) {
return;
}

const defines = subMesh!.materialDefines as unknown as MaterialIridescenceDefines;

const isFrozen = this._material.isFrozen;

const identicalTextures = defines.IRIDESCENCE_USE_THICKNESS_FROM_MAINTEXTURE;

if (!uniformBuffer.useUbo || !isFrozen || !uniformBuffer.isSync) {
if (identicalTextures && MaterialFlags.IridescenceTextureEnabled) {
uniformBuffer.updateFloat4("vIridescenceInfos", this._texture!.coordinatesIndex, this._texture!.level, -1, -1);
BindTextureMatrix(this._texture!, uniformBuffer, "iridescence");
} else if ((this._texture || this._thicknessTexture) && MaterialFlags.IridescenceTextureEnabled) {
if ((this._texture || this._thicknessTexture) && MaterialFlags.IridescenceTextureEnabled) {
uniformBuffer.updateFloat4(
"vIridescenceInfos",
this._texture?.coordinatesIndex ?? 0,
Expand All @@ -199,7 +186,7 @@ export class PBRIridescenceConfiguration extends MaterialPluginBase {
if (this._texture) {
BindTextureMatrix(this._texture, uniformBuffer, "iridescence");
}
if (this._thicknessTexture && !identicalTextures && !defines.IRIDESCENCE_USE_THICKNESS_FROM_MAINTEXTURE) {
if (this._thicknessTexture) {
BindTextureMatrix(this._thicknessTexture, uniformBuffer, "iridescenceThickness");
}
}
Expand All @@ -214,7 +201,7 @@ export class PBRIridescenceConfiguration extends MaterialPluginBase {
uniformBuffer.setTexture("iridescenceSampler", this._texture);
}

if (this._thicknessTexture && !identicalTextures && !defines.IRIDESCENCE_USE_THICKNESS_FROM_MAINTEXTURE && MaterialFlags.IridescenceTextureEnabled) {
if (this._thicknessTexture && MaterialFlags.IridescenceTextureEnabled) {
uniformBuffer.setTexture("iridescenceThicknessSampler", this._thicknessTexture);
}
}
Expand Down
15 changes: 3 additions & 12 deletions packages/dev/core/src/Materials/PBR/pbrSheenConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export class MaterialSheenDefines extends MaterialDefines {
public SHEEN_ROUGHNESS = false;
public SHEEN_ALBEDOSCALING = false;
public SHEEN_USE_ROUGHNESS_FROM_MAINTEXTURE = false;
public SHEEN_TEXTURE_ROUGHNESS_IDENTICAL = false;
}

/**
Expand Down Expand Up @@ -161,8 +160,6 @@ export class PBRSheenConfiguration extends MaterialPluginBase {
defines.SHEEN_ROUGHNESS = this._roughness !== null;
defines.SHEEN_ALBEDOSCALING = this._albedoScaling;
defines.SHEEN_USE_ROUGHNESS_FROM_MAINTEXTURE = this._useRoughnessFromMainTexture;
defines.SHEEN_TEXTURE_ROUGHNESS_IDENTICAL =
this._texture !== null && this._texture._texture === this._textureRoughness?._texture && this._texture.checkTransformsAreIdentical(this._textureRoughness);

if (defines._areTexturesDirty) {
if (scene.texturesEnabled) {
Expand All @@ -188,7 +185,6 @@ export class PBRSheenConfiguration extends MaterialPluginBase {
defines.SHEEN_ROUGHNESS = false;
defines.SHEEN_ALBEDOSCALING = false;
defines.SHEEN_USE_ROUGHNESS_FROM_MAINTEXTURE = false;
defines.SHEEN_TEXTURE_ROUGHNESS_IDENTICAL = false;
defines.SHEEN_GAMMATEXTURE = false;
defines.SHEEN_TEXTUREDIRECTUV = 0;
defines.SHEEN_TEXTURE_ROUGHNESSDIRECTUV = 0;
Expand All @@ -204,13 +200,8 @@ export class PBRSheenConfiguration extends MaterialPluginBase {

const isFrozen = this._material.isFrozen;

const identicalTextures = defines.SHEEN_TEXTURE_ROUGHNESS_IDENTICAL;

if (!uniformBuffer.useUbo || !isFrozen || !uniformBuffer.isSync) {
if (identicalTextures && MaterialFlags.SheenTextureEnabled) {
uniformBuffer.updateFloat4("vSheenInfos", this._texture!.coordinatesIndex, this._texture!.level, -1, -1);
BindTextureMatrix(this._texture!, uniformBuffer, "sheen");
} else if ((this._texture || this._textureRoughness) && MaterialFlags.SheenTextureEnabled) {
if ((this._texture || this._textureRoughness) && MaterialFlags.SheenTextureEnabled) {
uniformBuffer.updateFloat4(
"vSheenInfos",
this._texture?.coordinatesIndex ?? 0,
Expand All @@ -221,7 +212,7 @@ export class PBRSheenConfiguration extends MaterialPluginBase {
if (this._texture) {
BindTextureMatrix(this._texture, uniformBuffer, "sheen");
}
if (this._textureRoughness && !identicalTextures && !defines.SHEEN_USE_ROUGHNESS_FROM_MAINTEXTURE) {
if (this._textureRoughness && !defines.SHEEN_USE_ROUGHNESS_FROM_MAINTEXTURE) {
BindTextureMatrix(this._textureRoughness, uniformBuffer, "sheenRoughness");
}
}
Expand All @@ -240,7 +231,7 @@ export class PBRSheenConfiguration extends MaterialPluginBase {
uniformBuffer.setTexture("sheenSampler", this._texture);
}

if (this._textureRoughness && !identicalTextures && !defines.SHEEN_USE_ROUGHNESS_FROM_MAINTEXTURE && MaterialFlags.SheenTextureEnabled) {
if (this._textureRoughness && !defines.SHEEN_USE_ROUGHNESS_FROM_MAINTEXTURE && MaterialFlags.SheenTextureEnabled) {
uniformBuffer.setTexture("sheenRoughnessSampler", this._textureRoughness);
}
}
Expand Down
28 changes: 5 additions & 23 deletions packages/dev/core/src/Materials/PBR/pbrSubSurfaceConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,45 +425,27 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
defines.SS_USE_LOCAL_REFRACTIONMAP_CUBIC = false;
defines.SS_USE_THICKNESS_AS_DEPTH = false;

const refractionIntensityTextureIsThicknessTexture =
!!this._thicknessTexture &&
!!this._refractionIntensityTexture &&
this._refractionIntensityTexture.checkTransformsAreIdentical(this._thicknessTexture) &&
this._refractionIntensityTexture._texture === this._thicknessTexture._texture;

const translucencyIntensityTextureIsThicknessTexture =
!!this._thicknessTexture &&
!!this._translucencyIntensityTexture &&
this._translucencyIntensityTexture.checkTransformsAreIdentical(this._thicknessTexture) &&
this._translucencyIntensityTexture._texture === this._thicknessTexture._texture;

// if true, it means the refraction/translucency textures are the same than the thickness texture so there's no need to pass them to the shader, only thicknessTexture
const useOnlyThicknessTexture =
(refractionIntensityTextureIsThicknessTexture || !this._refractionIntensityTexture) &&
(translucencyIntensityTextureIsThicknessTexture || !this._translucencyIntensityTexture);

if (defines._areTexturesDirty) {
if (scene.texturesEnabled) {
if (this._thicknessTexture && MaterialFlags.ThicknessTextureEnabled) {
PrepareDefinesForMergedUV(this._thicknessTexture, defines, "SS_THICKNESSANDMASK_TEXTURE");
}

if (this._refractionIntensityTexture && MaterialFlags.RefractionIntensityTextureEnabled && !useOnlyThicknessTexture) {
if (this._refractionIntensityTexture && MaterialFlags.RefractionIntensityTextureEnabled) {
PrepareDefinesForMergedUV(this._refractionIntensityTexture, defines, "SS_REFRACTIONINTENSITY_TEXTURE");
}

if (this._translucencyIntensityTexture && MaterialFlags.TranslucencyIntensityTextureEnabled && !useOnlyThicknessTexture) {
if (this._translucencyIntensityTexture && MaterialFlags.TranslucencyIntensityTextureEnabled) {
PrepareDefinesForMergedUV(this._translucencyIntensityTexture, defines, "SS_TRANSLUCENCYINTENSITY_TEXTURE");
}
}
}

defines.SS_HAS_THICKNESS = this.maximumThickness - this.minimumThickness !== 0.0;
defines.SS_MASK_FROM_THICKNESS_TEXTURE =
(this._useMaskFromThicknessTexture || !!this._refractionIntensityTexture || !!this._translucencyIntensityTexture) && useOnlyThicknessTexture;
defines.SS_MASK_FROM_THICKNESS_TEXTURE = this._useMaskFromThicknessTexture || !!this._refractionIntensityTexture || !!this._translucencyIntensityTexture;
defines.SS_USE_GLTF_TEXTURES = this._useGltfStyleTextures;
defines.SS_REFRACTION_USE_INTENSITY_FROM_TEXTURE = (this._useMaskFromThicknessTexture || !!this._refractionIntensityTexture) && useOnlyThicknessTexture;
defines.SS_TRANSLUCENCY_USE_INTENSITY_FROM_TEXTURE = (this._useMaskFromThicknessTexture || !!this._translucencyIntensityTexture) && useOnlyThicknessTexture;
defines.SS_REFRACTION_USE_INTENSITY_FROM_TEXTURE = this._useMaskFromThicknessTexture || !!this._refractionIntensityTexture;
defines.SS_TRANSLUCENCY_USE_INTENSITY_FROM_TEXTURE = this._useMaskFromThicknessTexture || !!this._translucencyIntensityTexture;

if (this._isRefractionEnabled) {
if (scene.texturesEnabled) {
Expand Down
9 changes: 0 additions & 9 deletions packages/dev/core/src/Materials/Textures/baseTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,15 +575,6 @@ export class BaseTexture extends ThinTexture implements IAnimatable {
return this._engine;
}

/**
* Checks if the texture has the same transform matrix than another texture
* @param texture texture to check against
* @returns true if the transforms are the same, else false
*/
public checkTransformsAreIdentical(texture: Nullable<BaseTexture>): boolean {
return texture !== null;
}

/**
* Get the texture transform matrix used to offset tile the texture for instance.
* @returns the transformation matrix
Expand Down
18 changes: 0 additions & 18 deletions packages/dev/core/src/Materials/Textures/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,24 +658,6 @@ export class Texture extends BaseTexture {
t.z += this.wRotationCenter;
}

/**
* Checks if the texture has the same transform matrix than another texture
* @param texture texture to check against
* @returns true if the transforms are the same, else false
*/
public checkTransformsAreIdentical(texture: Nullable<Texture>): boolean {
return (
texture !== null &&
this.uOffset === texture.uOffset &&
this.vOffset === texture.vOffset &&
this.uScale === texture.uScale &&
this.vScale === texture.vScale &&
this.uAng === texture.uAng &&
this.vAng === texture.vAng &&
this.wAng === texture.wAng
);
}

/**
* Get the current texture matrix which includes the requested offsetting, tiling and rotation components.
* @param uBase The horizontal base offset multiplier (1 by default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ struct clearcoatOutParams


#if defined(CLEARCOAT_TEXTURE_ROUGHNESS) && !defined(CLEARCOAT_USE_ROUGHNESS_FROM_MAINTEXTURE)
#ifdef CLEARCOAT_TEXTURE_ROUGHNESS_IDENTICAL
clearCoatRoughness *= clearCoatMapData.y;
#else
clearCoatRoughness *= clearCoatMapRoughnessData.y;
#endif
clearCoatRoughness *= clearCoatMapRoughnessData.y;
#endif

outParams.clearCoatIntensity = clearCoatIntensity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ struct iridescenceOutParams

#ifdef IRIDESCENCE_TEXTURE
iridescenceIntensity *= iridescenceMapData.x;

#ifdef IRIDESCENCE_USE_THICKNESS_FROM_MAINTEXTURE
iridescenceThicknessWeight = iridescenceMapData.g;
#endif
#endif

#if defined(IRIDESCENCE_THICKNESS_TEXTURE)
Expand Down
Loading

0 comments on commit d190219

Please sign in to comment.