@onerjs/serializers 8.23.12 → 8.25.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.
Files changed (38) hide show
  1. package/glTF/2.0/Extensions/EXT_texture_avif.d.ts +17 -0
  2. package/glTF/2.0/Extensions/EXT_texture_avif.js +41 -0
  3. package/glTF/2.0/Extensions/EXT_texture_avif.js.map +1 -0
  4. package/glTF/2.0/Extensions/EXT_texture_webp.d.ts +16 -0
  5. package/glTF/2.0/Extensions/EXT_texture_webp.js +40 -0
  6. package/glTF/2.0/Extensions/EXT_texture_webp.js.map +1 -0
  7. package/glTF/2.0/Extensions/KHR_materials_transmission.js +1 -1
  8. package/glTF/2.0/Extensions/KHR_materials_transmission.js.map +1 -1
  9. package/glTF/2.0/Extensions/KHR_texture_basisu.d.ts +16 -0
  10. package/glTF/2.0/Extensions/KHR_texture_basisu.js +40 -0
  11. package/glTF/2.0/Extensions/KHR_texture_basisu.js.map +1 -0
  12. package/glTF/2.0/Extensions/index.d.ts +3 -0
  13. package/glTF/2.0/Extensions/index.js +3 -0
  14. package/glTF/2.0/Extensions/index.js.map +1 -1
  15. package/glTF/2.0/glTFExporter.d.ts +1 -4
  16. package/glTF/2.0/glTFExporter.js +2 -7
  17. package/glTF/2.0/glTFExporter.js.map +1 -1
  18. package/glTF/2.0/glTFExporterExtension.d.ts +1 -10
  19. package/glTF/2.0/glTFExporterExtension.js.map +1 -1
  20. package/glTF/2.0/glTFMaterialExporter.d.ts +4 -7
  21. package/glTF/2.0/glTFMaterialExporter.js +113 -71
  22. package/glTF/2.0/glTFMaterialExporter.js.map +1 -1
  23. package/legacy/legacy-glTF2Serializer.d.ts +2 -2
  24. package/legacy/legacy-glTF2Serializer.js +7 -7
  25. package/legacy/legacy-glTF2Serializer.js.map +1 -1
  26. package/legacy/legacy-objSerializer.d.ts +1 -1
  27. package/legacy/legacy-objSerializer.js +2 -2
  28. package/legacy/legacy-objSerializer.js.map +1 -1
  29. package/legacy/legacy-stlSerializer.d.ts +1 -1
  30. package/legacy/legacy-stlSerializer.js +2 -2
  31. package/legacy/legacy-stlSerializer.js.map +1 -1
  32. package/legacy/legacy-usdzSerializer.d.ts +1 -1
  33. package/legacy/legacy-usdzSerializer.js +2 -2
  34. package/legacy/legacy-usdzSerializer.js.map +1 -1
  35. package/legacy/legacy.d.ts +1 -1
  36. package/legacy/legacy.js +1 -1
  37. package/legacy/legacy.js.map +1 -1
  38. package/package.json +3 -3
@@ -1 +1 @@
1
- {"version":3,"file":"glTFExporterExtension.js","sourceRoot":"","sources":["../../../../../dev/serializers/src/glTF/2.0/glTFExporterExtension.ts"],"names":[],"mappings":"AAYA,gBAAgB;AAChB,wEAAwE;AACxE,MAAM,CAAC,IAAI,0BAA0B,GAAG,CAAC,CAAC,CAAC,uCAAuC","sourcesContent":["import type { ImageMimeType, IMeshPrimitive, INode, IMaterial, ITextureInfo, IAccessor } from \"babylonjs-gltf2interface\";\r\nimport type { Node } from \"core/node\";\r\nimport type { Nullable } from \"core/types\";\r\n\r\nimport type { Texture } from \"core/Materials/Textures/texture\";\r\nimport type { IDisposable } from \"core/scene\";\r\n\r\nimport type { IGLTFExporterExtension } from \"../glTFFileExporter\";\r\nimport type { Material } from \"core/Materials/material\";\r\nimport type { BaseTexture } from \"core/Materials/Textures/baseTexture\";\r\nimport type { BufferManager } from \"./bufferManager\";\r\n\r\n/** @internal */\r\n// eslint-disable-next-line no-var, @typescript-eslint/naming-convention\r\nexport var __IGLTFExporterExtensionV2 = 0; // I am here to allow dts to be created\r\n\r\n/**\r\n * Interface for a glTF exporter extension\r\n * @internal\r\n */\r\nexport interface IGLTFExporterExtensionV2 extends IGLTFExporterExtension, IDisposable {\r\n /**\r\n * Define this method to modify the default behavior before exporting a texture\r\n * @param context The context when loading the asset\r\n * @param babylonTexture The Babylon.js texture\r\n * @param mimeType The mime-type of the generated image\r\n * @returns A promise that resolves with the exported texture\r\n */\r\n preExportTextureAsync?(context: string, babylonTexture: Texture, mimeType: ImageMimeType): Promise<Nullable<Texture>>;\r\n\r\n /**\r\n * Define this method to get notified when a texture info is created\r\n * @param context The context when loading the asset\r\n * @param textureInfo The glTF texture info\r\n * @param babylonTexture The Babylon.js texture\r\n */\r\n postExportTexture?(context: string, textureInfo: ITextureInfo, babylonTexture: BaseTexture): void;\r\n\r\n /**\r\n * Define this method to get notified when a primitive is created\r\n * @param primitive glTF mesh primitive\r\n * @param bufferManager Buffer manager\r\n * @param accessors List of glTF accessors\r\n */\r\n postExportMeshPrimitive?(primitive: IMeshPrimitive, bufferManager: BufferManager, accessors: IAccessor[]): void;\r\n\r\n /**\r\n * Define this method to modify the default behavior when exporting a node\r\n * @param context The context when exporting the node\r\n * @param node glTF node\r\n * @param babylonNode BabylonJS node\r\n * @param nodeMap Current node mapping of babylon node to glTF node index. Useful for combining nodes together.\r\n * @param convertToRightHanded Flag indicating whether to convert values to right-handed\r\n * @param bufferManager Buffer manager\r\n * @returns nullable INode promise\r\n */\r\n postExportNodeAsync?(\r\n context: string,\r\n node: INode,\r\n babylonNode: Node,\r\n nodeMap: Map<Node, number>,\r\n convertToRightHanded: boolean,\r\n bufferManager: BufferManager\r\n ): Promise<Nullable<INode>>;\r\n\r\n /**\r\n * Define this method to modify the default behavior when exporting a material\r\n * @param material glTF material\r\n * @param babylonMaterial BabylonJS material\r\n * @returns nullable IMaterial promise\r\n */\r\n postExportMaterialAsync?(context: string, node: IMaterial, babylonMaterial: Material): Promise<IMaterial>;\r\n\r\n /**\r\n * Define this method to return additional textures to export from a material\r\n * @param material glTF material\r\n * @param babylonMaterial BabylonJS material\r\n * @returns List of textures\r\n */\r\n postExportMaterialAdditionalTextures?(context: string, node: IMaterial, babylonMaterial: Material): BaseTexture[];\r\n\r\n /**\r\n * Define this method to modify the glTF buffer data before it is finalized and written\r\n * @param bufferManager Buffer manager\r\n */\r\n preGenerateBinaryAsync?(bufferManager: BufferManager): Promise<void>;\r\n\r\n /** Gets a boolean indicating that this extension was used */\r\n wasUsed: boolean;\r\n\r\n /** Gets a boolean indicating that this extension is required for the file to work */\r\n required: boolean;\r\n\r\n /**\r\n * Called after the exporter state changes to EXPORTING\r\n */\r\n onExporting?(): void;\r\n}\r\n"]}
1
+ {"version":3,"file":"glTFExporterExtension.js","sourceRoot":"","sources":["../../../../../dev/serializers/src/glTF/2.0/glTFExporterExtension.ts"],"names":[],"mappings":"AAWA,gBAAgB;AAChB,wEAAwE;AACxE,MAAM,CAAC,IAAI,0BAA0B,GAAG,CAAC,CAAC,CAAC,uCAAuC","sourcesContent":["import type { IMeshPrimitive, INode, IMaterial, ITextureInfo, IAccessor } from \"babylonjs-gltf2interface\";\r\nimport type { Node } from \"core/node\";\r\nimport type { Nullable } from \"core/types\";\r\n\r\nimport type { IDisposable } from \"core/scene\";\r\n\r\nimport type { IGLTFExporterExtension } from \"../glTFFileExporter\";\r\nimport type { Material } from \"core/Materials/material\";\r\nimport type { BaseTexture } from \"core/Materials/Textures/baseTexture\";\r\nimport type { BufferManager } from \"./bufferManager\";\r\n\r\n/** @internal */\r\n// eslint-disable-next-line no-var, @typescript-eslint/naming-convention\r\nexport var __IGLTFExporterExtensionV2 = 0; // I am here to allow dts to be created\r\n\r\n/**\r\n * Interface for a glTF exporter extension\r\n * @internal\r\n */\r\nexport interface IGLTFExporterExtensionV2 extends IGLTFExporterExtension, IDisposable {\r\n /**\r\n * Define this method to get notified when a texture info is created\r\n * @param context The context when loading the asset\r\n * @param textureInfo The glTF texture info\r\n * @param babylonTexture The Babylon.js texture\r\n */\r\n postExportTexture?(context: string, textureInfo: ITextureInfo, babylonTexture: BaseTexture): void;\r\n\r\n /**\r\n * Define this method to get notified when a primitive is created\r\n * @param primitive glTF mesh primitive\r\n * @param bufferManager Buffer manager\r\n * @param accessors List of glTF accessors\r\n */\r\n postExportMeshPrimitive?(primitive: IMeshPrimitive, bufferManager: BufferManager, accessors: IAccessor[]): void;\r\n\r\n /**\r\n * Define this method to modify the default behavior when exporting a node\r\n * @param context The context when exporting the node\r\n * @param node glTF node\r\n * @param babylonNode BabylonJS node\r\n * @param nodeMap Current node mapping of babylon node to glTF node index. Useful for combining nodes together.\r\n * @param convertToRightHanded Flag indicating whether to convert values to right-handed\r\n * @param bufferManager Buffer manager\r\n * @returns nullable INode promise\r\n */\r\n postExportNodeAsync?(\r\n context: string,\r\n node: INode,\r\n babylonNode: Node,\r\n nodeMap: Map<Node, number>,\r\n convertToRightHanded: boolean,\r\n bufferManager: BufferManager\r\n ): Promise<Nullable<INode>>;\r\n\r\n /**\r\n * Define this method to modify the default behavior when exporting a material\r\n * @param material glTF material\r\n * @param babylonMaterial BabylonJS material\r\n * @returns nullable IMaterial promise\r\n */\r\n postExportMaterialAsync?(context: string, node: IMaterial, babylonMaterial: Material): Promise<IMaterial>;\r\n\r\n /**\r\n * Define this method to return additional textures to export from a material\r\n * @param material glTF material\r\n * @param babylonMaterial BabylonJS material\r\n * @returns List of textures\r\n */\r\n postExportMaterialAdditionalTextures?(context: string, node: IMaterial, babylonMaterial: Material): BaseTexture[];\r\n\r\n /**\r\n * Define this method to modify the glTF buffer data before it is finalized and written\r\n * @param bufferManager Buffer manager\r\n */\r\n preGenerateBinaryAsync?(bufferManager: BufferManager): Promise<void>;\r\n\r\n /** Gets a boolean indicating that this extension was used */\r\n wasUsed: boolean;\r\n\r\n /** Gets a boolean indicating that this extension is required for the file to work */\r\n required: boolean;\r\n\r\n /**\r\n * Called after the exporter state changes to EXPORTING\r\n */\r\n onExporting?(): void;\r\n}\r\n"]}
@@ -1,5 +1,4 @@
1
1
  import type { ITextureInfo, IMaterialPbrMetallicRoughness } from "babylonjs-gltf2interface";
2
- import { ImageMimeType } from "babylonjs-gltf2interface";
3
2
  import type { Nullable } from "@onerjs/core/types.js";
4
3
  import type { BaseTexture } from "@onerjs/core/Materials/Textures/baseTexture.js";
5
4
  import type { GLTFExporter } from "./glTFExporter.js";
@@ -29,7 +28,7 @@ export declare class GLTFMaterialExporter {
29
28
  private _internalTextureToImage;
30
29
  constructor(_exporter: GLTFExporter);
31
30
  getTextureInfo(babylonTexture: Nullable<BaseTexture>): Nullable<ITextureInfo>;
32
- exportStandardMaterialAsync(babylonStandardMaterial: StandardMaterial, mimeType: ImageMimeType, hasUVs: boolean): Promise<number>;
31
+ exportStandardMaterialAsync(babylonStandardMaterial: StandardMaterial, hasUVs: boolean): Promise<number>;
33
32
  private _finishMaterialAsync;
34
33
  private _getImageDataAsync;
35
34
  /**
@@ -72,7 +71,6 @@ export declare class GLTFMaterialExporter {
72
71
  /**
73
72
  * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors
74
73
  * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
75
- * @param mimeType mime type to use for the textures
76
74
  * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
77
75
  * @param hasUVs specifies if texture coordinates are present on the submesh to determine if textures should be applied
78
76
  * @returns glTF PBR Metallic Roughness factors
@@ -83,16 +81,15 @@ export declare class GLTFMaterialExporter {
83
81
  /**
84
82
  * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors
85
83
  * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
86
- * @param mimeType mime type to use for the textures
87
84
  * @param pbrMetallicRoughness glTF PBR Metallic Roughness interface
88
85
  * @param hasUVs specifies if texture coordinates are present on the submesh to determine if textures should be applied
89
86
  * @returns glTF PBR Metallic Roughness factors
90
87
  */
91
88
  private _convertSpecGlossFactorsToMetallicRoughnessAsync;
92
- exportPBRMaterialAsync(babylonPBRMaterial: PBRBaseMaterial, mimeType: ImageMimeType, hasUVs: boolean): Promise<number>;
89
+ exportPBRMaterialAsync(babylonPBRMaterial: PBRBaseMaterial, hasUVs: boolean): Promise<number>;
93
90
  private _setMetallicRoughnessPbrMaterialAsync;
94
- exportTextureAsync(babylonTexture: BaseTexture, mimeType: ImageMimeType): Promise<Nullable<ITextureInfo>>;
95
- private _exportTextureInfoAsync;
91
+ exportTextureAsync(babylonTexture: BaseTexture): Promise<Nullable<ITextureInfo>>;
92
+ private _exportTextureImageAsync;
96
93
  private _exportImage;
97
94
  private _exportTextureInfo;
98
95
  private _exportTextureSampler;
@@ -10,6 +10,7 @@ import { RawTexture } from "@onerjs/core/Materials/Textures/rawTexture.js";
10
10
  import { Constants } from "@onerjs/core/Engines/constants.js";
11
11
  import { DumpTools } from "@onerjs/core/Misc/dumpTools.js";
12
12
  import { SpecularPowerToRoughness } from "@onerjs/core/Helpers/materialConversionHelper.js";
13
+ import { GetMimeType } from "@onerjs/core/Misc/fileTools.js";
13
14
  const Epsilon = 1e-6;
14
15
  const DielectricSpecular = new Color3(0.04, 0.04, 0.04);
15
16
  const MaxSpecularPower = 1024;
@@ -25,8 +26,53 @@ function GetFileExtensionFromMimeType(mimeType) {
25
26
  return ".webp";
26
27
  case "image/avif" /* ImageMimeType.AVIF */:
27
28
  return ".avif";
29
+ case "image/ktx2" /* ImageMimeType.KTX2 */:
30
+ return ".ktx2";
28
31
  }
29
32
  }
33
+ /**
34
+ * Gets cached image from a texture, if available.
35
+ * @param babylonTexture texture to check for cached image
36
+ * @returns image data if found and directly usable; null otherwise
37
+ */
38
+ async function GetCachedImageAsync(babylonTexture) {
39
+ const internalTexture = babylonTexture.getInternalTexture();
40
+ if (!internalTexture || internalTexture.source !== 1 /* InternalTextureSource.Url */) {
41
+ return null;
42
+ }
43
+ if (internalTexture.invertY) {
44
+ return null;
45
+ }
46
+ const buffer = internalTexture._buffer;
47
+ let data;
48
+ let mimeType = babylonTexture.mimeType;
49
+ if (!buffer) {
50
+ data = await Tools.LoadFileAsync(internalTexture.url);
51
+ mimeType = GetMimeType(internalTexture.url) || mimeType;
52
+ }
53
+ else if (ArrayBuffer.isView(buffer)) {
54
+ data = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
55
+ }
56
+ else if (buffer instanceof ArrayBuffer) {
57
+ data = buffer;
58
+ }
59
+ else if (buffer instanceof Blob) {
60
+ data = await buffer.arrayBuffer();
61
+ mimeType = buffer.type || mimeType;
62
+ }
63
+ else if (typeof buffer === "string") {
64
+ data = await Tools.LoadFileAsync(buffer);
65
+ mimeType = GetMimeType(buffer) || mimeType;
66
+ }
67
+ else if (typeof HTMLImageElement !== "undefined" && buffer instanceof HTMLImageElement) {
68
+ data = await Tools.LoadFileAsync(buffer.src);
69
+ mimeType = GetMimeType(buffer.src) || mimeType;
70
+ }
71
+ if (data && mimeType) {
72
+ return { data, mimeType };
73
+ }
74
+ return null;
75
+ }
30
76
  /**
31
77
  * Computes the metallic factor from specular glossiness values.
32
78
  * @param diffuse diffused value
@@ -115,7 +161,7 @@ export class GLTFMaterialExporter {
115
161
  getTextureInfo(babylonTexture) {
116
162
  return babylonTexture ? (this._textureMap.get(babylonTexture) ?? null) : null;
117
163
  }
118
- async exportStandardMaterialAsync(babylonStandardMaterial, mimeType, hasUVs) {
164
+ async exportStandardMaterialAsync(babylonStandardMaterial, hasUVs) {
119
165
  const pbrMetallicRoughness = _ConvertToGLTFPBRMetallicRoughness(babylonStandardMaterial);
120
166
  const material = { name: babylonStandardMaterial.name };
121
167
  if (babylonStandardMaterial.backFaceCulling != null && !babylonStandardMaterial.backFaceCulling) {
@@ -128,7 +174,7 @@ export class GLTFMaterialExporter {
128
174
  const promises = [];
129
175
  const diffuseTexture = babylonStandardMaterial.diffuseTexture;
130
176
  if (diffuseTexture) {
131
- promises.push(this.exportTextureAsync(diffuseTexture, mimeType).then((textureInfo) => {
177
+ promises.push(this.exportTextureAsync(diffuseTexture).then((textureInfo) => {
132
178
  if (textureInfo) {
133
179
  pbrMetallicRoughness.baseColorTexture = textureInfo;
134
180
  }
@@ -136,7 +182,7 @@ export class GLTFMaterialExporter {
136
182
  }
137
183
  const bumpTexture = babylonStandardMaterial.bumpTexture;
138
184
  if (bumpTexture) {
139
- promises.push(this.exportTextureAsync(bumpTexture, mimeType).then((textureInfo) => {
185
+ promises.push(this.exportTextureAsync(bumpTexture).then((textureInfo) => {
140
186
  if (textureInfo) {
141
187
  material.normalTexture = textureInfo;
142
188
  if (bumpTexture.level !== 1) {
@@ -148,7 +194,7 @@ export class GLTFMaterialExporter {
148
194
  const emissiveTexture = babylonStandardMaterial.emissiveTexture;
149
195
  if (emissiveTexture) {
150
196
  material.emissiveFactor = [1.0, 1.0, 1.0];
151
- promises.push(this.exportTextureAsync(emissiveTexture, mimeType).then((textureInfo) => {
197
+ promises.push(this.exportTextureAsync(emissiveTexture).then((textureInfo) => {
152
198
  if (textureInfo) {
153
199
  material.emissiveTexture = textureInfo;
154
200
  }
@@ -156,7 +202,7 @@ export class GLTFMaterialExporter {
156
202
  }
157
203
  const ambientTexture = babylonStandardMaterial.ambientTexture;
158
204
  if (ambientTexture) {
159
- promises.push(this.exportTextureAsync(ambientTexture, mimeType).then((textureInfo) => {
205
+ promises.push(this.exportTextureAsync(ambientTexture).then((textureInfo) => {
160
206
  if (textureInfo) {
161
207
  const occlusionTexture = {
162
208
  index: textureInfo.index,
@@ -183,16 +229,16 @@ export class GLTFMaterialExporter {
183
229
  }
184
230
  material.pbrMetallicRoughness = pbrMetallicRoughness;
185
231
  SetAlphaMode(material, babylonStandardMaterial);
186
- await this._finishMaterialAsync(material, babylonStandardMaterial, mimeType);
232
+ await this._finishMaterialAsync(material, babylonStandardMaterial);
187
233
  const materials = this._exporter._materials;
188
234
  materials.push(material);
189
235
  return materials.length - 1;
190
236
  }
191
- async _finishMaterialAsync(glTFMaterial, babylonMaterial, mimeType) {
237
+ async _finishMaterialAsync(glTFMaterial, babylonMaterial) {
192
238
  const textures = this._exporter._extensionsPostExportMaterialAdditionalTextures("exportMaterial", glTFMaterial, babylonMaterial);
193
239
  const promises = [];
194
240
  for (const texture of textures) {
195
- promises.push(this.exportTextureAsync(texture, mimeType));
241
+ promises.push(this.exportTextureAsync(texture));
196
242
  }
197
243
  await Promise.all(promises);
198
244
  await this._exporter._extensionsPostExportMaterialAsync("exportMaterial", glTFMaterial, babylonMaterial);
@@ -408,12 +454,11 @@ export class GLTFMaterialExporter {
408
454
  /**
409
455
  * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors
410
456
  * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
411
- * @param mimeType mime type to use for the textures
412
457
  * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
413
458
  * @param hasUVs specifies if texture coordinates are present on the submesh to determine if textures should be applied
414
459
  * @returns glTF PBR Metallic Roughness factors
415
460
  */
416
- async _convertMetalRoughFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasUVs) {
461
+ async _convertMetalRoughFactorsToMetallicRoughnessAsync(babylonPBRMaterial, glTFPbrMetallicRoughness, hasUVs) {
417
462
  const promises = [];
418
463
  const metallicRoughness = {
419
464
  baseColor: babylonPBRMaterial._albedoColor,
@@ -423,7 +468,7 @@ export class GLTFMaterialExporter {
423
468
  if (hasUVs) {
424
469
  const albedoTexture = babylonPBRMaterial._albedoTexture;
425
470
  if (albedoTexture) {
426
- promises.push(this.exportTextureAsync(babylonPBRMaterial._albedoTexture, mimeType).then((glTFTexture) => {
471
+ promises.push(this.exportTextureAsync(albedoTexture).then((glTFTexture) => {
427
472
  if (glTFTexture) {
428
473
  glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
429
474
  }
@@ -431,7 +476,7 @@ export class GLTFMaterialExporter {
431
476
  }
432
477
  const metallicTexture = babylonPBRMaterial._metallicTexture;
433
478
  if (metallicTexture) {
434
- promises.push(this.exportTextureAsync(metallicTexture, mimeType).then((glTFTexture) => {
479
+ promises.push(this.exportTextureAsync(metallicTexture).then((glTFTexture) => {
435
480
  if (glTFTexture) {
436
481
  glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFTexture;
437
482
  }
@@ -541,12 +586,12 @@ export class GLTFMaterialExporter {
541
586
  /**
542
587
  * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors
543
588
  * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
544
- * @param mimeType mime type to use for the textures
545
589
  * @param pbrMetallicRoughness glTF PBR Metallic Roughness interface
546
590
  * @param hasUVs specifies if texture coordinates are present on the submesh to determine if textures should be applied
547
591
  * @returns glTF PBR Metallic Roughness factors
548
592
  */
549
- async _convertSpecGlossFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, pbrMetallicRoughness, hasUVs) {
593
+ async _convertSpecGlossFactorsToMetallicRoughnessAsync(babylonPBRMaterial, pbrMetallicRoughness, hasUVs) {
594
+ const mimeType = "image/png" /* ImageMimeType.PNG */;
550
595
  const specGloss = {
551
596
  diffuseColor: babylonPBRMaterial._albedoColor,
552
597
  specularColor: babylonPBRMaterial._reflectivityColor,
@@ -577,7 +622,7 @@ export class GLTFMaterialExporter {
577
622
  return this._convertSpecularGlossinessToMetallicRoughness(specGloss);
578
623
  }
579
624
  }
580
- async exportPBRMaterialAsync(babylonPBRMaterial, mimeType, hasUVs) {
625
+ async exportPBRMaterialAsync(babylonPBRMaterial, hasUVs) {
581
626
  const glTFPbrMetallicRoughness = {};
582
627
  const glTFMaterial = {
583
628
  name: babylonPBRMaterial.name,
@@ -591,15 +636,15 @@ export class GLTFMaterialExporter {
591
636
  }
592
637
  }
593
638
  const metallicRoughness = useMetallicRoughness
594
- ? await this._convertMetalRoughFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasUVs)
595
- : await this._convertSpecGlossFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasUVs);
596
- await this._setMetallicRoughnessPbrMaterialAsync(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasUVs);
597
- await this._finishMaterialAsync(glTFMaterial, babylonPBRMaterial, mimeType);
639
+ ? await this._convertMetalRoughFactorsToMetallicRoughnessAsync(babylonPBRMaterial, glTFPbrMetallicRoughness, hasUVs)
640
+ : await this._convertSpecGlossFactorsToMetallicRoughnessAsync(babylonPBRMaterial, glTFPbrMetallicRoughness, hasUVs);
641
+ await this._setMetallicRoughnessPbrMaterialAsync(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, hasUVs);
642
+ await this._finishMaterialAsync(glTFMaterial, babylonPBRMaterial);
598
643
  const materials = this._exporter._materials;
599
644
  materials.push(glTFMaterial);
600
645
  return materials.length - 1;
601
646
  }
602
- async _setMetallicRoughnessPbrMaterialAsync(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasUVs) {
647
+ async _setMetallicRoughnessPbrMaterialAsync(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, hasUVs) {
603
648
  SetAlphaMode(glTFMaterial, babylonPBRMaterial);
604
649
  if (!metallicRoughness.baseColor.equalsWithEpsilon(White, Epsilon) || !Scalar.WithinEpsilon(babylonPBRMaterial.alpha, 1, Epsilon)) {
605
650
  glTFPbrMetallicRoughness.baseColorFactor = [metallicRoughness.baseColor.r, metallicRoughness.baseColor.g, metallicRoughness.baseColor.b, babylonPBRMaterial.alpha];
@@ -620,7 +665,7 @@ export class GLTFMaterialExporter {
620
665
  const promises = [];
621
666
  const bumpTexture = babylonPBRMaterial._bumpTexture;
622
667
  if (bumpTexture) {
623
- promises.push(this.exportTextureAsync(bumpTexture, mimeType).then((glTFTexture) => {
668
+ promises.push(this.exportTextureAsync(bumpTexture).then((glTFTexture) => {
624
669
  if (glTFTexture) {
625
670
  glTFMaterial.normalTexture = glTFTexture;
626
671
  if (bumpTexture.level !== 1) {
@@ -631,7 +676,7 @@ export class GLTFMaterialExporter {
631
676
  }
632
677
  const ambientTexture = babylonPBRMaterial._ambientTexture;
633
678
  if (ambientTexture) {
634
- promises.push(this.exportTextureAsync(ambientTexture, mimeType).then((glTFTexture) => {
679
+ promises.push(this.exportTextureAsync(ambientTexture).then((glTFTexture) => {
635
680
  if (glTFTexture) {
636
681
  const occlusionTexture = {
637
682
  index: glTFTexture.index,
@@ -648,7 +693,7 @@ export class GLTFMaterialExporter {
648
693
  }
649
694
  const emissiveTexture = babylonPBRMaterial._emissiveTexture;
650
695
  if (emissiveTexture) {
651
- promises.push(this.exportTextureAsync(emissiveTexture, mimeType).then((glTFTexture) => {
696
+ promises.push(this.exportTextureAsync(emissiveTexture).then((glTFTexture) => {
652
697
  if (glTFTexture) {
653
698
  glTFMaterial.emissiveTexture = glTFTexture;
654
699
  }
@@ -665,58 +710,55 @@ export class GLTFMaterialExporter {
665
710
  }
666
711
  glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
667
712
  }
668
- async exportTextureAsync(babylonTexture, mimeType) {
669
- const extensionPromise = this._exporter._extensionsPreExportTextureAsync("exporter", babylonTexture, mimeType);
670
- if (!extensionPromise) {
671
- return await this._exportTextureInfoAsync(babylonTexture, mimeType);
672
- }
673
- return await extensionPromise.then(async (texture) => {
674
- if (!texture) {
675
- return await this._exportTextureInfoAsync(babylonTexture, mimeType);
676
- }
677
- return await this._exportTextureInfoAsync(texture, mimeType);
678
- });
679
- }
680
- async _exportTextureInfoAsync(babylonTexture, mimeType) {
713
+ async exportTextureAsync(babylonTexture) {
681
714
  let textureInfo = this._textureMap.get(babylonTexture);
682
- if (!textureInfo) {
683
- const pixels = await GetTextureDataAsync(babylonTexture).catch(() => null);
684
- if (!pixels) {
685
- return null;
686
- }
687
- const samplerIndex = this._exportTextureSampler(babylonTexture);
688
- // Preserve texture mime type if defined
689
- const textureMimeType = babylonTexture.mimeType;
690
- if (textureMimeType) {
691
- switch (textureMimeType) {
692
- case "image/jpeg":
693
- case "image/png":
694
- case "image/webp":
695
- mimeType = textureMimeType;
696
- break;
697
- default:
698
- Tools.Warn(`Unsupported media type: ${textureMimeType}. Exporting texture as PNG.`);
699
- // Will later fallback to default mime type, image/png, from Canvas API
700
- break;
715
+ if (textureInfo) {
716
+ return textureInfo;
717
+ }
718
+ const samplerIndex = this._exportTextureSampler(babylonTexture);
719
+ const imageIndex = await this._exportTextureImageAsync(babylonTexture);
720
+ textureInfo = this._exportTextureInfo(imageIndex, samplerIndex, babylonTexture.coordinatesIndex);
721
+ this._textureMap.set(babylonTexture, textureInfo);
722
+ this._exporter._extensionsPostExportTextures("exporter", textureInfo, babylonTexture);
723
+ return textureInfo;
724
+ }
725
+ async _exportTextureImageAsync(babylonTexture) {
726
+ const requestedMimeType = babylonTexture.mimeType ?? "none";
727
+ // TODO: Add an official way for users to export using a different mime type
728
+ // than the one they loaded with (which is denoted by Texture.mimeType)
729
+ const internalTextureToImage = this._internalTextureToImage;
730
+ const internalTextureUniqueId = babylonTexture.getInternalTexture().uniqueId;
731
+ internalTextureToImage[internalTextureUniqueId] = internalTextureToImage[internalTextureUniqueId] || {};
732
+ let imageIndexPromise = internalTextureToImage[internalTextureUniqueId][requestedMimeType];
733
+ if (imageIndexPromise === undefined) {
734
+ imageIndexPromise = (async () => {
735
+ // Try to get the image from memory first, if applicable
736
+ const cache = await GetCachedImageAsync(babylonTexture);
737
+ if (cache && (requestedMimeType === "none" || cache.mimeType === requestedMimeType)) {
738
+ return this._exportImage(babylonTexture.name, cache.mimeType, cache.data);
739
+ }
740
+ // Preserve texture mime type if defined
741
+ let mimeType = "image/png" /* ImageMimeType.PNG */;
742
+ if (requestedMimeType !== "none") {
743
+ switch (requestedMimeType) {
744
+ case "image/jpeg" /* ImageMimeType.JPEG */:
745
+ case "image/png" /* ImageMimeType.PNG */:
746
+ case "image/webp" /* ImageMimeType.WEBP */:
747
+ mimeType = requestedMimeType;
748
+ break;
749
+ default:
750
+ Tools.Warn(`Unsupported media type: ${requestedMimeType}. Exporting texture as PNG.`);
751
+ break;
752
+ }
701
753
  }
702
- }
703
- const internalTextureToImage = this._internalTextureToImage;
704
- const internalTextureUniqueId = babylonTexture.getInternalTexture().uniqueId;
705
- internalTextureToImage[internalTextureUniqueId] || (internalTextureToImage[internalTextureUniqueId] = {});
706
- let imageIndexPromise = internalTextureToImage[internalTextureUniqueId][mimeType];
707
- if (imageIndexPromise === undefined) {
708
754
  const size = babylonTexture.getSize();
709
- imageIndexPromise = (async () => {
710
- const data = await this._getImageDataAsync(pixels, size.width, size.height, mimeType);
711
- return this._exportImage(babylonTexture.name, mimeType, data);
712
- })();
713
- internalTextureToImage[internalTextureUniqueId][mimeType] = imageIndexPromise;
714
- }
715
- textureInfo = this._exportTextureInfo(await imageIndexPromise, samplerIndex, babylonTexture.coordinatesIndex);
716
- this._textureMap.set(babylonTexture, textureInfo);
717
- this._exporter._extensionsPostExportTextures("exporter", textureInfo, babylonTexture);
755
+ const pixels = await GetTextureDataAsync(babylonTexture);
756
+ const data = await this._getImageDataAsync(pixels, size.width, size.height, mimeType);
757
+ return this._exportImage(babylonTexture.name, mimeType, data);
758
+ })();
759
+ internalTextureToImage[internalTextureUniqueId][requestedMimeType] = imageIndexPromise;
718
760
  }
719
- return textureInfo;
761
+ return await imageIndexPromise;
720
762
  }
721
763
  _exportImage(name, mimeType, data) {
722
764
  const images = this._exporter._images;