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

Replace KHR_materials_translucency with KHR_materials_diffuse_transmission #15001

Merged
merged 3 commits into from
Apr 18, 2024
Merged
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 @@ -55,6 +55,7 @@ export class SubSurfaceBlock extends NodeMaterialBlock {
state._excludeVariableName("subSurfaceOut");
state._excludeVariableName("vThicknessParam");
state._excludeVariableName("vTintColor");
state._excludeVariableName("vTranslucencyColor");
state._excludeVariableName("vSubSurfaceIntensity");
state._excludeVariableName("dispersion");
}
Expand Down Expand Up @@ -248,6 +249,10 @@ export class SubSurfaceBlock extends NodeMaterialBlock {
#endif
#ifdef SS_TRANSLUCENCY
${translucencyDiffusionDistance},
vTintColor,
#ifdef SS_TRANSLUCENCYCOLOR_TEXTURE
vec4(0.),
#endif
#endif
subSurfaceOut
);
Expand Down
86 changes: 76 additions & 10 deletions packages/dev/core/src/Materials/PBR/pbrSubSurfaceConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class MaterialSubSurfaceDefines extends MaterialDefines {
public SS_REFRACTIONINTENSITY_TEXTUREDIRECTUV = 0;
public SS_TRANSLUCENCYINTENSITY_TEXTURE = false;
public SS_TRANSLUCENCYINTENSITY_TEXTUREDIRECTUV = 0;
public SS_TRANSLUCENCYCOLOR_TEXTURE = false;
public SS_TRANSLUCENCYCOLOR_TEXTUREDIRECTUV = 0;

public SS_REFRACTIONMAP_3D = false;
public SS_REFRACTIONMAP_OPPOSITEZ = false;
Expand Down Expand Up @@ -279,7 +281,7 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
* Stores the intensity of the different subsurface effects in the thickness texture.
* Note that if refractionIntensityTexture and/or translucencyIntensityTexture is provided it takes precedence over thicknessTexture + useMaskFromThicknessTexture
* * the green (red if useGltfStyleTextures = true) channel is the refraction intensity.
* * the blue channel is the translucency intensity.
* * the blue (alpha if useGltfStyleTextures = true) channel is the translucency intensity.
*/
@serialize()
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
Expand All @@ -297,23 +299,41 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
private _translucencyIntensityTexture: Nullable<BaseTexture> = null;
/**
* Stores the intensity of the translucency. If provided, it takes precedence over thicknessTexture + useMaskFromThicknessTexture
* * the blue channel is the translucency intensity.
* * the blue (alpha if useGltfStyleTextures = true) channel is the translucency intensity.
*/
@serializeAsTexture()
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
public translucencyIntensityTexture: Nullable<BaseTexture> = null;

private _scene: Scene;
private _useGltfStyleTextures = false;
/**
* Defines the translucency tint of the material.
* If not set, the tint color will be used instead.
*/
@serializeAsColor3()
public translucencyColor: Nullable<Color3> = null;

private _translucencyColorTexture: Nullable<BaseTexture> = null;
/**
* Defines the translucency tint color of the material as a texture.
* This is multiplied against the translucency color to add variety and realism to the material.
* If translucencyColor is not set, the tint color will be used instead.
*/
@serializeAsTexture()
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
public translucencyColorTexture: Nullable<BaseTexture> = null;

private _useGltfStyleTextures = true;
/**
* Use channels layout used by glTF:
* * thicknessTexture: the green (instead of red) channel is the thickness
* * thicknessTexture/refractionIntensityTexture: the red (instead of green) channel is the refraction intensity
* * thicknessTexture/translucencyIntensityTexture: no change, use the blue channel for the translucency intensity
* * thicknessTexture/translucencyIntensityTexture: the alpha (instead of blue) channel is the translucency intensity
*/
@serialize()
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
public useGltfStyleTextures: boolean = false;
public useGltfStyleTextures: boolean = true;

private _scene: Scene;

/** @internal */
private _internalMarkAllSubMeshesAsTexturesDirty: () => void;
Expand Down Expand Up @@ -353,6 +373,12 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
}
}

if (this._translucencyColorTexture && MaterialFlags.TranslucencyColorTextureEnabled) {
if (!this._translucencyColorTexture.isReadyOrNotBlocking()) {
return false;
}
}

const refractionTexture = this._getRefractionTexture(scene);
if (refractionTexture && MaterialFlags.RefractionTextureEnabled) {
if (!refractionTexture.isReadyOrNotBlocking()) {
Expand Down Expand Up @@ -393,6 +419,8 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
defines.SS_USE_LOCAL_REFRACTIONMAP_CUBIC = false;
defines.SS_USE_THICKNESS_AS_DEPTH = false;
defines.SS_USE_GLTF_TEXTURES = false;
defines.SS_TRANSLUCENCYCOLOR_TEXTURE = false;
defines.SS_TRANSLUCENCYCOLOR_TEXTUREDIRECTUV = 0;
return;
}

Expand Down Expand Up @@ -421,6 +449,7 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
defines.SS_ALBEDOFORTRANSLUCENCYTINT = false;
defines.SS_USE_LOCAL_REFRACTIONMAP_CUBIC = false;
defines.SS_USE_THICKNESS_AS_DEPTH = false;
defines.SS_TRANSLUCENCYCOLOR_TEXTURE = false;

if (defines._areTexturesDirty) {
if (scene.texturesEnabled) {
Expand All @@ -435,6 +464,10 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
if (this._translucencyIntensityTexture && MaterialFlags.TranslucencyIntensityTextureEnabled) {
PrepareDefinesForMergedUV(this._translucencyIntensityTexture, defines, "SS_TRANSLUCENCYINTENSITY_TEXTURE");
}

if (this._translucencyColorTexture && MaterialFlags.TranslucencyColorTextureEnabled) {
PrepareDefinesForMergedUV(this._translucencyColorTexture, defines, "SS_TRANSLUCENCYCOLOR_TEXTURE");
}
}
}

Expand Down Expand Up @@ -511,9 +544,9 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
BindTextureMatrix(this._refractionIntensityTexture, uniformBuffer, "refractionIntensity");
}

if (this._translucencyIntensityTexture && MaterialFlags.TranslucencyIntensityTextureEnabled && defines.SS_TRANSLUCENCYINTENSITY_TEXTURE) {
uniformBuffer.updateFloat2("vTranslucencyIntensityInfos", this._translucencyIntensityTexture.coordinatesIndex, this._translucencyIntensityTexture.level);
BindTextureMatrix(this._translucencyIntensityTexture, uniformBuffer, "translucencyIntensity");
if (this._translucencyColorTexture && MaterialFlags.TranslucencyColorTextureEnabled && defines.SS_TRANSLUCENCYCOLOR_TEXTURE) {
uniformBuffer.updateFloat2("vTranslucencyColorInfos", this._translucencyColorTexture.coordinatesIndex, this._translucencyColorTexture.level);
BindTextureMatrix(this._translucencyColorTexture, uniformBuffer, "translucencyColor");
}

if (refractionTexture && MaterialFlags.RefractionTextureEnabled) {
Expand Down Expand Up @@ -555,6 +588,7 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
uniformBuffer.updateColor3("vDiffusionDistance", this.diffusionDistance);

uniformBuffer.updateFloat4("vTintColor", this.tintColor.r, this.tintColor.g, this.tintColor.b, Math.max(0.00001, this.tintColorAtDistance));
uniformBuffer.updateColor4("vTranslucencyColor", this.translucencyColor ?? this.tintColor, 0);

uniformBuffer.updateFloat3("vSubSurfaceIntensity", this.refractionIntensity, this.translucencyIntensity, 0);

Expand All @@ -575,6 +609,10 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
uniformBuffer.setTexture("translucencyIntensitySampler", this._translucencyIntensityTexture);
}

if (this._translucencyColorTexture && MaterialFlags.TranslucencyColorTextureEnabled && defines.SS_TRANSLUCENCYCOLOR_TEXTURE) {
uniformBuffer.setTexture("translucencyColorSampler", this._translucencyColorTexture);
}

if (refractionTexture && MaterialFlags.RefractionTextureEnabled) {
if (lodBasedMicrosurface) {
uniformBuffer.setTexture("refractionSampler", refractionTexture);
Expand Down Expand Up @@ -639,6 +677,10 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
return true;
}

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

return false;
}

Expand All @@ -658,6 +700,10 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
if (this._refractionTexture) {
activeTextures.push(this._refractionTexture);
}

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

public getAnimatables(animatables: IAnimatable[]): void {
Expand All @@ -668,6 +714,10 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
if (this._refractionTexture && this._refractionTexture.animations && this._refractionTexture.animations.length > 0) {
animatables.push(this._refractionTexture);
}

if (this._translucencyColorTexture && this._translucencyColorTexture.animations && this._translucencyColorTexture.animations.length > 0) {
animatables.push(this._translucencyColorTexture);
}
}

public dispose(forceDisposeTextures?: boolean): void {
Expand All @@ -679,6 +729,10 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
if (this._refractionTexture) {
this._refractionTexture.dispose();
}

if (this._translucencyColorTexture) {
this._translucencyColorTexture.dispose();
}
}
}

Expand All @@ -697,7 +751,15 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
}

public getSamplers(samplers: string[]): void {
samplers.push("thicknessSampler", "refractionIntensitySampler", "translucencyIntensitySampler", "refractionSampler", "refractionSamplerLow", "refractionSamplerHigh");
samplers.push(
"thicknessSampler",
"refractionIntensitySampler",
"translucencyIntensitySampler",
"refractionSampler",
"refractionSamplerLow",
"refractionSamplerHigh",
"translucencyColorSampler"
);
}

public getUniforms(): { ubo?: Array<{ name: string; size: number; type: string }>; vertex?: string; fragment?: string } {
Expand All @@ -721,6 +783,10 @@ export class PBRSubSurfaceConfiguration extends MaterialPluginBase {
{ name: "vRefractionSize", size: 3, type: "vec3" },
{ name: "scatteringDiffusionProfile", size: 1, type: "float" },
{ name: "dispersion", size: 1, type: "float" },

{ name: "vTranslucencyColor", size: 4, type: "vec4" },
{ name: "vTranslucencyColorInfos", size: 2, type: "vec2" },
{ name: "translucencyColorMatrix", size: 16, type: "mat4" },
],
};
}
Expand Down
18 changes: 17 additions & 1 deletion packages/dev/core/src/Materials/materialFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export class MaterialFlags {
* Are translucency intensity textures enabled in the application.
*/
public static get TranslucencyIntensityTextureEnabled(): boolean {
return this._ThicknessTextureEnabled;
return this._TranslucencyIntensityTextureEnabled;
}
public static set TranslucencyIntensityTextureEnabled(value: boolean) {
if (this._TranslucencyIntensityTextureEnabled === value) {
Expand All @@ -342,6 +342,22 @@ export class MaterialFlags {
Engine.MarkAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag);
}

private static _TranslucencyColorTextureEnabled = true;
/**
* Are translucency tint textures enabled in the application.
*/
public static get TranslucencyColorTextureEnabled(): boolean {
return this._TranslucencyColorTextureEnabled;
}
public static set TranslucencyColorTextureEnabled(value: boolean) {
if (this._TranslucencyColorTextureEnabled === value) {
return;
}

this._TranslucencyColorTextureEnabled = value;
Engine.MarkAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag);
}

private static _IridescenceTextureEnabled = true;
/**
* Are translucency intensity textures enabled in the application.
Expand Down
28 changes: 23 additions & 5 deletions packages/dev/core/src/Shaders/ShadersInclude/pbrBlockSubSurface.fx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ struct subSurfaceOutParams
#endif
#ifdef SS_TRANSLUCENCY
in vec3 vDiffusionDistance,
in vec4 vTranslucencyColor,
#ifdef SS_TRANSLUCENCYCOLOR_TEXTURE
in vec4 translucencyColorMap,
#endif
#endif
out subSurfaceOutParams outParams
)
Expand All @@ -258,12 +262,13 @@ struct subSurfaceOutParams
outParams.alpha = 1.0;
#endif
#endif

#ifdef SS_TRANSLUCENCY
float translucencyIntensity = vSubSurfaceIntensity.y;
#endif

#ifdef SS_THICKNESSANDMASK_TEXTURE
#if defined(SS_USE_GLTF_TEXTURES)
#ifdef SS_USE_GLTF_TEXTURES
float thickness = thicknessMap.g * vThicknessParam.y + vThicknessParam.x;
#else
float thickness = thicknessMap.r * vThicknessParam.y + vThicknessParam.x;
Expand All @@ -274,15 +279,19 @@ struct subSurfaceOutParams
#endif

#if defined(SS_REFRACTION) && defined(SS_REFRACTION_USE_INTENSITY_FROM_THICKNESS)
#if defined(SS_USE_GLTF_TEXTURES)
#ifdef SS_USE_GLTF_TEXTURES
refractionIntensity *= thicknessMap.r;
#else
refractionIntensity *= thicknessMap.g;
#endif
#endif

#if defined(SS_TRANSLUCENCY) && defined(SS_TRANSLUCENCY_USE_INTENSITY_FROM_THICKNESS)
translucencyIntensity *= thicknessMap.b;
#ifdef SS_USE_GLTF_TEXTURES
translucencyIntensity *= thicknessMap.a;
#else
translucencyIntensity *= thicknessMap.b;
#endif
#endif
#else
float thickness = vThicknessParam.y;
Expand All @@ -297,15 +306,24 @@ struct subSurfaceOutParams
#endif

#if defined(SS_TRANSLUCENCY) && defined(SS_TRANSLUCENCYINTENSITY_TEXTURE)
translucencyIntensity *= translucencyIntensityMap.b;
#ifdef SS_USE_GLTF_TEXTURES
translucencyIntensity *= translucencyIntensityMap.a;
#else
translucencyIntensity *= translucencyIntensityMap.b;
#endif
#endif

// _________________________________________________________________________________________
// _____________________________ Translucency transmittance ________________________________
// _________________________________________________________________________________________
#ifdef SS_TRANSLUCENCY
thickness = maxEps(thickness);
vec3 transmittance = transmittanceBRDF_Burley(vTintColor.rgb, vDiffusionDistance, thickness);
vec4 translucencyColor = vTranslucencyColor;
#ifdef SS_TRANSLUCENCYCOLOR_TEXTURE
translucencyColor *= translucencyColorMap;
#endif

vec3 transmittance = transmittanceBRDF_Burley(translucencyColor.rgb, vDiffusionDistance, thickness);
transmittance *= translucencyIntensity;
outParams.transmittance = transmittance;
outParams.translucencyIntensity = translucencyIntensity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ uniform mat4 view;
uniform vec3 vDiffusionDistance;
uniform vec4 vTintColor;
uniform vec3 vSubSurfaceIntensity;

uniform vec4 vTranslucencyColor;

#ifdef SS_TRANSLUCENCYCOLOR_TEXTURE
uniform vec2 vTranslucencyColorInfos;
uniform mat4 translucencyColorMatrix;
#endif
#endif

#ifdef PREPASS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@
#include<samplerFragmentDeclaration>(_DEFINENAME_,SS_THICKNESSANDMASK_TEXTURE,_VARYINGNAME_,Thickness,_SAMPLERNAME_,thickness)
#include<samplerFragmentDeclaration>(_DEFINENAME_,SS_REFRACTIONINTENSITY_TEXTURE,_VARYINGNAME_,RefractionIntensity,_SAMPLERNAME_,refractionIntensity)
#include<samplerFragmentDeclaration>(_DEFINENAME_,SS_TRANSLUCENCYINTENSITY_TEXTURE,_VARYINGNAME_,TranslucencyIntensity,_SAMPLERNAME_,translucencyIntensity)
#include<samplerFragmentDeclaration>(_DEFINENAME_,SS_TRANSLUCENCYCOLOR_TEXTURE,_VARYINGNAME_,TranslucencyColor,_SAMPLERNAME_,translucencyColor)
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ uniform float pointSize;
uniform vec2 vTranslucencyIntensityInfos;
uniform mat4 translucencyIntensityMatrix;
#endif

#ifdef SS_TRANSLUCENCYCOLOR_TEXTURE
uniform vec2 vTranslucencyColorInfos;
uniform mat4 translucencyColorMatrix;
#endif
#endif

#ifdef NORMAL
Expand Down
8 changes: 8 additions & 0 deletions packages/dev/core/src/Shaders/pbr.fragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ void main(void) {
vec4 translucencyIntensityMap = texture2D(translucencyIntensitySampler, vTranslucencyIntensityUV + uvOffset);
#endif

#ifdef SS_TRANSLUCENCYCOLOR_TEXTURE
vec4 translucencyColorMap = texture2D(translucencyColorSampler, vTranslucencyColorUV + uvOffset);
#endif

subSurfaceBlock(
vSubSurfaceIntensity,
vThicknessParam,
Expand Down Expand Up @@ -592,6 +596,10 @@ void main(void) {
#endif
#ifdef SS_TRANSLUCENCY
vDiffusionDistance,
vTranslucencyColor,
#ifdef SS_TRANSLUCENCYCOLOR_TEXTURE
translucencyColorMap,
#endif
#endif
subSurfaceOut
);
Expand Down
Loading
Loading