@galacean/engine-loader 2.0.0-alpha.41 → 2.0.0-alpha.44

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, 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, 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';
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
 
@@ -115,10 +115,11 @@ function _create_class(Constructor, protoProps, staticProps) {
115
115
  }
116
116
 
117
117
  var BufferReader = /*#__PURE__*/ function() {
118
- function BufferReader(data, byteOffset, byteLength, littleEndian) {
118
+ function BufferReader(data, byteOffset, byteLength, littleEndian, header) {
119
119
  if (byteOffset === void 0) byteOffset = 0;
120
120
  if (littleEndian === void 0) littleEndian = true;
121
121
  this.data = data;
122
+ this.header = header;
122
123
  this._dataView = new DataView(data.buffer, data.byteOffset + byteOffset, byteLength != null ? byteLength : data.byteLength - byteOffset);
123
124
  this._littleEndian = littleEndian;
124
125
  this._position = 0;
@@ -318,8 +319,11 @@ var AnimationClipDecoder = /*#__PURE__*/ function() {
318
319
  function AnimationClipDecoder() {}
319
320
  AnimationClipDecoder.decode = function decode(engine, bufferReader) {
320
321
  return new AssetPromise(function(resolve) {
322
+ var _bufferReader_header;
321
323
  var name = bufferReader.nextStr();
322
324
  var clip = new AnimationClip(name);
325
+ var _bufferReader_header_version;
326
+ var version = (_bufferReader_header_version = (_bufferReader_header = bufferReader.header) == null ? void 0 : _bufferReader_header.version) != null ? _bufferReader_header_version : 1;
323
327
  var eventsLen = bufferReader.nextUint16();
324
328
  for(var i = 0; i < eventsLen; ++i){
325
329
  var event = new AnimationEvent();
@@ -333,6 +337,7 @@ var AnimationClipDecoder = /*#__PURE__*/ function() {
333
337
  var relativePath = bufferReader.nextStr();
334
338
  var componentStr = bufferReader.nextStr();
335
339
  var componentType = Loader.getClass(componentStr);
340
+ var typeIndex = version >= 2 ? bufferReader.nextUint16() : 0;
336
341
  var property = bufferReader.nextStr();
337
342
  var getProperty = bufferReader.nextStr();
338
343
  var curve = void 0;
@@ -494,7 +499,7 @@ var AnimationClipDecoder = /*#__PURE__*/ function() {
494
499
  break;
495
500
  }
496
501
  }
497
- clip.addCurveBinding(relativePath, componentType, property, getProperty, curve);
502
+ clip.addCurveBinding(relativePath, componentType, typeIndex, property, getProperty, curve);
498
503
  }
499
504
  resolve(clip);
500
505
  });
@@ -1323,8 +1328,8 @@ function _instanceof(left, right) {
1323
1328
  }
1324
1329
  var _proto = PrefabResource.prototype;
1325
1330
  /**
1326
- * Instantiate prefab.
1327
- * @returns prefab's root entity
1331
+ * Instantiate the prefab.
1332
+ * @returns A detached prefab instance that must be added to a parent Entity or Scene to update and render
1328
1333
  */ _proto.instantiate = function instantiate() {
1329
1334
  var _this__root;
1330
1335
  return (_this__root = this._root) == null ? void 0 : _this__root.clone();
@@ -1676,7 +1681,7 @@ var MaterialLoaderType = /*#__PURE__*/ function(MaterialLoaderType) {
1676
1681
  }
1677
1682
  var _decoderMap_header_type;
1678
1683
  var header = FileHeader.decode(arrayBuffer);
1679
- var bufferReader = new BufferReader(new Uint8Array(arrayBuffer), header.headerLength, header.dataLength);
1684
+ var bufferReader = new BufferReader(new Uint8Array(arrayBuffer), header.headerLength, header.dataLength, true, header);
1680
1685
  return (_decoderMap_header_type = decoderMap[header.type]).decode.apply(_decoderMap_header_type, [].concat([
1681
1686
  engine,
1682
1687
  bufferReader
@@ -2479,18 +2484,19 @@ var KTX2Loader = /*#__PURE__*/ function(Loader) {
2479
2484
  /**
2480
2485
  * @internal
2481
2486
  */ _proto.load = function load(item, resourceManager) {
2487
+ // @ts-expect-error -- internal method is omitted from public declarations
2488
+ var remoteUrl = resourceManager._getRemoteUrl(item.url);
2482
2489
  return new AssetPromise(function(resolve, reject, setTaskCompleteProgress, setTaskDetailProgress) {
2483
2490
  var requestConfig = _extends({}, item, {
2484
2491
  type: "arraybuffer"
2485
2492
  });
2486
- var url = item.url;
2487
2493
  resourceManager// @ts-ignore
2488
- ._request(url, requestConfig).onProgress(setTaskCompleteProgress, setTaskDetailProgress).then(function(buffer) {
2494
+ ._requestByRemoteUrl(remoteUrl, requestConfig).onProgress(setTaskCompleteProgress, setTaskDetailProgress).then(function(buffer) {
2489
2495
  return KTX2Loader._parseBuffer(new Uint8Array(buffer), resourceManager.engine, item.params).then(function(param) {
2490
2496
  var ktx2Container = param.ktx2Container, engine = param.engine, result = param.result, targetFormat = param.targetFormat, params = param.params;
2491
2497
  return KTX2Loader._createTextureByBuffer(engine, ktx2Container.isSRGB, result, targetFormat, params);
2492
2498
  }).then(function(texture) {
2493
- resourceManager.addContentRestorer(new KTX2ContentRestorer(texture, url, requestConfig));
2499
+ resourceManager.addContentRestorer(new KTX2ContentRestorer(texture, remoteUrl, requestConfig));
2494
2500
  resolve(texture);
2495
2501
  });
2496
2502
  }).catch(reject);
@@ -2674,9 +2680,9 @@ KTX2Loader = __decorate([
2674
2680
  ], KTX2Loader);
2675
2681
  var KTX2ContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
2676
2682
  _inherits(KTX2ContentRestorer, ContentRestorer);
2677
- function KTX2ContentRestorer(resource, url, requestConfig) {
2683
+ function KTX2ContentRestorer(resource, remoteUrl, requestConfig) {
2678
2684
  var _this;
2679
- _this = ContentRestorer.call(this, resource) || this, _this.url = url, _this.requestConfig = requestConfig;
2685
+ _this = ContentRestorer.call(this, resource) || this, _this.remoteUrl = remoteUrl, _this.requestConfig = requestConfig;
2680
2686
  return _this;
2681
2687
  }
2682
2688
  var _proto = KTX2ContentRestorer.prototype;
@@ -2686,7 +2692,7 @@ var KTX2ContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
2686
2692
  var engine = resource.engine;
2687
2693
  return new AssetPromise(function(resolve, reject) {
2688
2694
  engine.resourceManager// @ts-ignore
2689
- ._request(_this.url, requestConfig).then(function(buffer) {
2695
+ ._requestByRemoteUrl(_this.remoteUrl, requestConfig).then(function(buffer) {
2690
2696
  return KTX2Loader._parseBuffer(new Uint8Array(buffer), engine, requestConfig.params).then(function(param) {
2691
2697
  var ktx2Container = param.ktx2Container, engine = param.engine, result = param.result, targetFormat = param.targetFormat, params = param.params;
2692
2698
  return KTX2Loader._createTextureByBuffer(engine, ktx2Container.isSRGB, result, targetFormat, params, resource);
@@ -2704,21 +2710,22 @@ var AmbientLightLoader = /*#__PURE__*/ function(Loader) {
2704
2710
  }
2705
2711
  var _proto = AmbientLightLoader.prototype;
2706
2712
  _proto.load = function load(item, resourceManager) {
2713
+ // @ts-expect-error -- internal method is omitted from public declarations
2714
+ var remoteUrl = resourceManager._getRemoteUrl(item.url);
2707
2715
  return new AssetPromise(function(resolve, reject) {
2708
2716
  var requestConfig = _extends({}, item, {
2709
2717
  type: "arraybuffer"
2710
2718
  });
2711
2719
  var engine = resourceManager.engine;
2712
- var url = item.url;
2713
2720
  resourceManager// @ts-ignore
2714
- ._request(url, requestConfig).then(function(buffer) {
2721
+ ._requestByRemoteUrl(remoteUrl, requestConfig).then(function(buffer) {
2715
2722
  var header = FileHeader.decode(buffer);
2716
2723
  var dataOffset = header.headerLength;
2717
2724
  var sh = new SphericalHarmonics3();
2718
2725
  sh.copyFromArray(new Float32Array(buffer, dataOffset, 27));
2719
2726
  var textureOffset = dataOffset + AmbientLightLoader._shByteLength;
2720
2727
  return AmbientLightLoader._parseTexture(engine, buffer, textureOffset, header.dataLength - AmbientLightLoader._shByteLength).then(function(specularTexture) {
2721
- engine.resourceManager.addContentRestorer(new AmbientLightContentRestorer(specularTexture, url, requestConfig));
2728
+ engine.resourceManager.addContentRestorer(new AmbientLightContentRestorer(specularTexture, remoteUrl, requestConfig));
2722
2729
  var ambientLight = new AmbientLight(engine);
2723
2730
  ambientLight.diffuseMode = DiffuseMode.SphericalHarmonics;
2724
2731
  ambientLight.diffuseSphericalHarmonics = sh;
@@ -2767,9 +2774,9 @@ AmbientLightLoader = __decorate([
2767
2774
  * @internal
2768
2775
  */ var AmbientLightContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
2769
2776
  _inherits(AmbientLightContentRestorer, ContentRestorer);
2770
- function AmbientLightContentRestorer(resource, url, requestConfig) {
2777
+ function AmbientLightContentRestorer(resource, remoteUrl, requestConfig) {
2771
2778
  var _this;
2772
- _this = ContentRestorer.call(this, resource) || this, _this.url = url, _this.requestConfig = requestConfig;
2779
+ _this = ContentRestorer.call(this, resource) || this, _this.remoteUrl = remoteUrl, _this.requestConfig = requestConfig;
2773
2780
  return _this;
2774
2781
  }
2775
2782
  var _proto = AmbientLightContentRestorer.prototype;
@@ -2779,7 +2786,7 @@ AmbientLightLoader = __decorate([
2779
2786
  var resource = _this.resource;
2780
2787
  var engine = resource.engine;
2781
2788
  engine.resourceManager// @ts-ignore
2782
- ._request(_this.url, _this.requestConfig).then(function(buffer) {
2789
+ ._requestByRemoteUrl(_this.remoteUrl, _this.requestConfig).then(function(buffer) {
2783
2790
  var header = FileHeader.decode(buffer);
2784
2791
  var dataOffset = header.headerLength;
2785
2792
  var textureOffset = dataOffset + AmbientLightLoader._shByteLength;
@@ -2836,7 +2843,10 @@ var FontLoader = /*#__PURE__*/ function(Loader) {
2836
2843
  })).then(function(data) {
2837
2844
  var fontName = data.fontName, fontUrl = data.fontUrl;
2838
2845
  if (fontUrl) {
2839
- _this._registerFont(fontName, fontUrl).then(function() {
2846
+ var absoluteUrl = Utils.resolveAbsoluteUrl(item.url, fontUrl);
2847
+ // @ts-ignore
2848
+ var remoteUrl = resourceManager._getRemoteUrl(absoluteUrl);
2849
+ _this._registerFont(fontName, remoteUrl).then(function() {
2840
2850
  var font = new Font(resourceManager.engine, fontName);
2841
2851
  resolve(font);
2842
2852
  }).catch(function(e) {
@@ -3089,9 +3099,9 @@ function getMeshoptDecoder() {
3089
3099
  }
3090
3100
  var _proto = GLTFResource.prototype;
3091
3101
  /**
3092
- * Instantiate scene root entity.
3093
- * @param sceneIndex - Scene index
3094
- * @returns Root entity
3102
+ * Instantiate a glTF scene root Entity.
3103
+ * @param sceneIndex - The scene index
3104
+ * @returns A detached scene-root instance that must be added to a parent Entity or Scene to update and render
3095
3105
  */ _proto.instantiateSceneRoot = function instantiateSceneRoot(sceneIndex) {
3096
3106
  var sceneRoot = sceneIndex === undefined ? this._defaultSceneRoot : this._sceneRoots[sceneIndex];
3097
3107
  return sceneRoot.clone();
@@ -3330,6 +3340,8 @@ function getMeshoptDecoder() {
3330
3340
  detail.total = total;
3331
3341
  _this._setTaskDetailProgress(url, loaded, total);
3332
3342
  };
3343
+ // @ts-expect-error -- internal method is omitted from public declarations
3344
+ this._remoteAssetBaseURL = resourceManager._getRemoteUrl(glTFResource.url);
3333
3345
  this.contentRestorer = new GLTFContentRestorer(glTFResource);
3334
3346
  }
3335
3347
  var _proto = GLTFParserContext.prototype;
@@ -3420,7 +3432,7 @@ function getMeshoptDecoder() {
3420
3432
  var _this_glTFResource, _glTFResourceKey;
3421
3433
  ((_this_glTFResource = this.glTFResource)[_glTFResourceKey = glTFResourceKey] || (_this_glTFResource[_glTFResourceKey] = []))[index] = resource;
3422
3434
  } else {
3423
- var url = this.glTFResource.url;
3435
+ var remoteAssetBaseURL = this._remoteAssetBaseURL;
3424
3436
  resource.then(function(item) {
3425
3437
  if (index == undefined) {
3426
3438
  _this.glTFResource[glTFResourceKey] = item;
@@ -3432,15 +3444,15 @@ function getMeshoptDecoder() {
3432
3444
  for(var i = 0, length = item.length; i < length; i++){
3433
3445
  var mesh = item[i];
3434
3446
  // @ts-ignore
3435
- _this.resourceManager._onSubAssetSuccess(url, glTFResourceKey + "[" + index + "][" + i + "]", mesh);
3447
+ _this.resourceManager._onSubAssetSuccess(remoteAssetBaseURL, glTFResourceKey + "[" + index + "][" + i + "]", mesh);
3436
3448
  }
3437
3449
  } else {
3438
3450
  // @ts-ignore
3439
- _this.resourceManager._onSubAssetSuccess(url, "" + glTFResourceKey + (index === undefined ? "" : "[" + index + "]"), item);
3451
+ _this.resourceManager._onSubAssetSuccess(remoteAssetBaseURL, "" + glTFResourceKey + (index === undefined ? "" : "[" + index + "]"), item);
3440
3452
  var _this_glTF_scene;
3441
3453
  if (type === 2 && ((_this_glTF_scene = _this.glTF.scene) != null ? _this_glTF_scene : 0) === index) {
3442
3454
  // @ts-ignore
3443
- _this.resourceManager._onSubAssetSuccess(url, "defaultSceneRoot", item);
3455
+ _this.resourceManager._onSubAssetSuccess(remoteAssetBaseURL, "defaultSceneRoot", item);
3444
3456
  }
3445
3457
  }
3446
3458
  }).catch(function(e) {
@@ -4478,16 +4490,16 @@ var GLTFBufferParser = /*#__PURE__*/ function(GLTFParser) {
4478
4490
  _proto._parseSingleBuffer = function _parseSingleBuffer(context, bufferInfo) {
4479
4491
  var glTFResource = context.glTFResource, contentRestorer = context.contentRestorer, resourceManager = context.resourceManager;
4480
4492
  var url = glTFResource.url;
4493
+ var absoluteUrl = Utils.resolveAbsoluteUrl(url, bufferInfo.uri);
4481
4494
  // @ts-ignore
4482
- var remoteUrl = resourceManager._getRemoteUrl(url);
4495
+ var remoteUrl = resourceManager._getRemoteUrl(absoluteUrl);
4483
4496
  var restoreBufferRequests = contentRestorer.bufferRequests;
4484
4497
  var requestConfig = {
4485
4498
  type: "arraybuffer"
4486
4499
  };
4487
- var absoluteUrl = Utils.resolveAbsoluteUrl(remoteUrl, bufferInfo.uri);
4488
- restoreBufferRequests.push(new BufferRequestInfo(absoluteUrl, requestConfig));
4500
+ restoreBufferRequests.push(new BufferRequestInfo(remoteUrl, requestConfig));
4489
4501
  var promise = resourceManager// @ts-ignore
4490
- ._requestByRemoteUrl(absoluteUrl, requestConfig).onProgress(undefined, context._onTaskDetail);
4502
+ ._requestByRemoteUrl(remoteUrl, requestConfig).onProgress(undefined, context._onTaskDetail);
4491
4503
  context._addTaskCompletePromise(promise);
4492
4504
  return promise;
4493
4505
  };
@@ -5705,12 +5717,14 @@ var KTXLoader = /*#__PURE__*/ function(Loader) {
5705
5717
  }
5706
5718
  var _proto = KTXLoader.prototype;
5707
5719
  _proto.load = function load(item, resourceManager) {
5720
+ // @ts-expect-error -- internal method is omitted from public declarations
5721
+ var remoteUrl = resourceManager._getRemoteUrl(item.url);
5708
5722
  var requestConfig = _extends({}, item, {
5709
5723
  type: "arraybuffer"
5710
5724
  });
5711
5725
  return new AssetPromise(function(resolve, reject) {
5712
5726
  resourceManager// @ts-ignore
5713
- ._request(item.url, requestConfig).then(function(bin) {
5727
+ ._requestByRemoteUrl(remoteUrl, requestConfig).then(function(bin) {
5714
5728
  var parsedData = parseSingleKTX(bin);
5715
5729
  var width = parsedData.width, height = parsedData.height, mipmaps = parsedData.mipmaps, engineFormat = parsedData.engineFormat;
5716
5730
  var mipmap = mipmaps.length > 1;
@@ -5719,7 +5733,7 @@ var KTXLoader = /*#__PURE__*/ function(Loader) {
5719
5733
  var _mipmaps_miplevel = mipmaps[miplevel], width1 = _mipmaps_miplevel.width, height1 = _mipmaps_miplevel.height, data = _mipmaps_miplevel.data;
5720
5734
  texture.setPixelBuffer(data, miplevel, 0, 0, width1, height1);
5721
5735
  }
5722
- resourceManager.addContentRestorer(new KTXContentRestorer(texture, item.url, requestConfig));
5736
+ resourceManager.addContentRestorer(new KTXContentRestorer(texture, remoteUrl, requestConfig));
5723
5737
  resolve(texture);
5724
5738
  }).catch(function(e) {
5725
5739
  reject(e);
@@ -5735,9 +5749,9 @@ KTXLoader = __decorate([
5735
5749
  ], KTXLoader);
5736
5750
  var KTXContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
5737
5751
  _inherits(KTXContentRestorer, ContentRestorer);
5738
- function KTXContentRestorer(resource, url, requestConfig) {
5752
+ function KTXContentRestorer(resource, remoteUrl, requestConfig) {
5739
5753
  var _this;
5740
- _this = ContentRestorer.call(this, resource) || this, _this.url = url, _this.requestConfig = requestConfig;
5754
+ _this = ContentRestorer.call(this, resource) || this, _this.remoteUrl = remoteUrl, _this.requestConfig = requestConfig;
5741
5755
  return _this;
5742
5756
  }
5743
5757
  var _proto = KTXContentRestorer.prototype;
@@ -5747,7 +5761,7 @@ var KTXContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
5747
5761
  var engine = resource.engine;
5748
5762
  return new AssetPromise(function(resolve, reject) {
5749
5763
  engine.resourceManager// @ts-ignore
5750
- ._request(_this.url, _this.requestConfig).then(function(bin) {
5764
+ ._requestByRemoteUrl(_this.remoteUrl, _this.requestConfig).then(function(bin) {
5751
5765
  var mipmaps = parseSingleKTX(bin).mipmaps;
5752
5766
  for(var miplevel = 0; miplevel < mipmaps.length; miplevel++){
5753
5767
  var _mipmaps_miplevel = mipmaps[miplevel], width = _mipmaps_miplevel.width, height = _mipmaps_miplevel.height, data = _mipmaps_miplevel.data;
@@ -5762,6 +5776,7 @@ var KTXContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
5762
5776
  return KTXContentRestorer;
5763
5777
  }(ContentRestorer);
5764
5778
 
5779
+ var materialLoaderTypes = new Set(Object.values(MaterialLoaderType));
5765
5780
  var MaterialLoader = /*#__PURE__*/ function(Loader) {
5766
5781
  _inherits(MaterialLoader, Loader);
5767
5782
  function MaterialLoader() {
@@ -5770,23 +5785,32 @@ var MaterialLoader = /*#__PURE__*/ function(Loader) {
5770
5785
  var _proto = MaterialLoader.prototype;
5771
5786
  _proto.load = function load(item, resourceManager) {
5772
5787
  var _this = this;
5773
- return new AssetPromise(function(resolve, reject) {
5774
- resourceManager// @ts-ignore
5775
- ._request(item.url, _extends({}, item, {
5776
- type: "json"
5777
- })).then(function(materialSchema) {
5778
- var engine = resourceManager.engine;
5779
- var shaderRef = materialSchema.shaderRef, shaderName = materialSchema.shader;
5780
- var shader = Shader.find(shaderName);
5781
- if (shader) {
5782
- resolve(_this._getMaterialByShader(materialSchema, shader, engine));
5783
- } else if (shaderRef) {
5784
- resolve(resourceManager// @ts-ignore
5785
- .getResourceByRef(shaderRef).then(function(shader) {
5786
- return _this._getMaterialByShader(materialSchema, shader, engine);
5787
- }));
5788
+ // @ts-expect-error _request is @internal
5789
+ return resourceManager._request(item.url, _extends({}, item, {
5790
+ type: "json"
5791
+ })).then(function(materialSchema) {
5792
+ var shaderData = materialSchema.shaderData, shaderRef = materialSchema.shaderRef, shaderName = materialSchema.shader;
5793
+ for(var key in shaderData){
5794
+ var type = shaderData[key].type;
5795
+ if (!materialLoaderTypes.has(type)) {
5796
+ throw new Error('MaterialLoader: unsupported shader data type "' + type + '".');
5788
5797
  }
5789
- }).catch(reject);
5798
+ }
5799
+ var engine = resourceManager.engine;
5800
+ var shader = Shader.find(shaderName);
5801
+ if (shader) {
5802
+ return _this._getMaterialByShader(materialSchema, shader, engine);
5803
+ }
5804
+ if (!shaderRef) {
5805
+ throw new Error('MaterialLoader: shader "' + shaderName + '" not found.');
5806
+ }
5807
+ // @ts-expect-error getResourceByRef is @internal
5808
+ return resourceManager.getResourceByRef(shaderRef).then(function(shader) {
5809
+ if (!_instanceof(shader, Shader)) {
5810
+ throw new Error('MaterialLoader: shader reference "' + shaderRef.url + '" did not resolve to a Shader.');
5811
+ }
5812
+ return _this._getMaterialByShader(materialSchema, shader, engine);
5813
+ });
5790
5814
  });
5791
5815
  };
5792
5816
  _proto._getMaterialByShader = function _getMaterialByShader(materialSchema, shader, engine) {
@@ -5809,8 +5833,11 @@ var MaterialLoader = /*#__PURE__*/ function(Loader) {
5809
5833
  materialShaderData.setFloat(key, value);
5810
5834
  break;
5811
5835
  case MaterialLoaderType.Texture:
5812
- texturePromises.push(// @ts-ignore
5836
+ texturePromises.push(// @ts-expect-error getResourceByRef is @internal
5813
5837
  engine.resourceManager.getResourceByRef(value).then(function(texture) {
5838
+ if (!_instanceof(texture, Texture)) {
5839
+ throw new Error('MaterialLoader: texture reference "' + value.url + '" did not resolve to a Texture.');
5840
+ }
5814
5841
  materialShaderData.setTexture(key, texture);
5815
5842
  }));
5816
5843
  break;
@@ -5820,6 +5847,8 @@ var MaterialLoader = /*#__PURE__*/ function(Loader) {
5820
5847
  case MaterialLoaderType.Integer:
5821
5848
  materialShaderData.setInt(key, Number(value));
5822
5849
  break;
5850
+ default:
5851
+ throw new Error('MaterialLoader: unsupported shader data type "' + type + '".');
5823
5852
  }
5824
5853
  };
5825
5854
  var name = materialSchema.name, shaderData = materialSchema.shaderData, macros = materialSchema.macros;
@@ -5838,6 +5867,9 @@ var MaterialLoader = /*#__PURE__*/ function(Loader) {
5838
5867
  }
5839
5868
  return Promise.all(texturePromises).then(function() {
5840
5869
  return material;
5870
+ }, function(error) {
5871
+ material.destroy();
5872
+ throw error;
5841
5873
  });
5842
5874
  };
5843
5875
  return MaterialLoader;
@@ -5858,13 +5890,14 @@ var MeshLoader = /*#__PURE__*/ function(Loader) {
5858
5890
  var requestConfig = _extends({}, item, {
5859
5891
  type: "arraybuffer"
5860
5892
  });
5861
- var url = item.url;
5893
+ // @ts-expect-error -- internal method is omitted from public declarations
5894
+ var remoteUrl = resourceManager._getRemoteUrl(item.url);
5862
5895
  return new AssetPromise(function(resolve, reject) {
5863
5896
  resourceManager// @ts-ignore
5864
- ._request(url, requestConfig).then(function(data) {
5897
+ ._requestByRemoteUrl(remoteUrl, requestConfig).then(function(data) {
5865
5898
  return decode(data, resourceManager.engine);
5866
5899
  }).then(function(mesh) {
5867
- resourceManager.addContentRestorer(new MeshContentRestorer(mesh, url, requestConfig));
5900
+ resourceManager.addContentRestorer(new MeshContentRestorer(mesh, remoteUrl, requestConfig));
5868
5901
  resolve(mesh);
5869
5902
  }).catch(reject);
5870
5903
  });
@@ -5878,9 +5911,9 @@ MeshLoader = __decorate([
5878
5911
  ], MeshLoader);
5879
5912
  var MeshContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
5880
5913
  _inherits(MeshContentRestorer, ContentRestorer);
5881
- function MeshContentRestorer(resource, url, requestConfig) {
5914
+ function MeshContentRestorer(resource, remoteUrl, requestConfig) {
5882
5915
  var _this;
5883
- _this = ContentRestorer.call(this, resource) || this, _this.url = url, _this.requestConfig = requestConfig;
5916
+ _this = ContentRestorer.call(this, resource) || this, _this.remoteUrl = remoteUrl, _this.requestConfig = requestConfig;
5884
5917
  return _this;
5885
5918
  }
5886
5919
  var _proto = MeshContentRestorer.prototype;
@@ -5890,7 +5923,7 @@ var MeshContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
5890
5923
  var engine = resource.engine;
5891
5924
  return new AssetPromise(function(resolve, reject) {
5892
5925
  engine.resourceManager// @ts-ignore
5893
- ._request(_this.url, _this.requestConfig).then(function(data) {
5926
+ ._requestByRemoteUrl(_this.remoteUrl, _this.requestConfig).then(function(data) {
5894
5927
  return decode(data, engine, resource);
5895
5928
  }).then(function(mesh) {
5896
5929
  resolve(mesh);
@@ -6196,14 +6229,16 @@ var TextureLoader = /*#__PURE__*/ function(Loader) {
6196
6229
  _proto.load = function load(item, resourceManager) {
6197
6230
  var _this = this;
6198
6231
  var url = item.url;
6232
+ // @ts-expect-error -- internal method is omitted from public declarations
6233
+ var remoteUrl = resourceManager._getRemoteUrl(url);
6199
6234
  var requestConfig = _extends({}, item, {
6200
6235
  type: "arraybuffer"
6201
6236
  });
6202
6237
  return new AssetPromise(function(resolve, reject, setTaskCompleteProgress, setTaskDetailProgress) {
6203
6238
  resourceManager// @ts-ignore
6204
- ._request(url, requestConfig).onProgress(setTaskCompleteProgress, setTaskDetailProgress).then(function(buffer) {
6239
+ ._requestByRemoteUrl(remoteUrl, requestConfig).onProgress(setTaskCompleteProgress, setTaskDetailProgress).then(function(buffer) {
6205
6240
  _this._decode(buffer, item, resourceManager).then(function(texture) {
6206
- resourceManager.addContentRestorer(new TextureContentRestorer(texture, url, requestConfig));
6241
+ resourceManager.addContentRestorer(new TextureContentRestorer(texture, url, remoteUrl, requestConfig));
6207
6242
  resolve(texture);
6208
6243
  }, reject);
6209
6244
  }).catch(reject);
@@ -6240,29 +6275,17 @@ var TextureLoader = /*#__PURE__*/ function(Loader) {
6240
6275
  };
6241
6276
  _proto._decodeImage = function _decodeImage(buffer, item, resourceManager) {
6242
6277
  var _this = this;
6243
- return new AssetPromise(function(resolve, reject) {
6244
- var blob = new Blob([
6245
- buffer
6246
- ]);
6247
- var img = new Image();
6248
- img.onload = function() {
6249
- URL.revokeObjectURL(img.src);
6250
- var _item_params;
6251
- var _ref = (_item_params = item.params) != null ? _item_params : {}, _ref_format = _ref.format, format = _ref_format === void 0 ? TextureFormat.R8G8B8A8 : _ref_format, _ref_isSRGBColorSpace = _ref.isSRGBColorSpace, isSRGBColorSpace = _ref_isSRGBColorSpace === void 0 ? true : _ref_isSRGBColorSpace, _ref_mipmap = _ref.mipmap, mipmap = _ref_mipmap === void 0 ? true : _ref_mipmap;
6252
- var engine = resourceManager.engine;
6253
- var width = img.width, height = img.height;
6254
- var generateMipmap = TextureUtils.supportGenerateMipmapsWithCorrection(engine, width, height, format, mipmap, isSRGBColorSpace);
6255
- var texture = new Texture2D(engine, width, height, format, generateMipmap, isSRGBColorSpace);
6256
- texture.setImageSource(img);
6257
- generateMipmap && texture.generateMipmaps();
6258
- _this._applyParams(texture, item);
6259
- resolve(texture);
6260
- };
6261
- img.onerror = function(e) {
6262
- URL.revokeObjectURL(img.src);
6263
- reject(e);
6264
- };
6265
- img.src = URL.createObjectURL(blob);
6278
+ return decodeImage(buffer, item.url).then(function(img) {
6279
+ var _item_params;
6280
+ var _ref = (_item_params = item.params) != null ? _item_params : {}, _ref_format = _ref.format, format = _ref_format === void 0 ? TextureFormat.R8G8B8A8 : _ref_format, _ref_isSRGBColorSpace = _ref.isSRGBColorSpace, isSRGBColorSpace = _ref_isSRGBColorSpace === void 0 ? true : _ref_isSRGBColorSpace, _ref_mipmap = _ref.mipmap, mipmap = _ref_mipmap === void 0 ? true : _ref_mipmap;
6281
+ var engine = resourceManager.engine;
6282
+ var width = img.width, height = img.height;
6283
+ var generateMipmap = TextureUtils.supportGenerateMipmapsWithCorrection(engine, width, height, format, mipmap, isSRGBColorSpace);
6284
+ var texture = new Texture2D(engine, width, height, format, generateMipmap, isSRGBColorSpace);
6285
+ texture.setImageSource(img);
6286
+ generateMipmap && texture.generateMipmaps();
6287
+ _this._applyParams(texture, item);
6288
+ return texture;
6266
6289
  });
6267
6290
  };
6268
6291
  _proto._applyParams = function _applyParams(texture, item) {
@@ -6291,16 +6314,16 @@ TextureLoader = __decorate([
6291
6314
  ], TextureLoader);
6292
6315
  var TextureContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
6293
6316
  _inherits(TextureContentRestorer, ContentRestorer);
6294
- function TextureContentRestorer(resource, url, requestConfig) {
6317
+ function TextureContentRestorer(resource, url, remoteUrl, requestConfig) {
6295
6318
  var _this;
6296
- _this = ContentRestorer.call(this, resource) || this, _this.url = url, _this.requestConfig = requestConfig;
6319
+ _this = ContentRestorer.call(this, resource) || this, _this.url = url, _this.remoteUrl = remoteUrl, _this.requestConfig = requestConfig;
6297
6320
  return _this;
6298
6321
  }
6299
6322
  var _proto = TextureContentRestorer.prototype;
6300
6323
  _proto.restoreContent = function restoreContent() {
6301
6324
  var _this = this;
6302
6325
  return this.resource.engine.resourceManager// @ts-ignore
6303
- ._request(this.url, this.requestConfig).then(function(buffer) {
6326
+ ._requestByRemoteUrl(this.remoteUrl, this.requestConfig).then(function(buffer) {
6304
6327
  if (FileHeader.checkMagic(buffer)) {
6305
6328
  return decode(buffer, _this.resource.engine, _this.resource);
6306
6329
  }
@@ -6312,27 +6335,35 @@ var TextureContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
6312
6335
  texture.mipmapCount > 1 && texture.generateMipmaps();
6313
6336
  return texture;
6314
6337
  }
6315
- return new AssetPromise(function(resolve, reject) {
6316
- var blob = new Blob([
6317
- buffer
6318
- ]);
6319
- var img = new Image();
6320
- img.onload = function() {
6321
- URL.revokeObjectURL(img.src);
6322
- texture.setImageSource(img);
6323
- texture.mipmapCount > 1 && texture.generateMipmaps();
6324
- resolve(texture);
6325
- };
6326
- img.onerror = function(e) {
6327
- URL.revokeObjectURL(img.src);
6328
- reject(e);
6329
- };
6330
- img.src = URL.createObjectURL(blob);
6338
+ return decodeImage(buffer, _this.url).then(function(img) {
6339
+ texture.setImageSource(img);
6340
+ texture.mipmapCount > 1 && texture.generateMipmaps();
6341
+ return texture;
6331
6342
  });
6332
6343
  });
6333
6344
  };
6334
6345
  return TextureContentRestorer;
6335
6346
  }(ContentRestorer);
6347
+ function decodeImage(buffer, url) {
6348
+ return new AssetPromise(function(resolve, reject) {
6349
+ var objectUrl = URL.createObjectURL(new Blob([
6350
+ buffer
6351
+ ]));
6352
+ var image = new Image();
6353
+ var releaseObjectUrl = function() {
6354
+ return URL.revokeObjectURL(objectUrl);
6355
+ };
6356
+ image.onload = function() {
6357
+ releaseObjectUrl();
6358
+ resolve(image);
6359
+ };
6360
+ image.onerror = function() {
6361
+ releaseObjectUrl();
6362
+ reject(new Error('TextureLoader: failed to decode texture "' + url + '" (' + buffer.byteLength + " bytes)."));
6363
+ };
6364
+ image.src = objectUrl;
6365
+ });
6366
+ }
6336
6367
 
6337
6368
  var AudioLoader = /*#__PURE__*/ function(Loader) {
6338
6369
  _inherits(AudioLoader, Loader);
@@ -6397,11 +6428,11 @@ var ShaderLoader = /*#__PURE__*/ function(Loader) {
6397
6428
  type: "text"
6398
6429
  })).then(function(code) {
6399
6430
  var source = code.trimStart();
6400
- if (source.startsWith("{")) {
6401
- // @ts-expect-error _createFromPrecompiled is @internal
6402
- return Shader._createFromPrecompiled(JSON.parse(source));
6431
+ var shader = source.startsWith("{") ? Shader._createFromPrecompiled(JSON.parse(source)) : Shader.create(code, undefined, url);
6432
+ if (!shader) {
6433
+ throw new Error('ShaderLoader: failed to create shader "' + url + '".');
6403
6434
  }
6404
- return Shader.create(code, undefined, url);
6435
+ return shader;
6405
6436
  });
6406
6437
  };
6407
6438
  return ShaderLoader;
@@ -6449,8 +6480,10 @@ var RenderTargetLoader = /*#__PURE__*/ function(Loader) {
6449
6480
  var _proto = RenderTargetLoader.prototype;
6450
6481
  _proto.load = function load(item, resourceManager) {
6451
6482
  var engine = resourceManager.engine;
6483
+ // @ts-expect-error -- internal method is omitted from public declarations
6484
+ var remoteAssetBaseURL = resourceManager._getRemoteUrl(item.url);
6452
6485
  return resourceManager// @ts-ignore
6453
- ._request(item.url, _extends({}, item, {
6486
+ ._requestByRemoteUrl(remoteAssetBaseURL, _extends({}, item, {
6454
6487
  type: "json"
6455
6488
  })).then(function(data) {
6456
6489
  var width = data.width, height = data.height, colorFormats = data.colorFormats, depthFormat = data.depthFormat, antiAliasing = data.antiAliasing, autoGenerateMipmaps = data.autoGenerateMipmaps;
@@ -6476,7 +6509,7 @@ var RenderTargetLoader = /*#__PURE__*/ function(Loader) {
6476
6509
  // Notify pending sub-asset requests for colorTextures
6477
6510
  for(var i = 0, n = colorTextures.length; i < n; i++){
6478
6511
  // @ts-ignore
6479
- resourceManager._onSubAssetSuccess(item.url, "colorTextures[" + i + "]", colorTextures[i]);
6512
+ resourceManager._onSubAssetSuccess(remoteAssetBaseURL, "colorTextures[" + i + "]", colorTextures[i]);
6480
6513
  }
6481
6514
  return rt;
6482
6515
  });