@galacean/engine-loader 0.0.0-experimental-2.0-migrate.5 → 0.0.0-experimental-2.0-migrate.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.
package/dist/main.js CHANGED
@@ -643,6 +643,39 @@ function float32ArrayToVector2(float32Array, vertexCount) {
643
643
  return array;
644
644
  }
645
645
 
646
+ function _is_native_reflect_construct() {
647
+ // Since Reflect.construct can't be properly polyfilled, some
648
+ // implementations (e.g. core-js@2) don't set the correct internal slots.
649
+ // Those polyfills don't allow us to subclass built-ins, so we need to
650
+ // use our fallback implementation.
651
+ try {
652
+ // If the internal slots aren't set, this throws an error similar to
653
+ // TypeError: this is not a Boolean object.
654
+ var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
655
+ } catch (_) {}
656
+ return (_is_native_reflect_construct = function() {
657
+ return !!result;
658
+ })();
659
+ }
660
+
661
+ function _construct(Parent, args, Class) {
662
+ if (_is_native_reflect_construct()) _construct = Reflect.construct;
663
+ else {
664
+ _construct = function construct(Parent, args, Class) {
665
+ var a = [null];
666
+ a.push.apply(a, args);
667
+ var Constructor = Function.bind.apply(Parent, a);
668
+ var instance = new Constructor();
669
+
670
+ if (Class) _set_prototype_of(instance, Class.prototype);
671
+
672
+ return instance;
673
+ };
674
+ }
675
+
676
+ return _construct.apply(null, arguments);
677
+ }
678
+
646
679
  function _object_without_properties_loose(source, excluded) {
647
680
  if (source == null) return {};
648
681
 
@@ -659,6 +692,45 @@ function _object_without_properties_loose(source, excluded) {
659
692
  return target;
660
693
  }
661
694
 
695
+ function _array_like_to_array(arr, len) {
696
+ if (len == null || len > arr.length) len = arr.length;
697
+
698
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
699
+
700
+ return arr2;
701
+ }
702
+
703
+ function _unsupported_iterable_to_array(o, minLen) {
704
+ if (!o) return;
705
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
706
+
707
+ var n = Object.prototype.toString.call(o).slice(8, -1);
708
+
709
+ if (n === "Object" && o.constructor) n = o.constructor.name;
710
+ if (n === "Map" || n === "Set") return Array.from(n);
711
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
712
+ }
713
+
714
+ function _create_for_of_iterator_helper_loose(o, allowArrayLike) {
715
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
716
+
717
+ if (it) return (it = it.call(o)).next.bind(it);
718
+ // Fallback for engines without symbol support
719
+ if (Array.isArray(o) || (it = _unsupported_iterable_to_array(o)) || allowArrayLike && o && typeof o.length === "number") {
720
+ if (it) o = it;
721
+
722
+ var i = 0;
723
+
724
+ return function() {
725
+ if (i >= o.length) return { done: true };
726
+
727
+ return { done: false, value: o[i++] };
728
+ };
729
+ }
730
+
731
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
732
+ }
733
+
662
734
  function resolveRefItem(refs, index, owner, label) {
663
735
  if (!Number.isInteger(index) || index < 0 || index >= refs.length) {
664
736
  throw new Error(owner + ": invalid ref index " + index + " for " + label);
@@ -709,7 +781,7 @@ var ReflectionParser = /*#__PURE__*/ function() {
709
781
  var _proto = ReflectionParser.prototype;
710
782
  /**
711
783
  * Apply v2 props to a component/object instance.
712
- * Each prop value is resolved recursively (handling $ref, $type, $class, $entity, $component, $signal).
784
+ * Each prop value is resolved recursively (handling $ref, $number, $type, $class, $entity, $component, $signal).
713
785
  */ _proto.parseProps = function parseProps(instance, props) {
714
786
  var promises = [];
715
787
  if (props) {
@@ -732,15 +804,29 @@ var ReflectionParser = /*#__PURE__*/ function() {
732
804
  var _loop = function(i, n) {
733
805
  var call = calls[i];
734
806
  chain = chain.then(function() {
735
- var method = instance == null ? void 0 : instance[call.method];
807
+ var target = instance;
808
+ if (call.target !== undefined) {
809
+ if (!Array.isArray(call.target) || call.target.some(function(key) {
810
+ return typeof key !== "string" || key.length === 0;
811
+ })) {
812
+ return Promise.reject(new Error('Call "' + call.method + '" target must be an array of non-empty strings'));
813
+ }
814
+ for(var _iterator = _create_for_of_iterator_helper_loose(call.target), _step; !(_step = _iterator()).done;){
815
+ var key = _step.value;
816
+ target = target == null ? void 0 : target[key];
817
+ }
818
+ }
819
+ var method = target == null ? void 0 : target[call.method];
736
820
  if (typeof method !== "function") {
737
- return Promise.reject(new Error('Call target does not have method "' + call.method + '"'));
821
+ var _call_target;
822
+ var path = ((_call_target = call.target) == null ? void 0 : _call_target.length) ? "" + call.target.join(".") + "." : "";
823
+ return Promise.reject(new Error('Call target does not have method "' + path + call.method + '"'));
738
824
  }
739
825
  var _call_args;
740
826
  return Promise.all(((_call_args = call.args) != null ? _call_args : []).map(function(arg) {
741
827
  return _this._resolveValue(arg);
742
828
  })).then(function(resolvedArgs) {
743
- return Promise.resolve(method.apply(instance, resolvedArgs));
829
+ return Promise.resolve(method.apply(target, resolvedArgs));
744
830
  }).then(function(result) {
745
831
  if (!call.result) return result;
746
832
  if (result == null || (typeof result === "undefined" ? "undefined" : _type_of(result)) !== "object" && typeof result !== "function") {
@@ -759,13 +845,13 @@ var ReflectionParser = /*#__PURE__*/ function() {
759
845
  });
760
846
  };
761
847
  /**
762
- * Apply props and calls from the same mutation block without imposing ordering between them.
848
+ * Apply props before executing calls from the same mutation block.
763
849
  */ _proto.parseMutationBlock = function parseMutationBlock(target, block) {
850
+ var _this = this;
764
851
  if (!block) return Promise.resolve(target);
765
- return Promise.all([
766
- this.parseProps(target, block.props),
767
- this.parseCalls(target, block.calls)
768
- ]).then(function() {
852
+ return this.parseProps(target, block.props).then(function() {
853
+ return _this.parseCalls(target, block.calls);
854
+ }).then(function() {
769
855
  return target;
770
856
  });
771
857
  };
@@ -776,12 +862,13 @@ var ReflectionParser = /*#__PURE__*/ function() {
776
862
  * 1. null/undefined/primitive → passthrough
777
863
  * 2. Array → recurse each element
778
864
  * 3. { $ref } → asset reference
779
- * 4. { $type } polymorphic type construct
780
- * 5. { $class } registered class constructor
781
- * 6. { $entity } entity reference by path (flat index + optional children descent)
782
- * 7. { $component } component reference
783
- * 8. { $signal } signal binding
784
- * 9. plain object recurse values (modify originValue in place if exists)
865
+ * 4. { $number } JSON-safe special number
866
+ * 5. { $type, $args? } polymorphic type construct
867
+ * 6. { $class } registered class constructor
868
+ * 7. { $entity } entity reference by path (flat index + optional children descent)
869
+ * 8. { $component } component reference
870
+ * 9. { $signal } signal binding
871
+ * 10. plain object → recurse values (modify originValue in place if exists)
785
872
  */ _proto._resolveValue = function _resolveValue(value, originValue) {
786
873
  var _this, _loop = function(key) {
787
874
  promises.push(_this._resolveValue(obj[key], target[key]).then(function(v) {
@@ -794,6 +881,9 @@ var ReflectionParser = /*#__PURE__*/ function() {
794
881
  return _this1._resolveValue(v);
795
882
  }));
796
883
  var obj = value;
884
+ if ("$args" in obj && !("$type" in obj)) {
885
+ return Promise.reject(new Error("$args requires $type"));
886
+ }
797
887
  // $ref — asset reference (index into refs array)
798
888
  if ("$ref" in obj) {
799
889
  var _this2 = this, context = _this2._context;
@@ -812,14 +902,29 @@ var ReflectionParser = /*#__PURE__*/ function() {
812
902
  return resource;
813
903
  });
814
904
  }
815
- // $type polymorphic type: construct instance and apply remaining props
905
+ if ("$number" in obj) {
906
+ if (Object.keys(obj).length !== 1 || obj.$number !== "Infinity") {
907
+ return Promise.reject(new Error('$number must be exactly "Infinity"'));
908
+ }
909
+ return Promise.resolve(Infinity);
910
+ }
911
+ // $type — polymorphic type: resolve constructor args, construct instance, then apply remaining props
816
912
  if ("$type" in obj) {
817
- var $type = obj.$type, rest = _object_without_properties_loose(obj, [
818
- "$type"
913
+ var $type = obj.$type, $args = obj.$args, rest = _object_without_properties_loose(obj, [
914
+ "$type",
915
+ "$args"
819
916
  ]);
917
+ if ($args !== undefined && !Array.isArray($args)) {
918
+ return Promise.reject(new Error("$args must be an array when used with $type"));
919
+ }
920
+ var constructorArgs = Array.isArray($args) ? $args : [];
820
921
  return this._resolveRegisteredClass($type, "$type").then(function(Class) {
821
- var instance = new Class();
822
- return Object.keys(rest).length > 0 ? _this1.parseProps(instance, rest) : instance;
922
+ return Promise.all(constructorArgs.map(function(arg) {
923
+ return _this1._resolveValue(arg);
924
+ })).then(function(args) {
925
+ var instance = _construct(Class, [].concat(args));
926
+ return Object.keys(rest).length > 0 ? _this1.parseProps(instance, rest) : instance;
927
+ });
823
928
  });
824
929
  }
825
930
  // $class — registered class constructor for factory-style methods.
@@ -1301,45 +1406,6 @@ function _instanceof(left, right) {
1301
1406
  return GLTFResource;
1302
1407
  }(engineCore.ReferResource);
1303
1408
 
1304
- function _array_like_to_array(arr, len) {
1305
- if (len == null || len > arr.length) len = arr.length;
1306
-
1307
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
1308
-
1309
- return arr2;
1310
- }
1311
-
1312
- function _unsupported_iterable_to_array(o, minLen) {
1313
- if (!o) return;
1314
- if (typeof o === "string") return _array_like_to_array(o, minLen);
1315
-
1316
- var n = Object.prototype.toString.call(o).slice(8, -1);
1317
-
1318
- if (n === "Object" && o.constructor) n = o.constructor.name;
1319
- if (n === "Map" || n === "Set") return Array.from(n);
1320
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
1321
- }
1322
-
1323
- function _create_for_of_iterator_helper_loose(o, allowArrayLike) {
1324
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
1325
-
1326
- if (it) return (it = it.call(o)).next.bind(it);
1327
- // Fallback for engines without symbol support
1328
- if (Array.isArray(o) || (it = _unsupported_iterable_to_array(o)) || allowArrayLike && o && typeof o.length === "number") {
1329
- if (it) o = it;
1330
-
1331
- var i = 0;
1332
-
1333
- return function() {
1334
- if (i >= o.length) return { done: true };
1335
-
1336
- return { done: false, value: o[i++] };
1337
- };
1338
- }
1339
-
1340
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
1341
- }
1342
-
1343
1409
  var DFDTransferFunction = /*#__PURE__*/ function(DFDTransferFunction) {
1344
1410
  DFDTransferFunction[DFDTransferFunction["linear"] = 1] = "linear";
1345
1411
  DFDTransferFunction[DFDTransferFunction["sRGB"] = 2] = "sRGB";
@@ -6461,8 +6527,7 @@ var ProjectLoader = /*#__PURE__*/ function(Loader) {
6461
6527
  ._request(item.url, _extends({}, item, {
6462
6528
  type: "json"
6463
6529
  })).then(function(data) {
6464
- // @ts-ignore
6465
- engine.resourceManager.initVirtualResources(data.files);
6530
+ engine.resourceManager.registerVirtualResources(data.files);
6466
6531
  return resourceManager.load({
6467
6532
  type: engineCore.AssetType.Scene,
6468
6533
  url: data.scene
@@ -6751,29 +6816,17 @@ var TextureLoader = /*#__PURE__*/ function(Loader) {
6751
6816
  };
6752
6817
  _proto._decodeImage = function _decodeImage(buffer, item, resourceManager) {
6753
6818
  var _this = this;
6754
- return new engineCore.AssetPromise(function(resolve, reject) {
6755
- var blob = new Blob([
6756
- buffer
6757
- ]);
6758
- var img = new Image();
6759
- img.onload = function() {
6760
- URL.revokeObjectURL(img.src);
6761
- var _item_params;
6762
- var _ref = (_item_params = item.params) != null ? _item_params : {}, _ref_format = _ref.format, format = _ref_format === void 0 ? engineCore.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;
6763
- var engine = resourceManager.engine;
6764
- var width = img.width, height = img.height;
6765
- var generateMipmap = engineCore.TextureUtils.supportGenerateMipmapsWithCorrection(engine, width, height, format, mipmap, isSRGBColorSpace);
6766
- var texture = new engineCore.Texture2D(engine, width, height, format, generateMipmap, isSRGBColorSpace);
6767
- texture.setImageSource(img);
6768
- generateMipmap && texture.generateMipmaps();
6769
- _this._applyParams(texture, item);
6770
- resolve(texture);
6771
- };
6772
- img.onerror = function(e) {
6773
- URL.revokeObjectURL(img.src);
6774
- reject(e);
6775
- };
6776
- img.src = URL.createObjectURL(blob);
6819
+ return decodeImage(buffer, item.url).then(function(img) {
6820
+ var _item_params;
6821
+ var _ref = (_item_params = item.params) != null ? _item_params : {}, _ref_format = _ref.format, format = _ref_format === void 0 ? engineCore.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;
6822
+ var engine = resourceManager.engine;
6823
+ var width = img.width, height = img.height;
6824
+ var generateMipmap = engineCore.TextureUtils.supportGenerateMipmapsWithCorrection(engine, width, height, format, mipmap, isSRGBColorSpace);
6825
+ var texture = new engineCore.Texture2D(engine, width, height, format, generateMipmap, isSRGBColorSpace);
6826
+ texture.setImageSource(img);
6827
+ generateMipmap && texture.generateMipmaps();
6828
+ _this._applyParams(texture, item);
6829
+ return texture;
6777
6830
  });
6778
6831
  };
6779
6832
  _proto._applyParams = function _applyParams(texture, item) {
@@ -6823,27 +6876,35 @@ var TextureContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
6823
6876
  texture.mipmapCount > 1 && texture.generateMipmaps();
6824
6877
  return texture;
6825
6878
  }
6826
- return new engineCore.AssetPromise(function(resolve, reject) {
6827
- var blob = new Blob([
6828
- buffer
6829
- ]);
6830
- var img = new Image();
6831
- img.onload = function() {
6832
- URL.revokeObjectURL(img.src);
6833
- texture.setImageSource(img);
6834
- texture.mipmapCount > 1 && texture.generateMipmaps();
6835
- resolve(texture);
6836
- };
6837
- img.onerror = function(e) {
6838
- URL.revokeObjectURL(img.src);
6839
- reject(e);
6840
- };
6841
- img.src = URL.createObjectURL(blob);
6879
+ return decodeImage(buffer, _this.url).then(function(img) {
6880
+ texture.setImageSource(img);
6881
+ texture.mipmapCount > 1 && texture.generateMipmaps();
6882
+ return texture;
6842
6883
  });
6843
6884
  });
6844
6885
  };
6845
6886
  return TextureContentRestorer;
6846
6887
  }(engineCore.ContentRestorer);
6888
+ function decodeImage(buffer, url) {
6889
+ return new engineCore.AssetPromise(function(resolve, reject) {
6890
+ var objectUrl = URL.createObjectURL(new Blob([
6891
+ buffer
6892
+ ]));
6893
+ var image = new Image();
6894
+ var releaseObjectUrl = function() {
6895
+ return URL.revokeObjectURL(objectUrl);
6896
+ };
6897
+ image.onload = function() {
6898
+ releaseObjectUrl();
6899
+ resolve(image);
6900
+ };
6901
+ image.onerror = function() {
6902
+ releaseObjectUrl();
6903
+ reject(new Error('TextureLoader: failed to decode texture "' + url + '" (' + buffer.byteLength + " bytes)."));
6904
+ };
6905
+ image.src = objectUrl;
6906
+ });
6907
+ }
6847
6908
 
6848
6909
  var AudioLoader = /*#__PURE__*/ function(Loader) {
6849
6910
  _inherits(AudioLoader, Loader);
@@ -6903,19 +6964,15 @@ var ShaderLoader = /*#__PURE__*/ function(Loader) {
6903
6964
  var _proto = ShaderLoader.prototype;
6904
6965
  _proto.load = function load(item, resourceManager) {
6905
6966
  var url = item.url;
6906
- if (url.endsWith(".shaderc")) {
6907
- // @ts-ignore
6908
- return resourceManager._request(url, _extends({}, item, {
6909
- type: "json"
6910
- })).then(function(data) {
6911
- // @ts-ignore - _createFromPrecompiled is @internal
6912
- return engineCore.Shader._createFromPrecompiled(data);
6913
- });
6914
- }
6915
- // @ts-ignore
6967
+ // @ts-expect-error _request is @internal
6916
6968
  return resourceManager._request(url, _extends({}, item, {
6917
6969
  type: "text"
6918
6970
  })).then(function(code) {
6971
+ var source = code.trimStart();
6972
+ if (source.startsWith("{")) {
6973
+ // @ts-expect-error _createFromPrecompiled is @internal
6974
+ return engineCore.Shader._createFromPrecompiled(JSON.parse(source));
6975
+ }
6919
6976
  return engineCore.Shader.create(code, undefined, url);
6920
6977
  });
6921
6978
  };