@galacean/engine-loader 2.0.0-alpha.57 → 2.0.0-alpha.58

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.
package/dist/module.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Utils, AssetPromise, AnimationClip, AnimationEvent, Loader, AnimationStringCurve, Keyframe, AnimationBoolCurve, AnimationRefCurve, AnimationQuaternionCurve, AnimationColorCurve, AnimationVector4Curve, AnimationVector3Curve, AnimationVector2Curve, AnimationFloatArrayCurve, AnimationArrayCurve, AnimationFloatCurve, ModelMesh, BlendShape, TextureFormat, Texture2D, TextureCube, TextureCubeFace, ReferResource, Entity, resourceLoader, AssetType, AnimatorController, AnimatorControllerLayer, AnimatorStateTransition, BufferAsset, GLCapabilityType, Logger, ContentRestorer, AmbientLight, DiffuseMode, TextureFilterMode, Font, SystemInfo, Animator, IndexFormat, VertexElementFormat, request, InterpolationType, SkinnedMeshRenderer, Transform, PBRMaterial, TextureCoordinate, RenderFace, VertexElement, Buffer, BufferBindFlag, BufferUsage, Camera, MeshRenderer, Skin, TextureWrapMode as TextureWrapMode$1, TextureUtils, AnimatorStateMachine, JSONAsset, Shader, Material, Texture, PrimitiveMesh, SpriteAtlas, Sprite, TextAsset, AudioClip, PhysicsMaterial, RenderTarget, Scene, BackgroundMode, DirectLight, PointLight, SpotLight, UnlitMaterial } from '@galacean/engine-core';
1
+ import { Utils, AssetPromise, AnimationClip, AnimationEvent, Loader, AnimationStringCurve, Keyframe, AnimationBoolCurve, AnimationRefCurve, AnimationQuaternionCurve, AnimationColorCurve, AnimationVector4Curve, AnimationVector3Curve, AnimationVector2Curve, AnimationFloatArrayCurve, AnimationArrayCurve, AnimationFloatCurve, ModelMesh, BlendShape, SystemInfo, Platform, TextureFormat, Texture2D, TextureCube, TextureCubeFace, ReferResource, Entity, resourceLoader, AssetType, AnimatorController, AnimatorControllerLayer, AnimatorStateTransition, BufferAsset, GLCapabilityType, Logger, ContentRestorer, AmbientLight, DiffuseMode, TextureFilterMode, Font, Animator, IndexFormat, VertexElementFormat, request, InterpolationType, SkinnedMeshRenderer, Transform, PBRMaterial, TextureCoordinate, RenderFace, VertexElement, Buffer, BufferBindFlag, BufferUsage, Camera, MeshRenderer, Skin, TextureWrapMode as TextureWrapMode$1, TextureUtils, AnimatorStateMachine, JSONAsset, Shader, Material, Texture, PrimitiveMesh, SpriteAtlas, Sprite, TextAsset, AudioClip, PhysicsMaterial, RenderTarget, Scene, BackgroundMode, DirectLight, PointLight, SpotLight, UnlitMaterial } from '@galacean/engine-core';
2
2
  import { Quaternion, Vector4, Color, Vector3, Vector2, MathUtil, SphericalHarmonics3, Matrix, BoundingBox, Rect } from '@galacean/engine-math';
3
3
  import { GLCompressedTextureInternalFormat } from '@galacean/engine-rhi-webgl';
4
4
 
@@ -1203,32 +1203,42 @@ function _instanceof(left, right) {
1203
1203
  /**
1204
1204
  * Decodes encoded image bytes (PNG, JPEG, WebP, ...) into an image element.
1205
1205
  *
1206
- * The blob handed to the image carries the MIME type detected from the bytes: browsers sniff an untyped
1207
- * blob, but some platforms (for example mini-game runtimes) only decode images whose type is declared.
1206
+ * The blob handed to the image carries the declared `mimeType` or the MIME type detected from the bytes:
1207
+ * browsers sniff an untyped blob, but some platforms (for example mini-game runtimes) only decode images
1208
+ * whose type is declared.
1208
1209
  * @internal
1209
1210
  */ var ImageDecoder = /*#__PURE__*/ function() {
1210
1211
  function ImageDecoder() {}
1211
- ImageDecoder._decode = function _decode(data, url) {
1212
+ ImageDecoder._decode = function _decode(data, url, mimeType) {
1212
1213
  return new AssetPromise(function(resolve, reject) {
1213
1214
  var objectUrl = URL.createObjectURL(new Blob([
1214
1215
  data
1215
1216
  ], {
1216
- type: ImageDecoder._getMimeType(data)
1217
+ type: mimeType || ImageDecoder._getMimeType(data)
1217
1218
  }));
1218
1219
  var image = new Image();
1219
1220
  image.onload = function() {
1220
- ImageDecoder._releaseObjectUrl(image, objectUrl);
1221
- resolve(image);
1221
+ var complete = function() {
1222
+ ImageDecoder._releaseObjectUrl(image, objectUrl);
1223
+ resolve(image);
1224
+ };
1225
+ if ((SystemInfo.platform === Platform.IPhone || SystemInfo.platform === Platform.IPad) && typeof requestAnimationFrame !== "undefined") {
1226
+ // Defer on iOS WebKit so the decoded image surface is ready before revoking the blob URL.
1227
+ requestAnimationFrame(complete);
1228
+ } else {
1229
+ complete();
1230
+ }
1222
1231
  };
1223
- image.onerror = function() {
1232
+ image.onerror = image.onabort = function() {
1224
1233
  ImageDecoder._releaseObjectUrl(image, objectUrl);
1225
1234
  reject(new Error('TextureLoader: failed to decode texture "' + url + '" (' + data.byteLength + " bytes)."));
1226
1235
  };
1236
+ image.crossOrigin = "anonymous";
1227
1237
  image.src = objectUrl;
1228
1238
  });
1229
1239
  };
1230
1240
  ImageDecoder._getMimeType = function _getMimeType(data) {
1231
- var bytes = _instanceof(data, Uint8Array) ? data : new Uint8Array(data);
1241
+ var bytes = _instanceof(data, Uint8Array) ? data : ArrayBuffer.isView(data) ? new Uint8Array(data.buffer, data.byteOffset, data.byteLength) : new Uint8Array(data);
1232
1242
  if (bytes.length >= 4 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) {
1233
1243
  return "image/png";
1234
1244
  }
@@ -1244,6 +1254,7 @@ function _instanceof(left, right) {
1244
1254
  ImageDecoder._releaseObjectUrl = function _releaseObjectUrl(image, objectUrl) {
1245
1255
  image.onload = null;
1246
1256
  image.onerror = null;
1257
+ image.onabort = null;
1247
1258
  URL.revokeObjectURL(objectUrl);
1248
1259
  };
1249
1260
  return ImageDecoder;
@@ -3781,30 +3792,7 @@ function registerGLTFParser(pipeline) {
3781
3792
  /**
3782
3793
  * Load image buffer
3783
3794
  */ GLTFUtils.loadImageBuffer = function loadImageBuffer(imageBuffer, type) {
3784
- return new Promise(function(resolve, reject) {
3785
- var blob = new window.Blob([
3786
- imageBuffer
3787
- ], {
3788
- type: type
3789
- });
3790
- var img = new Image();
3791
- img.onerror = function() {
3792
- URL.revokeObjectURL(img.src);
3793
- reject(new Error("Failed to load image buffer"));
3794
- };
3795
- img.onload = function() {
3796
- // Call requestAnimationFrame to avoid iOS's bug.
3797
- requestAnimationFrame(function() {
3798
- URL.revokeObjectURL(img.src);
3799
- resolve(img);
3800
- img.onload = null;
3801
- img.onerror = null;
3802
- img.onabort = null;
3803
- });
3804
- };
3805
- img.crossOrigin = "anonymous";
3806
- img.src = URL.createObjectURL(blob);
3807
- });
3795
+ return Promise.resolve(ImageDecoder._decode(imageBuffer, type, type));
3808
3796
  };
3809
3797
  /**
3810
3798
  * Parse the glb format.
@@ -3943,7 +3931,7 @@ function registerGLTFParser(pipeline) {
3943
3931
  }
3944
3932
  });
3945
3933
  } else {
3946
- return GLTFUtils.loadImageBuffer(bufferData, textureRestoreInfo.mimeType).then(function(image) {
3934
+ return ImageDecoder._decode(bufferData, _this.resource.url, textureRestoreInfo.mimeType).then(function(image) {
3947
3935
  texture.setImageSource(image);
3948
3936
  texture.generateMipmaps();
3949
3937
  });
@@ -5211,8 +5199,9 @@ var GLTFTextureParser = /*#__PURE__*/ function(GLTFParser1) {
5211
5199
  var bufferView = glTF.bufferViews[bufferViewIndex];
5212
5200
  texture = context.get(GLTFParserType.Buffer).then(function(buffers) {
5213
5201
  var buffer = buffers[bufferView.buffer];
5214
- var imageBuffer = new Uint8Array(buffer, bufferView.byteOffset, bufferView.byteLength);
5215
- return GLTFUtils.loadImageBuffer(imageBuffer, mimeType).then(function(image) {
5202
+ var _bufferView_byteOffset;
5203
+ var imageBuffer = new Uint8Array(buffer, (_bufferView_byteOffset = bufferView.byteOffset) != null ? _bufferView_byteOffset : 0, bufferView.byteLength);
5204
+ return ImageDecoder._decode(imageBuffer, imageName || textureName || url, mimeType).then(function(image) {
5216
5205
  var width = image.width, height = image.height;
5217
5206
  var format = TextureFormat.R8G8B8A8;
5218
5207
  var _samplerInfo_mipmap;
@@ -5226,8 +5215,6 @@ var GLTFTextureParser = /*#__PURE__*/ function(GLTFParser1) {
5226
5215
  context.contentRestorer.bufferTextures.push(bufferTextureRestoreInfo);
5227
5216
  return texture;
5228
5217
  });
5229
- }).catch(function(e) {
5230
- Logger.error("GLTFTextureParser: image buffer error", e);
5231
5218
  });
5232
5219
  }
5233
5220
  return texture;