Skip to content

Commit

Permalink
Merge pull request BabylonJS#3 from alexchuber/uncomment-material-tex…
Browse files Browse the repository at this point in the history
…ture-extensions

Restoring all material and texture extensions
  • Loading branch information
SergioRZMasson authored Nov 14, 2024
2 parents bd4f274 + 61831d1 commit c44f379
Show file tree
Hide file tree
Showing 16 changed files with 1,208 additions and 1,234 deletions.
Original file line number Diff line number Diff line change
@@ -1,83 +1,84 @@
// import type { IMaterial, IKHRMaterialsAnisotropy } from "babylonjs-gltf2interface";
// import type { IGLTFExporterExtensionV2 } from "../glTFExporterExtension";
// import { GLTFExporter } from "../glTFExporter";
// import type { Material } from "core/Materials/material";
// import { PBRBaseMaterial } from "core/Materials/PBR/pbrBaseMaterial";
// import type { BaseTexture } from "core/Materials/Textures/baseTexture";

// const NAME = "KHR_materials_anisotropy";

// /**
// * @internal
// */
// // eslint-disable-next-line @typescript-eslint/naming-convention
// export class KHR_materials_anisotropy implements IGLTFExporterExtensionV2 {
// /** Name of this extension */
// public readonly name = NAME;

// /** Defines whether this extension is enabled */
// public enabled = true;

// /** Defines whether this extension is required */
// public required = false;

// private _exporter: GLTFExporter;

// private _wasUsed = false;

// constructor(exporter: GLTFExporter) {
// this._exporter = exporter;
// }

// public dispose() {}

// /** @internal */
// public get wasUsed() {
// return this._wasUsed;
// }

// public postExportMaterialAdditionalTextures?(context: string, node: IMaterial, babylonMaterial: Material): BaseTexture[] {
// const additionalTextures: BaseTexture[] = [];
// if (babylonMaterial instanceof PBRBaseMaterial) {
// if (babylonMaterial.anisotropy.isEnabled && !babylonMaterial.anisotropy.legacy) {
// if (babylonMaterial.anisotropy.texture) {
// additionalTextures.push(babylonMaterial.anisotropy.texture);
// }
// return additionalTextures;
// }
// }

// return [];
// }

// public postExportMaterialAsync?(context: string, node: IMaterial, babylonMaterial: Material): Promise<IMaterial> {
// return new Promise((resolve) => {
// if (babylonMaterial instanceof PBRBaseMaterial) {
// if (!babylonMaterial.anisotropy.isEnabled || babylonMaterial.anisotropy.legacy) {
// resolve(node);
// return;
// }

// this._wasUsed = true;

// node.extensions = node.extensions || {};

// const anisotropyTextureInfo = this._exporter._glTFMaterialExporter._getTextureInfo(babylonMaterial.anisotropy.texture);

// const anisotropyInfo: IKHRMaterialsAnisotropy = {
// anisotropyStrength: babylonMaterial.anisotropy.intensity,
// anisotropyRotation: babylonMaterial.anisotropy.angle,
// anisotropyTexture: anisotropyTextureInfo ?? undefined,
// hasTextures: () => {
// return anisotropyInfo.anisotropyTexture !== null;
// },
// };

// node.extensions[NAME] = anisotropyInfo;
// }
// resolve(node);
// });
// }
// }

// GLTFExporter.RegisterExtension(NAME, (exporter) => new KHR_materials_anisotropy(exporter));
import type { IMaterial, IKHRMaterialsAnisotropy } from "babylonjs-gltf2interface";
import type { IGLTFExporterExtensionV2 } from "../glTFExporterExtension";
import { GLTFExporter } from "../glTFExporter";
import type { Material } from "core/Materials/material";
import { PBRBaseMaterial } from "core/Materials/PBR/pbrBaseMaterial";
import type { BaseTexture } from "core/Materials/Textures/baseTexture";

const NAME = "KHR_materials_anisotropy";

/**
* @internal
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export class KHR_materials_anisotropy implements IGLTFExporterExtensionV2 {
/** Name of this extension */
public readonly name = NAME;

/** Defines whether this extension is enabled */
public enabled = true;

/** Defines whether this extension is required */
public required = false;

private _exporter: GLTFExporter;

private _wasUsed = false;

constructor(exporter: GLTFExporter) {
this._exporter = exporter;
}

public dispose() {}

/** @internal */
public get wasUsed() {
return this._wasUsed;
}

public postExportMaterialAdditionalTextures?(context: string, node: IMaterial, babylonMaterial: Material): BaseTexture[] {
const additionalTextures: BaseTexture[] = [];
if (babylonMaterial instanceof PBRBaseMaterial) {
if (babylonMaterial.anisotropy.isEnabled && !babylonMaterial.anisotropy.legacy) {
if (babylonMaterial.anisotropy.texture) {
additionalTextures.push(babylonMaterial.anisotropy.texture);
}
return additionalTextures;
}
}

return [];
}

public postExportMaterialAsync?(context: string, node: IMaterial, babylonMaterial: Material): Promise<IMaterial> {
return new Promise((resolve) => {
if (babylonMaterial instanceof PBRBaseMaterial) {
if (!babylonMaterial.anisotropy.isEnabled || babylonMaterial.anisotropy.legacy) {
resolve(node);
return;
}

this._wasUsed = true;

node.extensions = node.extensions || {};

const anisotropyTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.anisotropy.texture);

const anisotropyInfo: IKHRMaterialsAnisotropy = {
anisotropyStrength: babylonMaterial.anisotropy.intensity,
anisotropyRotation: babylonMaterial.anisotropy.angle,
anisotropyTexture: anisotropyTextureInfo ?? undefined,
};

if (anisotropyInfo.anisotropyTexture !== null) {
this._exporter._materialNeedsUVsSet.add(babylonMaterial);
}

node.extensions[NAME] = anisotropyInfo;
}
resolve(node);
});
}
}

GLTFExporter.RegisterExtension(NAME, (exporter) => new KHR_materials_anisotropy(exporter));
Original file line number Diff line number Diff line change
@@ -1,109 +1,110 @@
// import type { IMaterial, IKHRMaterialsClearcoat } from "babylonjs-gltf2interface";
// import type { IGLTFExporterExtensionV2 } from "../glTFExporterExtension";
// import { GLTFExporter } from "../glTFExporter";
// import type { Material } from "core/Materials/material";
// import { PBRBaseMaterial } from "core/Materials/PBR/pbrBaseMaterial";
// import type { BaseTexture } from "core/Materials/Textures/baseTexture";

// import { Tools } from "core/Misc/tools";

// const NAME = "KHR_materials_clearcoat";

// /**
// * @internal
// */
// // eslint-disable-next-line @typescript-eslint/naming-convention
// export class KHR_materials_clearcoat implements IGLTFExporterExtensionV2 {
// /** Name of this extension */
// public readonly name = NAME;

// /** Defines whether this extension is enabled */
// public enabled = true;

// /** Defines whether this extension is required */
// public required = false;

// private _exporter: GLTFExporter;

// private _wasUsed = false;

// constructor(exporter: GLTFExporter) {
// this._exporter = exporter;
// }

// public dispose() {}

// /** @internal */
// public get wasUsed() {
// return this._wasUsed;
// }

// public postExportMaterialAdditionalTextures?(context: string, node: IMaterial, babylonMaterial: Material): BaseTexture[] {
// const additionalTextures: BaseTexture[] = [];
// if (babylonMaterial instanceof PBRBaseMaterial) {
// if (babylonMaterial.clearCoat.isEnabled) {
// if (babylonMaterial.clearCoat.texture) {
// additionalTextures.push(babylonMaterial.clearCoat.texture);
// }
// if (!babylonMaterial.clearCoat.useRoughnessFromMainTexture && babylonMaterial.clearCoat.textureRoughness) {
// additionalTextures.push(babylonMaterial.clearCoat.textureRoughness);
// }
// if (babylonMaterial.clearCoat.bumpTexture) {
// additionalTextures.push(babylonMaterial.clearCoat.bumpTexture);
// }
// return additionalTextures;
// }
// }

// return [];
// }

// public postExportMaterialAsync?(context: string, node: IMaterial, babylonMaterial: Material): Promise<IMaterial> {
// return new Promise((resolve) => {
// if (babylonMaterial instanceof PBRBaseMaterial) {
// if (!babylonMaterial.clearCoat.isEnabled) {
// resolve(node);
// return;
// }

// this._wasUsed = true;

// node.extensions = node.extensions || {};

// const clearCoatTextureInfo = this._exporter._glTFMaterialExporter._getTextureInfo(babylonMaterial.clearCoat.texture);
// let clearCoatTextureRoughnessInfo;
// if (babylonMaterial.clearCoat.useRoughnessFromMainTexture) {
// clearCoatTextureRoughnessInfo = this._exporter._glTFMaterialExporter._getTextureInfo(babylonMaterial.clearCoat.texture);
// } else {
// clearCoatTextureRoughnessInfo = this._exporter._glTFMaterialExporter._getTextureInfo(babylonMaterial.clearCoat.textureRoughness);
// }

// if (babylonMaterial.clearCoat.isTintEnabled) {
// Tools.Warn(`Clear Color tint is not supported for glTF export. Ignoring for: ${babylonMaterial.name}`);
// }

// if (babylonMaterial.clearCoat.remapF0OnInterfaceChange) {
// Tools.Warn(`Clear Color F0 remapping is not supported for glTF export. Ignoring for: ${babylonMaterial.name}`);
// }

// const clearCoatNormalTextureInfo = this._exporter._glTFMaterialExporter._getTextureInfo(babylonMaterial.clearCoat.bumpTexture);

// const clearCoatInfo: IKHRMaterialsClearcoat = {
// clearcoatFactor: babylonMaterial.clearCoat.intensity,
// clearcoatTexture: clearCoatTextureInfo ?? undefined,
// clearcoatRoughnessFactor: babylonMaterial.clearCoat.roughness,
// clearcoatRoughnessTexture: clearCoatTextureRoughnessInfo ?? undefined,
// clearcoatNormalTexture: clearCoatNormalTextureInfo ?? undefined,
// hasTextures: () => {
// return clearCoatInfo.clearcoatTexture !== null || clearCoatInfo.clearcoatRoughnessTexture !== null || clearCoatInfo.clearcoatRoughnessTexture !== null;
// },
// };

// node.extensions[NAME] = clearCoatInfo;
// }
// resolve(node);
// });
// }
// }

// GLTFExporter.RegisterExtension(NAME, (exporter) => new KHR_materials_clearcoat(exporter));
import type { IMaterial, IKHRMaterialsClearcoat } from "babylonjs-gltf2interface";
import type { IGLTFExporterExtensionV2 } from "../glTFExporterExtension";
import { GLTFExporter } from "../glTFExporter";
import type { Material } from "core/Materials/material";
import { PBRBaseMaterial } from "core/Materials/PBR/pbrBaseMaterial";
import type { BaseTexture } from "core/Materials/Textures/baseTexture";

import { Tools } from "core/Misc/tools";

const NAME = "KHR_materials_clearcoat";

/**
* @internal
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export class KHR_materials_clearcoat implements IGLTFExporterExtensionV2 {
/** Name of this extension */
public readonly name = NAME;

/** Defines whether this extension is enabled */
public enabled = true;

/** Defines whether this extension is required */
public required = false;

private _exporter: GLTFExporter;

private _wasUsed = false;

constructor(exporter: GLTFExporter) {
this._exporter = exporter;
}

public dispose() {}

/** @internal */
public get wasUsed() {
return this._wasUsed;
}

public postExportMaterialAdditionalTextures?(context: string, node: IMaterial, babylonMaterial: Material): BaseTexture[] {
const additionalTextures: BaseTexture[] = [];
if (babylonMaterial instanceof PBRBaseMaterial) {
if (babylonMaterial.clearCoat.isEnabled) {
if (babylonMaterial.clearCoat.texture) {
additionalTextures.push(babylonMaterial.clearCoat.texture);
}
if (!babylonMaterial.clearCoat.useRoughnessFromMainTexture && babylonMaterial.clearCoat.textureRoughness) {
additionalTextures.push(babylonMaterial.clearCoat.textureRoughness);
}
if (babylonMaterial.clearCoat.bumpTexture) {
additionalTextures.push(babylonMaterial.clearCoat.bumpTexture);
}
return additionalTextures;
}
}

return [];
}

public postExportMaterialAsync?(context: string, node: IMaterial, babylonMaterial: Material): Promise<IMaterial> {
return new Promise((resolve) => {
if (babylonMaterial instanceof PBRBaseMaterial) {
if (!babylonMaterial.clearCoat.isEnabled) {
resolve(node);
return;
}

this._wasUsed = true;

node.extensions = node.extensions || {};

const clearCoatTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.texture);
let clearCoatTextureRoughnessInfo;
if (babylonMaterial.clearCoat.useRoughnessFromMainTexture) {
clearCoatTextureRoughnessInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.texture);
} else {
clearCoatTextureRoughnessInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.textureRoughness);
}

if (babylonMaterial.clearCoat.isTintEnabled) {
Tools.Warn(`Clear Color tint is not supported for glTF export. Ignoring for: ${babylonMaterial.name}`);
}

if (babylonMaterial.clearCoat.remapF0OnInterfaceChange) {
Tools.Warn(`Clear Color F0 remapping is not supported for glTF export. Ignoring for: ${babylonMaterial.name}`);
}

const clearCoatNormalTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.bumpTexture);

const clearCoatInfo: IKHRMaterialsClearcoat = {
clearcoatFactor: babylonMaterial.clearCoat.intensity,
clearcoatTexture: clearCoatTextureInfo ?? undefined,
clearcoatRoughnessFactor: babylonMaterial.clearCoat.roughness,
clearcoatRoughnessTexture: clearCoatTextureRoughnessInfo ?? undefined,
clearcoatNormalTexture: clearCoatNormalTextureInfo ?? undefined,
};

if (clearCoatInfo.clearcoatTexture !== null || clearCoatInfo.clearcoatRoughnessTexture !== null || clearCoatInfo.clearcoatRoughnessTexture !== null) {
this._exporter._materialNeedsUVsSet.add(babylonMaterial);
}

node.extensions[NAME] = clearCoatInfo;
}
resolve(node);
});
}
}

GLTFExporter.RegisterExtension(NAME, (exporter) => new KHR_materials_clearcoat(exporter));
Loading

0 comments on commit c44f379

Please sign in to comment.