@babylonjs/loaders 9.8.0 → 9.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -106,13 +106,19 @@ export function LoadBoundingInfoFromPositionAccessor(accessor) {
106
106
  */
107
107
  export class GLTFLoader {
108
108
  /**
109
- * Test if the given material is of the same type as the one used by the loader
109
+ * Test if the given material is an instance of any PBR material type known to this loader.
110
110
  * @param material The material to test
111
- * @returns true if the material is of the same type, false otherwise
111
+ * @returns true if the material matches one of the loaded PBR implementations
112
112
  */
113
113
  isMatchingMaterialType(material) {
114
- if (material && this._pbrMaterialImpl) {
115
- return material instanceof this._pbrMaterialImpl.materialClass;
114
+ if (!material) {
115
+ return false;
116
+ }
117
+ const materialImpls = Array.from(this._pbrMaterialImpls.values());
118
+ for (const impl of materialImpls) {
119
+ if (material instanceof impl.materialClass) {
120
+ return true;
121
+ }
116
122
  }
117
123
  return false;
118
124
  }
@@ -204,8 +210,13 @@ export class GLTFLoader {
204
210
  this._postSceneLoadActions = new Array();
205
211
  this._materialAdapterCache = new WeakMap();
206
212
  this._materialAdapters = new Set();
207
- /** @internal */
208
- this._pbrMaterialImpl = null;
213
+ /**
214
+ * Loaded PBR material implementations, keyed by their identifier (e.g. "pbr", "openpbr").
215
+ * Only populated after the load has started and only for the types actually needed by the asset.
216
+ * Empty when PBR materials are disabled (skipMaterials).
217
+ * @internal
218
+ */
219
+ this._pbrMaterialImpls = new Map();
209
220
  this._parent = parent;
210
221
  }
211
222
  /**
@@ -217,10 +228,14 @@ export class GLTFLoader {
217
228
  _getOrCreateMaterialAdapter(material) {
218
229
  let adapter = this._materialAdapterCache.get(material);
219
230
  if (!adapter) {
220
- if (this._pbrMaterialImpl) {
221
- adapter = new this._pbrMaterialImpl.adapterClass(material);
231
+ const materialImpls = Array.from(this._pbrMaterialImpls.values());
232
+ for (const impl of materialImpls) {
233
+ if (material instanceof impl.materialClass) {
234
+ adapter = new impl.adapterClass(material);
235
+ break;
236
+ }
222
237
  }
223
- else {
238
+ if (!adapter) {
224
239
  throw new Error(`Appropriate material adapter class not found`);
225
240
  }
226
241
  const createdAdapter = adapter;
@@ -304,20 +319,35 @@ export class GLTFLoader {
304
319
  this._fileName = fileName;
305
320
  this._allMaterialsDirtyRequired = false;
306
321
  await this._loadExtensionsAsync();
307
- // NOTE: Explicitly check _pbrMaterialImpl for null as a value of false means don't use PBR materials at all.
308
- if (!this.parent.skipMaterials && this._pbrMaterialImpl == null) {
309
- if (this.parent.useOpenPBR || this.isExtensionUsed("KHR_materials_openpbr")) {
310
- this._pbrMaterialImpl = {
311
- materialClass: (await import("@babylonjs/core/Materials/PBR/openpbrMaterial.js")).OpenPBRMaterial,
312
- adapterClass: (await import("./openpbrMaterialLoadingAdapter.js")).OpenPBRMaterialLoadingAdapter,
313
- };
322
+ if (!this.parent.skipMaterials) {
323
+ const needsOpenPBR = this.parent.useOpenPBR || this.isExtensionUsed("KHR_materials_openpbr");
324
+ let needsPBR = false;
325
+ if (!this.parent.useOpenPBR) {
326
+ // PBR is needed when useOpenPBR is turned off.
327
+ needsPBR = true;
314
328
  }
315
- else {
316
- this._pbrMaterialImpl = {
317
- materialClass: (await import("@babylonjs/core/Materials/PBR/pbrMaterial.js")).PBRMaterial,
318
- adapterClass: (await import("./pbrMaterialLoadingAdapter.js")).PBRMaterialLoadingAdapter,
319
- };
329
+ else if (this._gltf.materials?.length && this._gltf.materials.some((m) => !m.extensions?.["KHR_materials_openpbr"])) {
330
+ // PBR is needed if there is at least one material that does not use the KHR_materials_openpbr extension (i.e. relies on the default PBR implementation).
331
+ needsPBR = true;
320
332
  }
333
+ const implPromises = [];
334
+ if (needsOpenPBR && !this._pbrMaterialImpls.has("openpbr")) {
335
+ implPromises.push(Promise.all([import("@babylonjs/core/Materials/PBR/openpbrMaterial.js"), import("./openpbrMaterialLoadingAdapter.js")]).then(([{ OpenPBRMaterial: openPBRMaterialClass }, { OpenPBRMaterialLoadingAdapter: openPBRAdapterClass }]) => {
336
+ this._pbrMaterialImpls.set("openpbr", {
337
+ materialClass: openPBRMaterialClass,
338
+ adapterClass: openPBRAdapterClass,
339
+ });
340
+ }));
341
+ }
342
+ if (needsPBR && !this._pbrMaterialImpls.has("pbr")) {
343
+ implPromises.push(Promise.all([import("@babylonjs/core/Materials/PBR/pbrMaterial.js"), import("./pbrMaterialLoadingAdapter.js")]).then(([{ PBRMaterial: pbrMaterialClass }, { PBRMaterialLoadingAdapter: pbrAdapterClass }]) => {
344
+ this._pbrMaterialImpls.set("pbr", {
345
+ materialClass: pbrMaterialClass,
346
+ adapterClass: pbrAdapterClass,
347
+ });
348
+ }));
349
+ }
350
+ await Promise.all(implPromises);
321
351
  }
322
352
  const loadingToReadyCounterName = `${GLTFLoaderState[GLTFLoaderState.LOADING]} => ${GLTFLoaderState[GLTFLoaderState.READY]}`;
323
353
  const loadingToCompleteCounterName = `${GLTFLoaderState[GLTFLoaderState.LOADING]} => ${GLTFLoaderState[GLTFLoaderState.COMPLETE]}`;
@@ -867,7 +897,7 @@ export class GLTFLoader {
867
897
  if (primitive.material == undefined) {
868
898
  let babylonMaterial = this._defaultBabylonMaterialData[babylonDrawMode];
869
899
  if (!babylonMaterial) {
870
- babylonMaterial = this._createDefaultMaterial("__GLTFLoader._default", babylonDrawMode);
900
+ babylonMaterial = this._createDefaultMaterial("__GLTFLoader._default", babylonDrawMode, this._getDefaultImpl());
871
901
  this._parent.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
872
902
  this._defaultBabylonMaterialData[babylonDrawMode] = babylonMaterial;
873
903
  }
@@ -1803,16 +1833,52 @@ export class GLTFLoader {
1803
1833
  return babylonData.babylonMaterial;
1804
1834
  });
1805
1835
  }
1806
- _createDefaultMaterial(name, babylonDrawMode) {
1807
- if (!this._pbrMaterialImpl) {
1808
- throw new Error("PBR Material class not loaded");
1836
+ /**
1837
+ * Selects the appropriate PBR material implementation for a given glTF material.
1838
+ * Uses OpenPBR when the material carries a "KHR_materials_openpbr" extension or when
1839
+ * the loader-level `useOpenPBR` flag is set; falls back to standard PBR otherwise.
1840
+ * @param material The glTF material
1841
+ * @returns The matching loaded implementation
1842
+ */
1843
+ _selectImplForGltfMaterial(material) {
1844
+ if (this.parent.useOpenPBR || material.extensions?.["KHR_materials_openpbr"]) {
1845
+ const impl = this._pbrMaterialImpls.get("openpbr");
1846
+ if (impl) {
1847
+ return impl;
1848
+ }
1849
+ }
1850
+ const impl = this._pbrMaterialImpls.get("pbr");
1851
+ if (impl) {
1852
+ return impl;
1809
1853
  }
1854
+ throw new Error("No PBR material implementation loaded");
1855
+ }
1856
+ /**
1857
+ * Returns the default PBR material implementation used when there is no per-material
1858
+ * selection context (e.g. when creating the built-in default material for primitives
1859
+ * that have no glTF material assigned). Prefers OpenPBR when `useOpenPBR` is set.
1860
+ * @returns The default loaded implementation
1861
+ */
1862
+ _getDefaultImpl() {
1863
+ if (this.parent.useOpenPBR) {
1864
+ const impl = this._pbrMaterialImpls.get("openpbr");
1865
+ if (impl) {
1866
+ return impl;
1867
+ }
1868
+ }
1869
+ const impl = this._pbrMaterialImpls.get("pbr") ?? this._pbrMaterialImpls.values().next().value;
1870
+ if (impl) {
1871
+ return impl;
1872
+ }
1873
+ throw new Error("No PBR material implementation loaded");
1874
+ }
1875
+ _createDefaultMaterial(name, babylonDrawMode, impl) {
1810
1876
  this._babylonScene._blockEntityCollection = !!this._assetContainer;
1811
- const babylonMaterial = new this._pbrMaterialImpl.materialClass(name, this._babylonScene);
1877
+ const babylonMaterial = new impl.materialClass(name, this._babylonScene);
1812
1878
  babylonMaterial._parentContainer = this._assetContainer;
1813
1879
  this._babylonScene._blockEntityCollection = false;
1814
1880
  babylonMaterial.fillMode = babylonDrawMode;
1815
- babylonMaterial.transparencyMode = this._pbrMaterialImpl.materialClass.MATERIAL_OPAQUE;
1881
+ babylonMaterial.transparencyMode = impl.materialClass.MATERIAL_OPAQUE;
1816
1882
  // Create the material adapter and set some default properties.
1817
1883
  // We don't need to wait for the promise to resolve here.
1818
1884
  const adapter = this._getOrCreateMaterialAdapter(babylonMaterial);
@@ -1835,7 +1901,7 @@ export class GLTFLoader {
1835
1901
  return extensionMaterial;
1836
1902
  }
1837
1903
  const name = material.name || `material${material.index}`;
1838
- const babylonMaterial = this._createDefaultMaterial(name, babylonDrawMode);
1904
+ const babylonMaterial = this._createDefaultMaterial(name, babylonDrawMode, this._selectImplForGltfMaterial(material));
1839
1905
  return babylonMaterial;
1840
1906
  }
1841
1907
  /**
@@ -1925,7 +1991,7 @@ export class GLTFLoader {
1925
1991
  * @param babylonMaterial The Babylon material
1926
1992
  */
1927
1993
  loadMaterialAlphaProperties(context, material, babylonMaterial) {
1928
- if (!this._pbrMaterialImpl) {
1994
+ if (this._pbrMaterialImpls.size === 0) {
1929
1995
  throw new Error(`${context}: Material type not supported`);
1930
1996
  }
1931
1997
  const adapter = this._getOrCreateMaterialAdapter(babylonMaterial);
@@ -1933,12 +1999,12 @@ export class GLTFLoader {
1933
1999
  const alphaMode = material.alphaMode || "OPAQUE" /* MaterialAlphaMode.OPAQUE */;
1934
2000
  switch (alphaMode) {
1935
2001
  case "OPAQUE" /* MaterialAlphaMode.OPAQUE */: {
1936
- babylonMaterial.transparencyMode = this._pbrMaterialImpl.materialClass.MATERIAL_OPAQUE;
2002
+ babylonMaterial.transparencyMode = Material.MATERIAL_OPAQUE;
1937
2003
  babylonMaterial.alpha = 1.0; // Force alpha to 1.0 for opaque mode.
1938
2004
  break;
1939
2005
  }
1940
2006
  case "MASK" /* MaterialAlphaMode.MASK */: {
1941
- babylonMaterial.transparencyMode = this._pbrMaterialImpl.materialClass.MATERIAL_ALPHATEST;
2007
+ babylonMaterial.transparencyMode = Material.MATERIAL_ALPHATEST;
1942
2008
  adapter.alphaCutOff = material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff;
1943
2009
  if (baseColorTexture) {
1944
2010
  baseColorTexture.hasAlpha = true;
@@ -1946,7 +2012,7 @@ export class GLTFLoader {
1946
2012
  break;
1947
2013
  }
1948
2014
  case "BLEND" /* MaterialAlphaMode.BLEND */: {
1949
- babylonMaterial.transparencyMode = this._pbrMaterialImpl.materialClass.MATERIAL_ALPHABLEND;
2015
+ babylonMaterial.transparencyMode = Material.MATERIAL_ALPHABLEND;
1950
2016
  if (baseColorTexture) {
1951
2017
  baseColorTexture.hasAlpha = true;
1952
2018
  adapter.useAlphaFromBaseColorTexture = true;