Skip to content

Commit 68a6cf5

Browse files
author
Mike Bond
committed
Revert "WIP glTFLoader dynamic material load"
This reverts commit 00d18b1.
1 parent fe839d1 commit 68a6cf5

8 files changed

+127
-265
lines changed

packages/dev/loaders/src/glTF/2.0/Extensions/KHR_materials_anisotropy.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Nullable } from "core/types";
2-
import type { PBRMaterial } from "core/Materials/PBR/pbrMaterial";
3-
import type { OpenPBRMaterial } from "core/Materials/PBR/openPbrMaterial";
2+
import { PBRMaterial } from "core/Materials/PBR/pbrMaterial";
43
import type { Material } from "core/Materials/material";
54

65
import type { IMaterial, ITextureInfo } from "../glTFLoaderInterfaces";
@@ -22,8 +21,6 @@ declare module "../../glTFFileLoader" {
2221
}
2322
}
2423

25-
let PBRMaterialClass: typeof PBRMaterial | typeof OpenPBRMaterial;
26-
2724
/**
2825
* [Specification](https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_anisotropy)
2926
*/
@@ -63,15 +60,8 @@ export class KHR_materials_anisotropy implements IGLTFLoaderExtension {
6360
* @internal
6461
*/
6562
// eslint-disable-next-line no-restricted-syntax
66-
public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material, useOpenPBR: boolean = false): Nullable<Promise<void>> {
63+
public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material): Nullable<Promise<void>> {
6764
return GLTFLoader.LoadExtensionAsync<IKHRMaterialsAnisotropy>(context, material, this.name, async (extensionContext, extension) => {
68-
if (useOpenPBR) {
69-
const mod = await import("core/Materials/PBR/openPbrMaterial");
70-
PBRMaterialClass = mod.OpenPBRMaterial;
71-
} else {
72-
const mod = await import("core/Materials/PBR/pbrMaterial");
73-
PBRMaterialClass = mod.PBRMaterial;
74-
}
7565
const promises = new Array<Promise<any>>();
7666
promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial));
7767
promises.push(this._loadIridescencePropertiesAsync(extensionContext, extension, babylonMaterial));
@@ -80,23 +70,23 @@ export class KHR_materials_anisotropy implements IGLTFLoaderExtension {
8070
}
8171

8272
private async _loadIridescencePropertiesAsync(context: string, properties: IKHRMaterialsAnisotropy, babylonMaterial: Material): Promise<void> {
83-
if (!(babylonMaterial instanceof PBRMaterialClass)) {
73+
if (!(babylonMaterial instanceof PBRMaterial)) {
8474
throw new Error(`${context}: Material type not supported`);
8575
}
8676

8777
const promises = new Array<Promise<any>>();
8878

89-
(babylonMaterial as PBRMaterial).anisotropy.isEnabled = true;
79+
babylonMaterial.anisotropy.isEnabled = true;
9080

91-
(babylonMaterial as PBRMaterial).anisotropy.intensity = properties.anisotropyStrength ?? 0;
92-
(babylonMaterial as PBRMaterial).anisotropy.angle = properties.anisotropyRotation ?? 0;
81+
babylonMaterial.anisotropy.intensity = properties.anisotropyStrength ?? 0;
82+
babylonMaterial.anisotropy.angle = properties.anisotropyRotation ?? 0;
9383

9484
if (properties.anisotropyTexture) {
9585
(properties.anisotropyTexture as ITextureInfo).nonColorData = true;
9686
promises.push(
9787
this._loader.loadTextureInfoAsync(`${context}/anisotropyTexture`, properties.anisotropyTexture, (texture) => {
9888
texture.name = `${babylonMaterial.name} (Anisotropy Intensity)`;
99-
(babylonMaterial as PBRMaterial).anisotropy.texture = texture;
89+
babylonMaterial.anisotropy.texture = texture;
10090
})
10191
);
10292
}

packages/dev/loaders/src/glTF/2.0/Extensions/KHR_materials_clearcoat.ts

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type { Nullable } from "core/types";
2-
import type { PBRMaterial } from "core/Materials/PBR/pbrMaterial";
3-
import type { OpenPBRMaterial } from "core/Materials/PBR/openPbrMaterial";
2+
import { PBRMaterial } from "core/Materials/PBR/pbrMaterial";
43
import type { Material } from "core/Materials/material";
5-
import type { BaseTexture } from "core/Materials/Textures/baseTexture";
64

75
import type { IMaterial, ITextureInfo } from "../glTFLoaderInterfaces";
86
import type { IGLTFLoaderExtension } from "../glTFLoaderExtension";
@@ -23,8 +21,6 @@ declare module "../../glTFFileLoader" {
2321
}
2422
}
2523

26-
let PBRMaterialClass: typeof PBRMaterial | typeof OpenPBRMaterial;
27-
2824
/**
2925
* [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_clearcoat/README.md)
3026
* [Playground Sample](https://www.babylonjs-playground.com/frame.html#7F7PN6#8)
@@ -65,66 +61,54 @@ export class KHR_materials_clearcoat implements IGLTFLoaderExtension {
6561
* @internal
6662
*/
6763
// eslint-disable-next-line no-restricted-syntax
68-
public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material, useOpenPBR: boolean = false): Nullable<Promise<void>> {
64+
public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material): Nullable<Promise<void>> {
6965
return GLTFLoader.LoadExtensionAsync<IKHRMaterialsClearcoat>(context, material, this.name, async (extensionContext, extension) => {
70-
if (useOpenPBR) {
71-
const mod = await import("core/Materials/PBR/openPbrMaterial");
72-
PBRMaterialClass = mod.OpenPBRMaterial;
73-
} else {
74-
const mod = await import("core/Materials/PBR/pbrMaterial");
75-
PBRMaterialClass = mod.PBRMaterial;
76-
}
77-
if (!(babylonMaterial instanceof PBRMaterialClass)) {
78-
throw new Error(`${context}: Material type not supported`);
79-
}
8066
const promises = new Array<Promise<any>>();
8167
promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial));
82-
promises.push(this._loadClearCoatPropertiesAsync(extensionContext, extension, babylonMaterial, useOpenPBR));
68+
promises.push(this._loadClearCoatPropertiesAsync(extensionContext, extension, babylonMaterial));
8369
await Promise.all(promises);
8470
});
8571
}
8672

8773
// eslint-disable-next-line @typescript-eslint/promise-function-async, no-restricted-syntax
88-
private _loadClearCoatPropertiesAsync(context: string, properties: IKHRMaterialsClearcoat, babylonMaterial: Material, useOpenPBR: boolean = false): Promise<void> {
89-
if (!(babylonMaterial instanceof PBRMaterialClass)) {
74+
private _loadClearCoatPropertiesAsync(context: string, properties: IKHRMaterialsClearcoat, babylonMaterial: Material): Promise<void> {
75+
if (!(babylonMaterial instanceof PBRMaterial)) {
9076
throw new Error(`${context}: Material type not supported`);
9177
}
9278

9379
const promises = new Array<Promise<any>>();
94-
let coatRoughness = 0;
95-
let coatWeight = 0;
96-
let coatWeightTexture: Nullable<BaseTexture> = null;
97-
let coatRoughnessTexture: Nullable<BaseTexture> = null;
98-
let coatNormalTexture: Nullable<BaseTexture> = null;
99-
let coatNormalTextureScale = 1;
80+
81+
babylonMaterial.clearCoat.isEnabled = true;
82+
babylonMaterial.clearCoat.useRoughnessFromMainTexture = false;
83+
babylonMaterial.clearCoat.remapF0OnInterfaceChange = false;
10084

10185
if (properties.clearcoatFactor != undefined) {
102-
coatWeight = properties.clearcoatFactor;
86+
babylonMaterial.clearCoat.intensity = properties.clearcoatFactor;
10387
} else {
104-
coatWeight = 0;
88+
babylonMaterial.clearCoat.intensity = 0;
10589
}
10690

10791
if (properties.clearcoatTexture) {
10892
promises.push(
10993
this._loader.loadTextureInfoAsync(`${context}/clearcoatTexture`, properties.clearcoatTexture, (texture) => {
11094
texture.name = `${babylonMaterial.name} (ClearCoat)`;
111-
coatWeightTexture = texture;
95+
babylonMaterial.clearCoat.texture = texture;
11296
})
11397
);
11498
}
11599

116100
if (properties.clearcoatRoughnessFactor != undefined) {
117-
coatRoughness = properties.clearcoatRoughnessFactor;
101+
babylonMaterial.clearCoat.roughness = properties.clearcoatRoughnessFactor;
118102
} else {
119-
coatRoughness = 0;
103+
babylonMaterial.clearCoat.roughness = 0;
120104
}
121105

122106
if (properties.clearcoatRoughnessTexture) {
123107
(properties.clearcoatRoughnessTexture as ITextureInfo).nonColorData = true;
124108
promises.push(
125109
this._loader.loadTextureInfoAsync(`${context}/clearcoatRoughnessTexture`, properties.clearcoatRoughnessTexture, (texture) => {
126110
texture.name = `${babylonMaterial.name} (ClearCoat Roughness)`;
127-
coatRoughnessTexture = texture;
111+
babylonMaterial.clearCoat.textureRoughness = texture;
128112
})
129113
);
130114
}
@@ -134,37 +118,19 @@ export class KHR_materials_clearcoat implements IGLTFLoaderExtension {
134118
promises.push(
135119
this._loader.loadTextureInfoAsync(`${context}/clearcoatNormalTexture`, properties.clearcoatNormalTexture, (texture) => {
136120
texture.name = `${babylonMaterial.name} (ClearCoat Normal)`;
137-
coatNormalTexture = texture;
121+
babylonMaterial.clearCoat.bumpTexture = texture;
138122
})
139123
);
140124

141125
babylonMaterial.invertNormalMapX = !babylonMaterial.getScene().useRightHandedSystem;
142126
babylonMaterial.invertNormalMapY = babylonMaterial.getScene().useRightHandedSystem;
143127
if (properties.clearcoatNormalTexture.scale != undefined) {
144-
coatNormalTextureScale = properties.clearcoatNormalTexture.scale;
128+
babylonMaterial.clearCoat.bumpTexture!.level = properties.clearcoatNormalTexture.scale;
145129
}
146130
}
147131

148132
// eslint-disable-next-line github/no-then
149-
return Promise.all(promises).then(() => {
150-
if (useOpenPBR) {
151-
// eslint-disable-next-line github/no-then
152-
return;
153-
}
154-
const material = babylonMaterial as PBRMaterial;
155-
material.clearCoat.isEnabled = true;
156-
material.clearCoat.useRoughnessFromMainTexture = false;
157-
material.clearCoat.remapF0OnInterfaceChange = false;
158-
material.clearCoat.intensity = coatWeight;
159-
material.clearCoat.texture = coatWeightTexture;
160-
material.clearCoat.roughness = coatRoughness;
161-
material.clearCoat.textureRoughness = coatRoughnessTexture;
162-
163-
material.clearCoat.bumpTexture = coatNormalTexture;
164-
if (coatNormalTexture) {
165-
material.clearCoat.bumpTexture!.level = coatNormalTextureScale;
166-
}
167-
});
133+
return Promise.all(promises).then(() => {});
168134
}
169135
}
170136

packages/dev/loaders/src/glTF/2.0/Extensions/KHR_materials_diffuse_transmission.ts

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable github/no-then */
22
import type { Nullable } from "core/types";
3-
import type { PBRMaterial } from "core/Materials/PBR/pbrMaterial";
4-
import type { OpenPBRMaterial } from "core/Materials/PBR/openPbrMaterial";
3+
import { PBRMaterial } from "core/Materials/PBR/pbrMaterial";
54
import type { Material } from "core/Materials/material";
65
import type { BaseTexture } from "core/Materials/Textures/baseTexture";
76
import type { IMaterial, ITextureInfo } from "../glTFLoaderInterfaces";
@@ -24,8 +23,6 @@ declare module "../../glTFFileLoader" {
2423
}
2524
}
2625

27-
let PBRMaterialClass: typeof PBRMaterial | typeof OpenPBRMaterial;
28-
2926
/**
3027
* [Proposed Specification](https://github.com/KhronosGroup/glTF/pull/1825)
3128
* !!! Experimental Extension Subject to Changes !!!
@@ -69,15 +66,8 @@ export class KHR_materials_diffuse_transmission implements IGLTFLoaderExtension
6966
* @internal
7067
*/
7168
// eslint-disable-next-line no-restricted-syntax
72-
public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material, useOpenPBR: boolean = false): Nullable<Promise<void>> {
69+
public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material): Nullable<Promise<void>> {
7370
return GLTFLoader.LoadExtensionAsync<IKHRMaterialsDiffuseTransmission>(context, material, this.name, async (extensionContext, extension) => {
74-
if (useOpenPBR) {
75-
const mod = await import("core/Materials/PBR/openPbrMaterial");
76-
PBRMaterialClass = mod.OpenPBRMaterial;
77-
} else {
78-
const mod = await import("core/Materials/PBR/pbrMaterial");
79-
PBRMaterialClass = mod.PBRMaterial;
80-
}
8171
const promises = new Array<Promise<any>>();
8272
promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial));
8373
promises.push(this._loadTranslucentPropertiesAsync(extensionContext, material, babylonMaterial, extension));
@@ -87,71 +77,62 @@ export class KHR_materials_diffuse_transmission implements IGLTFLoaderExtension
8777

8878
// eslint-disable-next-line no-restricted-syntax, @typescript-eslint/promise-function-async
8979
private _loadTranslucentPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material, extension: IKHRMaterialsDiffuseTransmission): Promise<void> {
90-
if (!(babylonMaterial instanceof PBRMaterialClass)) {
80+
if (!(babylonMaterial instanceof PBRMaterial)) {
9181
throw new Error(`${context}: Material type not supported`);
9282
}
9383

94-
let translucencyWeight = 0.0;
95-
let translucencyWeightTexture: Nullable<BaseTexture>;
96-
let translucencyColor = Color3.White();
97-
let translucencyColorTexture: Nullable<BaseTexture>;
84+
const pbrMaterial = babylonMaterial;
85+
86+
// Enables "translucency" texture which represents diffusely-transmitted light.
87+
pbrMaterial.subSurface.isTranslucencyEnabled = true;
88+
89+
// Since this extension models thin-surface transmission only, we must make the
90+
// internal IOR == 1.0 and set the thickness to 0.
91+
pbrMaterial.subSurface.volumeIndexOfRefraction = 1.0;
92+
pbrMaterial.subSurface.minimumThickness = 0.0;
93+
pbrMaterial.subSurface.maximumThickness = 0.0;
94+
95+
// Tint color will be used for transmission.
96+
pbrMaterial.subSurface.useAlbedoToTintTranslucency = false;
9897

9998
if (extension.diffuseTransmissionFactor !== undefined) {
100-
translucencyWeight = extension.diffuseTransmissionFactor;
99+
pbrMaterial.subSurface.translucencyIntensity = extension.diffuseTransmissionFactor;
100+
} else {
101+
pbrMaterial.subSurface.translucencyIntensity = 0.0;
102+
pbrMaterial.subSurface.isTranslucencyEnabled = false;
103+
return Promise.resolve();
101104
}
102105

103106
const promises = new Array<Promise<any>>();
104107

108+
pbrMaterial.subSurface.useGltfStyleTextures = true;
109+
105110
if (extension.diffuseTransmissionTexture) {
106111
(extension.diffuseTransmissionTexture as ITextureInfo).nonColorData = true;
107112
promises.push(
108113
this._loader.loadTextureInfoAsync(`${context}/diffuseTransmissionTexture`, extension.diffuseTransmissionTexture).then((texture: BaseTexture) => {
109114
texture.name = `${babylonMaterial.name} (Diffuse Transmission)`;
110-
translucencyWeightTexture = texture;
115+
pbrMaterial.subSurface.translucencyIntensityTexture = texture;
111116
})
112117
);
113118
}
114119

115120
if (extension.diffuseTransmissionColorFactor !== undefined) {
116-
translucencyColor = Color3.FromArray(extension.diffuseTransmissionColorFactor);
121+
pbrMaterial.subSurface.translucencyColor = Color3.FromArray(extension.diffuseTransmissionColorFactor);
122+
} else {
123+
pbrMaterial.subSurface.translucencyColor = Color3.White();
117124
}
118125

119126
if (extension.diffuseTransmissionColorTexture) {
120127
promises.push(
121128
this._loader.loadTextureInfoAsync(`${context}/diffuseTransmissionColorTexture`, extension.diffuseTransmissionColorTexture).then((texture: BaseTexture) => {
122129
texture.name = `${babylonMaterial.name} (Diffuse Transmission Color)`;
123-
translucencyColorTexture = texture;
130+
pbrMaterial.subSurface.translucencyColorTexture = texture;
124131
})
125132
);
126133
}
127134

128-
return Promise.all(promises).then(() => {
129-
const pbrMaterial = babylonMaterial as PBRMaterial;
130-
131-
// Enables "translucency" texture which represents diffusely-transmitted light.
132-
pbrMaterial.subSurface.isTranslucencyEnabled = true;
133-
134-
// Since this extension models thin-surface transmission only, we must make the
135-
// internal IOR == 1.0 and set the thickness to 0.
136-
pbrMaterial.subSurface.volumeIndexOfRefraction = 1.0;
137-
pbrMaterial.subSurface.minimumThickness = 0.0;
138-
pbrMaterial.subSurface.maximumThickness = 0.0;
139-
140-
// Tint color will be used for transmission.
141-
pbrMaterial.subSurface.useAlbedoToTintTranslucency = false;
142-
143-
pbrMaterial.subSurface.translucencyIntensity = translucencyWeight;
144-
if (translucencyWeight === 0.0) {
145-
pbrMaterial.subSurface.isTranslucencyEnabled = false;
146-
return;
147-
}
148-
149-
pbrMaterial.subSurface.useGltfStyleTextures = true;
150-
pbrMaterial.subSurface.translucencyIntensityTexture = translucencyWeightTexture;
151-
152-
pbrMaterial.subSurface.translucencyColor = translucencyColor;
153-
pbrMaterial.subSurface.translucencyColorTexture = translucencyColorTexture;
154-
});
135+
return Promise.all(promises).then(() => {});
155136
}
156137
}
157138

0 commit comments

Comments
 (0)