Skip to content
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
9 changes: 9 additions & 0 deletions packages/dev/core/src/Materials/PBR/openpbrMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export class OpenPBRMaterialDefines extends ImageProcessingDefinesMixin(OpenPBRM
public COAT_ROUGHNESS_ANISOTROPY_FROM_TANGENT_TEXTURE = false;
public USE_GLTF_STYLE_ANISOTROPY = false;
public THIN_FILM_THICKNESS_FROM_THIN_FILM_TEXTURE = false;
public FUZZ_ROUGHNESS_FROM_TEXTURE_ALPHA = false;

public ENVIRONMENTBRDF = false;
public ENVIRONMENTBRDF_RGBD = false;
Expand Down Expand Up @@ -1241,6 +1242,13 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
*/
public _useGltfStyleAnisotropy = false;

/**
* Specifies that the fuzz roughness is stored in the alpha channel of the texture.
* This is for compatibility with glTF where the fuzz roughness is often stored in
* the alpha channel of the fuzz color texture.
*/
public _useFuzzRoughnessFromTextureAlpha = false;

/**
* This parameters will enable/disable Horizon occlusion to prevent normal maps to look shiny when the normal
* makes the reflect vector face the model (under horizon).
Expand Down Expand Up @@ -2621,6 +2629,7 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
defines.SPECULAR_ROUGHNESS_ANISOTROPY_FROM_TANGENT_TEXTURE = this._useSpecularRoughnessAnisotropyFromTangentTexture;
defines.COAT_ROUGHNESS_ANISOTROPY_FROM_TANGENT_TEXTURE = this._useCoatRoughnessAnisotropyFromTangentTexture;
defines.ROUGHNESSSTOREINMETALMAPGREEN = this._useRoughnessFromMetallicTextureGreen;
defines.FUZZ_ROUGHNESS_FROM_TEXTURE_ALPHA = this._useFuzzRoughnessFromTextureAlpha;
defines.METALLNESSSTOREINMETALMAPBLUE = this._useMetallicFromMetallicTextureBlue;
defines.THIN_FILM_THICKNESS_FROM_THIN_FILM_TEXTURE = this._useThinFilmThicknessFromTextureGreen;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fuzz_roughness = vFuzzRoughness;
fuzz_color *= vFuzzColorInfos.y;
#endif

#if defined(FUZZ_ROUGHNESS) && defined(FUZZ_ROUGHNESS_FROM_COLOR_TEXTURE_ALPHA)
#if defined(FUZZ_ROUGHNESS) && defined(FUZZ_ROUGHNESS_FROM_TEXTURE_ALPHA)
fuzz_roughness *= fuzzRoughnessFromTexture.a;
#elif defined(FUZZ_ROUGHNESS)
fuzz_roughness *= fuzzRoughnessFromTexture.r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
vec2 UV = vec2(perceptualRoughness, NdotV);

// We can find the scale and offset to apply to the specular value.
vec4 brdfLookup = texture(environmentFuzzBrdfSampler, UV);
vec4 brdfLookup = texture2D(environmentFuzzBrdfSampler, UV);

const vec2 RiRange = vec2(0.0, 0.75);
const vec2 ARange = vec2(0.005, 0.88);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fuzz_roughness = uniforms.vFuzzRoughness;
fuzz_color *= uniforms.vFuzzColorInfos.y;
#endif

#if defined(FUZZ_ROUGHNESS) && defined(FUZZ_ROUGHNESS_FROM_COLOR_TEXTURE_ALPHA)
#if defined(FUZZ_ROUGHNESS) && defined(FUZZ_ROUGHNESS_FROM_TEXTURE_ALPHA)
fuzz_roughness *= fuzzRoughnessFromTexture.a;
#elif defined(FUZZ_ROUGHNESS)
fuzz_roughness *= fuzzRoughnessFromTexture.r;
Expand Down
119 changes: 119 additions & 0 deletions packages/dev/loaders/src/glTF/2.0/Extensions/KHR_materials_fuzz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import type { Nullable } from "core/types";
import type { Material } from "core/Materials/material";
import type { IMaterial, ITextureInfo } from "../glTFLoaderInterfaces";
import type { IGLTFLoaderExtension } from "../glTFLoaderExtension";
import { GLTFLoader } from "../glTFLoader";
import { Color3 } from "core/Maths/math.color";
import type { IKHRMaterialsFuzz } from "babylonjs-gltf2interface";
import { registerGLTFExtension, unregisterGLTFExtension } from "../glTFLoaderExtensionRegistry";

const NAME = "KHR_materials_fuzz";

declare module "../../glTFFileLoader" {
// eslint-disable-next-line jsdoc/require-jsdoc, @typescript-eslint/naming-convention
export interface GLTFLoaderExtensionOptions {
/**
* Defines options for the KHR_materials_fuzz extension.
*/
// NOTE: Don't use NAME here as it will break the UMD type declarations.
["KHR_materials_fuzz"]: {};
}
}

/**
* [Specification]
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export class KHR_materials_fuzz implements IGLTFLoaderExtension {
/**
* The name of this extension.
*/
public readonly name = NAME;

/**
* Defines whether this extension is enabled.
*/
public enabled: boolean;

/**
* Defines a number that determines the order the extensions are applied.
*/
public order = 190;

private _loader: GLTFLoader;

/**
* @internal
*/
constructor(loader: GLTFLoader) {
this._loader = loader;
this.enabled = this._loader.isExtensionUsed(NAME);
}

/** @internal */
public dispose() {
(this._loader as any) = null;
}

/**
* @internal
*/
// eslint-disable-next-line no-restricted-syntax
public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material): Nullable<Promise<void>> {
return GLTFLoader.LoadExtensionAsync<IKHRMaterialsFuzz>(context, material, this.name, async (extensionContext, extension) => {
const promises = new Array<Promise<any>>();
promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial));
promises.push(this._loadFuzzPropertiesAsync(extensionContext, extension, babylonMaterial));
// eslint-disable-next-line github/no-then
return await Promise.all(promises).then(() => {});
});
}

// eslint-disable-next-line @typescript-eslint/promise-function-async, no-restricted-syntax
private _loadFuzzPropertiesAsync(context: string, properties: IKHRMaterialsFuzz, babylonMaterial: Material): Promise<void> {
const adapter = this._loader._getOrCreateMaterialAdapter(babylonMaterial);
const promises = new Array<Promise<any>>();

adapter.configureFuzz();

// Set non-texture properties immediately
adapter.fuzzWeight = properties.fuzzFactor !== undefined ? properties.fuzzFactor : 0.0;
adapter.fuzzColor = properties.fuzzColorFactor !== undefined ? Color3.FromArray(properties.fuzzColorFactor) : Color3.White();
adapter.fuzzRoughness = properties.fuzzRoughnessFactor !== undefined ? properties.fuzzRoughnessFactor : 0.5;

// Load textures
if (properties.fuzzTexture) {
promises.push(
this._loader.loadTextureInfoAsync(`${context}/fuzzTexture`, properties.fuzzTexture, (texture) => {
texture.name = `${babylonMaterial.name} (Fuzz)`;
adapter.fuzzWeightTexture = texture;
})
);
}

if (properties.fuzzColorTexture) {
promises.push(
this._loader.loadTextureInfoAsync(`${context}/fuzzColorTexture`, properties.fuzzColorTexture, (texture) => {
texture.name = `${babylonMaterial.name} (Fuzz Color)`;
adapter.fuzzColorTexture = texture;
})
);
}

if (properties.fuzzRoughnessTexture) {
(properties.fuzzRoughnessTexture as ITextureInfo).nonColorData = true;
promises.push(
this._loader.loadTextureInfoAsync(`${context}/fuzzRoughnessTexture`, properties.fuzzRoughnessTexture, (texture) => {
texture.name = `${babylonMaterial.name} (Fuzz Roughness)`;
adapter.fuzzRoughnessTexture = texture;
})
);
}

// eslint-disable-next-line github/no-then
return Promise.all(promises).then(() => {});
}
}

unregisterGLTFExtension(NAME);
registerGLTFExtension(NAME, true, (loader) => new KHR_materials_fuzz(loader));
1 change: 1 addition & 0 deletions packages/dev/loaders/src/glTF/2.0/Extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from "./KHR_materials_iridescence";
export * from "./KHR_materials_anisotropy";
export * from "./KHR_materials_emissive_strength";
export * from "./KHR_materials_sheen";
export * from "./KHR_materials_fuzz";
export * from "./KHR_materials_specular";
export * from "./KHR_materials_ior";
export * from "./KHR_materials_variants";
Expand Down
Loading
Loading