|
| 1 | +/* eslint-disable github/no-then */ |
| 2 | +import type { Nullable } from "core/types"; |
| 3 | +import type { PBRMaterial } from "core/Materials/PBR/pbrMaterial"; |
| 4 | +import type { OpenPBRMaterial } from "core/Materials/PBR/openPbrMaterial"; |
| 5 | +import type { Material } from "core/Materials/material"; |
| 6 | +import type { BaseTexture } from "core/Materials/Textures/baseTexture"; |
| 7 | +import type { IMaterial, ITextureInfo } from "../glTFLoaderInterfaces"; |
| 8 | +import type { IGLTFLoaderExtension } from "../glTFLoaderExtension"; |
| 9 | +import { GLTFLoader } from "../glTFLoader"; |
| 10 | +import type { IEXTMaterialsClearcoatDarkening } from "babylonjs-gltf2interface"; |
| 11 | +import { registerGLTFExtension, unregisterGLTFExtension } from "../glTFLoaderExtensionRegistry"; |
| 12 | + |
| 13 | +const NAME = "EXT_materials_clearcoat_darkening"; |
| 14 | + |
| 15 | +declare module "../../glTFFileLoader" { |
| 16 | + // eslint-disable-next-line jsdoc/require-jsdoc, @typescript-eslint/naming-convention |
| 17 | + export interface GLTFLoaderExtensionOptions { |
| 18 | + /** |
| 19 | + * Defines options for the EXT_materials_clearcoat_darkening extension. |
| 20 | + */ |
| 21 | + // NOTE: Don't use NAME here as it will break the UMD type declarations. |
| 22 | + ["EXT_materials_clearcoat_darkening"]: {}; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +let PBRMaterialClass: typeof PBRMaterial | typeof OpenPBRMaterial; |
| 27 | + |
| 28 | +/** |
| 29 | + * [Proposed Specification](https://github.com/KhronosGroup/glTF/pull/2518) |
| 30 | + * !!! Experimental Extension Subject to Changes !!! |
| 31 | + */ |
| 32 | +// eslint-disable-next-line @typescript-eslint/naming-convention |
| 33 | +export class EXT_materials_clearcoat_darkening implements IGLTFLoaderExtension { |
| 34 | + /** |
| 35 | + * The name of this extension. |
| 36 | + */ |
| 37 | + public readonly name = NAME; |
| 38 | + |
| 39 | + /** |
| 40 | + * Defines whether this extension is enabled. |
| 41 | + */ |
| 42 | + public enabled: boolean; |
| 43 | + |
| 44 | + /** |
| 45 | + * Defines a number that determines the order the extensions are applied. |
| 46 | + */ |
| 47 | + public order = 191; |
| 48 | + |
| 49 | + private _loader: GLTFLoader; |
| 50 | + |
| 51 | + /** |
| 52 | + * @internal |
| 53 | + */ |
| 54 | + constructor(loader: GLTFLoader) { |
| 55 | + this._loader = loader; |
| 56 | + this.enabled = this._loader.isExtensionUsed(NAME); |
| 57 | + } |
| 58 | + |
| 59 | + /** @internal */ |
| 60 | + public dispose() { |
| 61 | + (this._loader as any) = null; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @internal |
| 66 | + */ |
| 67 | + // eslint-disable-next-line no-restricted-syntax |
| 68 | + public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material, useOpenPBR: boolean = false): Nullable<Promise<void>> { |
| 69 | + return GLTFLoader.LoadExtensionAsync<IEXTMaterialsClearcoatDarkening>(context, material, this.name, async (extensionContext, extension) => { |
| 70 | + const promises = new Array<Promise<any>>(); |
| 71 | + promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial)); |
| 72 | + if (useOpenPBR) { |
| 73 | + const mod = await import("core/Materials/PBR/openPbrMaterial"); |
| 74 | + PBRMaterialClass = mod.OpenPBRMaterial; |
| 75 | + } else { |
| 76 | + // Logger.Warn(`${extensionContext}: The EXT_materials_clearcoat_darkening extension is only supported with OpenPBR materials. Falling back to PBRMaterial.`); |
| 77 | + return await Promise.resolve(); |
| 78 | + } |
| 79 | + promises.push(this._loadDarkeningPropertiesAsync(extensionContext, material, babylonMaterial, extension)); |
| 80 | + return await Promise.all(promises).then(() => {}); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + // eslint-disable-next-line no-restricted-syntax, @typescript-eslint/promise-function-async |
| 85 | + private _loadDarkeningPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material, extension: IEXTMaterialsClearcoatDarkening): Promise<void> { |
| 86 | + if (!(babylonMaterial instanceof PBRMaterialClass)) { |
| 87 | + throw new Error(`${context}: Material type not supported`); |
| 88 | + } |
| 89 | + |
| 90 | + let darkeningFactor = 1.0; |
| 91 | + let darkeningFactorTexture: Nullable<BaseTexture>; |
| 92 | + |
| 93 | + if (extension.clearcoatDarkeningFactor !== undefined) { |
| 94 | + darkeningFactor = extension.clearcoatDarkeningFactor; |
| 95 | + } |
| 96 | + |
| 97 | + let texturePromise = Promise.resolve(); |
| 98 | + |
| 99 | + if (extension.clearcoatDarkeningTexture) { |
| 100 | + (extension.clearcoatDarkeningTexture as ITextureInfo).nonColorData = true; |
| 101 | + texturePromise = this._loader.loadTextureInfoAsync(`${context}/clearcoatDarkeningTexture`, extension.clearcoatDarkeningTexture).then((texture: BaseTexture) => { |
| 102 | + texture.name = `${babylonMaterial.name} (Clearcoat Darkening)`; |
| 103 | + darkeningFactorTexture = texture; |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + return texturePromise.then(() => { |
| 108 | + const openpbrMaterial = babylonMaterial as OpenPBRMaterial; |
| 109 | + openpbrMaterial.coatDarkening = darkeningFactor; |
| 110 | + if (darkeningFactorTexture) { |
| 111 | + openpbrMaterial.coatDarkeningTexture = darkeningFactorTexture; |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +unregisterGLTFExtension(NAME); |
| 118 | +registerGLTFExtension(NAME, true, (loader) => new EXT_materials_clearcoat_darkening(loader)); |
0 commit comments