@onerjs/serializers 8.30.4 → 8.30.6

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.
@@ -31,7 +31,6 @@ export declare class GLTFMaterialExporter {
31
31
  getTextureInfo(babylonTexture: Nullable<BaseTexture>): Nullable<ITextureInfo>;
32
32
  exportStandardMaterialAsync(babylonStandardMaterial: StandardMaterial, hasUVs: boolean): Promise<number>;
33
33
  private _finishMaterialAsync;
34
- private _getImageDataAsync;
35
34
  /**
36
35
  * Resizes the two source textures to the same dimensions. If a texture is null, a default white texture is generated. If both textures are null, returns null
37
36
  * @param texture1 first texture to resize
@@ -47,7 +46,6 @@ export declare class GLTFMaterialExporter {
47
46
  * @param diffuseTexture texture used to store diffuse information
48
47
  * @param specularGlossinessTexture texture used to store specular and glossiness information
49
48
  * @param factors specular glossiness material factors
50
- * @param mimeType the mime type to use for the texture
51
49
  * @returns pbr metallic roughness interface or null
52
50
  */
53
51
  private _convertSpecularGlossinessTexturesToMetallicRoughnessAsync;
@@ -98,7 +96,7 @@ export declare class GLTFMaterialExporter {
98
96
  exportOpenPBRMaterialAsync(babylonOpenPBRMaterial: OpenPBRMaterial, hasUVs: boolean): Promise<number>;
99
97
  exportTextureAsync(babylonTexture: BaseTexture, overrideId?: Nullable<number>): Promise<Nullable<ITextureInfo>>;
100
98
  private _exportTextureImageAsync;
101
- private _exportImage;
99
+ private _exportImageAsync;
102
100
  private _exportTextureInfo;
103
101
  private _exportTextureSampler;
104
102
  }
@@ -8,7 +8,7 @@ import { GetTextureDataAsync, TextureTools } from "@onerjs/core/Misc/textureTool
8
8
  import { Texture } from "@onerjs/core/Materials/Textures/texture.js";
9
9
  import { RawTexture } from "@onerjs/core/Materials/Textures/rawTexture.js";
10
10
  import { Constants } from "@onerjs/core/Engines/constants.js";
11
- import { DumpTools } from "@onerjs/core/Misc/dumpTools.js";
11
+ import { EncodeImageAsync } from "@onerjs/core/Misc/dumpTools.js";
12
12
  import { PBRBaseMaterial } from "@onerjs/core/Materials/PBR/pbrBaseMaterial.js";
13
13
  import { SpecularPowerToRoughness } from "@onerjs/core/Helpers/materialConversionHelper.js";
14
14
  import { GetMimeType } from "@onerjs/core/Misc/fileTools.js";
@@ -33,6 +33,22 @@ function GetFileExtensionFromMimeType(mimeType) {
33
33
  return ".ktx2";
34
34
  }
35
35
  }
36
+ /**
37
+ * @param mimeType the MIME type requested by the user
38
+ * @returns true if the given mime type is compatible with glTF
39
+ */
40
+ function IsSupportedMimeType(mimeType) {
41
+ switch (mimeType) {
42
+ case "image/jpeg" /* ImageMimeType.JPEG */:
43
+ case "image/png" /* ImageMimeType.PNG */:
44
+ case "image/webp" /* ImageMimeType.WEBP */:
45
+ case "image/avif" /* ImageMimeType.AVIF */:
46
+ case "image/ktx2" /* ImageMimeType.KTX2 */:
47
+ return true;
48
+ default:
49
+ return false;
50
+ }
51
+ }
36
52
  /**
37
53
  * Gets cached image from a texture, if available.
38
54
  * @param babylonTexture texture to check for cached image
@@ -71,8 +87,8 @@ async function GetCachedImageAsync(babylonTexture) {
71
87
  data = await Tools.LoadFileAsync(buffer.src);
72
88
  mimeType = GetMimeType(buffer.src) || mimeType;
73
89
  }
74
- if (data && mimeType) {
75
- return { data, mimeType };
90
+ if (data && IsSupportedMimeType(mimeType)) {
91
+ return new Blob([data], { type: mimeType });
76
92
  }
77
93
  return null;
78
94
  }
@@ -245,9 +261,6 @@ export class GLTFMaterialExporter {
245
261
  await Promise.all(promises);
246
262
  await this._exporter._extensionsPostExportMaterialAsync("exportMaterial", glTFMaterial, babylonMaterial);
247
263
  }
248
- async _getImageDataAsync(buffer, width, height, mimeType) {
249
- return await DumpTools.DumpDataAsync(width, height, buffer, mimeType, undefined, false, true);
250
- }
251
264
  /**
252
265
  * Resizes the two source textures to the same dimensions. If a texture is null, a default white texture is generated. If both textures are null, returns null
253
266
  * @param texture1 first texture to resize
@@ -294,10 +307,9 @@ export class GLTFMaterialExporter {
294
307
  * @param diffuseTexture texture used to store diffuse information
295
308
  * @param specularGlossinessTexture texture used to store specular and glossiness information
296
309
  * @param factors specular glossiness material factors
297
- * @param mimeType the mime type to use for the texture
298
310
  * @returns pbr metallic roughness interface or null
299
311
  */
300
- async _convertSpecularGlossinessTexturesToMetallicRoughnessAsync(diffuseTexture, specularGlossinessTexture, factors, mimeType) {
312
+ async _convertSpecularGlossinessTexturesToMetallicRoughnessAsync(diffuseTexture, specularGlossinessTexture, factors) {
301
313
  const promises = new Array();
302
314
  if (!(diffuseTexture || specularGlossinessTexture)) {
303
315
  return await Promise.reject("diffuse and specular glossiness textures are not defined!");
@@ -393,12 +405,12 @@ export class GLTFMaterialExporter {
393
405
  }
394
406
  }
395
407
  if (writeOutMetallicRoughnessTexture) {
396
- promises.push(this._getImageDataAsync(metallicRoughnessBuffer, width, height, mimeType).then((data) => {
408
+ promises.push(EncodeImageAsync(metallicRoughnessBuffer, width, height).then((data) => {
397
409
  metallicRoughnessFactors.metallicRoughnessTextureData = data;
398
410
  }));
399
411
  }
400
412
  if (writeOutBaseColorTexture) {
401
- promises.push(this._getImageDataAsync(baseColorBuffer, width, height, mimeType).then((data) => {
413
+ promises.push(EncodeImageAsync(baseColorBuffer, width, height).then((data) => {
402
414
  metallicRoughnessFactors.baseColorTextureData = data;
403
415
  }));
404
416
  }
@@ -648,7 +660,6 @@ export class GLTFMaterialExporter {
648
660
  * @returns glTF PBR Metallic Roughness factors
649
661
  */
650
662
  async _convertSpecGlossFactorsToMetallicRoughnessAsync(babylonPBRMaterial, pbrMetallicRoughness, hasUVs) {
651
- const mimeType = "image/png" /* ImageMimeType.PNG */;
652
663
  const specGloss = {
653
664
  diffuseColor: babylonPBRMaterial._albedoColor,
654
665
  specularColor: babylonPBRMaterial._reflectivityColor,
@@ -663,14 +674,14 @@ export class GLTFMaterialExporter {
663
674
  if ((albedoTexture || reflectivityTexture) && hasUVs) {
664
675
  this._exporter._materialNeedsUVsSet.add(babylonPBRMaterial);
665
676
  const samplerIndex = this._exportTextureSampler(albedoTexture || reflectivityTexture);
666
- const metallicRoughnessFactors = await this._convertSpecularGlossinessTexturesToMetallicRoughnessAsync(albedoTexture, reflectivityTexture, specGloss, mimeType);
677
+ const metallicRoughnessFactors = await this._convertSpecularGlossinessTexturesToMetallicRoughnessAsync(albedoTexture, reflectivityTexture, specGloss);
667
678
  const textures = this._exporter._textures;
668
679
  if (metallicRoughnessFactors.baseColorTextureData) {
669
- const imageIndex = this._exportImage(`baseColor${textures.length}`, mimeType, metallicRoughnessFactors.baseColorTextureData);
680
+ const imageIndex = await this._exportImageAsync(`baseColor${textures.length}`, metallicRoughnessFactors.baseColorTextureData);
670
681
  pbrMetallicRoughness.baseColorTexture = this._exportTextureInfo(imageIndex, samplerIndex, albedoTexture?.coordinatesIndex);
671
682
  }
672
683
  if (metallicRoughnessFactors.metallicRoughnessTextureData) {
673
- const imageIndex = this._exportImage(`metallicRoughness${textures.length}`, mimeType, metallicRoughnessFactors.metallicRoughnessTextureData);
684
+ const imageIndex = await this._exportImageAsync(`metallicRoughness${textures.length}`, metallicRoughnessFactors.metallicRoughnessTextureData);
674
685
  pbrMetallicRoughness.metallicRoughnessTexture = this._exportTextureInfo(imageIndex, samplerIndex, reflectivityTexture?.coordinatesIndex);
675
686
  }
676
687
  return metallicRoughnessFactors;
@@ -826,48 +837,46 @@ export class GLTFMaterialExporter {
826
837
  imageIndexPromise = (async () => {
827
838
  // Try to get the image from memory first, if applicable
828
839
  const cache = await GetCachedImageAsync(babylonTexture);
829
- if (cache && (requestedMimeType === "none" || cache.mimeType === requestedMimeType)) {
830
- return this._exportImage(babylonTexture.name, cache.mimeType, cache.data);
840
+ if (cache && (requestedMimeType === "none" || cache.type === requestedMimeType)) {
841
+ return await this._exportImageAsync(babylonTexture.name, cache);
831
842
  }
832
843
  // Preserve texture mime type if defined
833
844
  let mimeType = "image/png" /* ImageMimeType.PNG */;
834
845
  if (requestedMimeType !== "none") {
835
- switch (requestedMimeType) {
836
- case "image/jpeg" /* ImageMimeType.JPEG */:
837
- case "image/png" /* ImageMimeType.PNG */:
838
- case "image/webp" /* ImageMimeType.WEBP */:
839
- mimeType = requestedMimeType;
840
- break;
841
- default:
842
- Tools.Warn(`Unsupported media type: ${requestedMimeType}. Exporting texture as PNG.`);
843
- break;
846
+ if (IsSupportedMimeType(requestedMimeType)) {
847
+ mimeType = requestedMimeType;
848
+ }
849
+ else {
850
+ mimeType = "image/png" /* ImageMimeType.PNG */;
851
+ Tools.Warn(`Unsupported media type: ${requestedMimeType}. Exporting texture as PNG.`);
844
852
  }
845
853
  }
846
854
  const size = babylonTexture.getSize();
847
855
  const pixels = await GetTextureDataAsync(babylonTexture);
848
- const data = await this._getImageDataAsync(pixels, size.width, size.height, mimeType);
849
- return this._exportImage(babylonTexture.name, mimeType, data);
856
+ const imageData = await EncodeImageAsync(pixels, size.width, size.height, mimeType);
857
+ return await this._exportImageAsync(babylonTexture.name, imageData);
850
858
  })();
851
859
  internalTextureToImage[internalTextureUniqueId][requestedMimeType] = imageIndexPromise;
852
860
  }
853
861
  return await imageIndexPromise;
854
862
  }
855
- _exportImage(name, mimeType, data) {
863
+ async _exportImageAsync(name, imageData) {
856
864
  const images = this._exporter._images;
857
865
  let image;
858
866
  if (this._exporter._shouldUseGlb) {
859
867
  image = {
860
868
  name: name,
861
- mimeType: mimeType,
869
+ mimeType: imageData.type,
862
870
  bufferView: undefined, // Will be updated later by BufferManager
863
871
  };
872
+ const data = await imageData.arrayBuffer();
864
873
  const bufferView = this._exporter._bufferManager.createBufferView(new Uint8Array(data));
865
874
  this._exporter._bufferManager.setBufferView(image, bufferView);
866
875
  }
867
876
  else {
868
877
  // Build a unique URI
869
878
  const baseName = name.replace(/\.\/|\/|\.\\|\\/g, "_");
870
- const extension = GetFileExtensionFromMimeType(mimeType);
879
+ const extension = GetFileExtensionFromMimeType(imageData.type);
871
880
  let fileName = baseName + extension;
872
881
  if (images.some((image) => image.uri === fileName)) {
873
882
  fileName = `${baseName}_${Tools.RandomId()}${extension}`;
@@ -876,7 +885,7 @@ export class GLTFMaterialExporter {
876
885
  name: name,
877
886
  uri: fileName,
878
887
  };
879
- this._exporter._imageData[fileName] = { data: data, mimeType: mimeType }; // Save image data to be written to file later
888
+ this._exporter._imageData[fileName] = imageData; // Save image data to be written to file later
880
889
  }
881
890
  images.push(image);
882
891
  return images.length - 1;