@galacean/engine-loader 2.0.0-alpha.29 → 2.0.0-alpha.31

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,37 +643,27 @@ 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();
646
+ function _object_without_properties_loose(source, excluded) {
647
+ if (source == null) return {};
669
648
 
670
- if (Class) _set_prototype_of(instance, Class.prototype);
649
+ var target = {};
650
+ var sourceKeys = Object.keys(source);
651
+ var key, i;
671
652
 
672
- return instance;
673
- };
653
+ for (i = 0; i < sourceKeys.length; i++) {
654
+ key = sourceKeys[i];
655
+ if (excluded.indexOf(key) >= 0) continue;
656
+ target[key] = source[key];
674
657
  }
675
658
 
676
- return _construct.apply(null, arguments);
659
+ return target;
660
+ }
661
+
662
+ function resolveRefItem(refs, index, owner, label) {
663
+ if (!Number.isInteger(index) || index < 0 || index >= refs.length) {
664
+ throw new Error(owner + ": invalid ref index " + index + " for " + label);
665
+ }
666
+ return refs[index];
677
667
  }
678
668
 
679
669
  var ParserType = /*#__PURE__*/ function(ParserType) {
@@ -689,32 +679,20 @@ var ParserType = /*#__PURE__*/ function(ParserType) {
689
679
  this.engine = engine;
690
680
  this.type = type;
691
681
  this.resource = resource;
692
- this.entityMap = new Map();
693
- this.entityConfigMap = new Map();
694
- this.components = new Map();
695
- this.componentConfigMap = new Map();
696
- this.rootIds = [];
697
- this.strippedIds = [];
698
- this._tasks = new Set();
682
+ this./** Runtime Entity instances, indexed by the flat entities[] position. */ entityInstances = [];
683
+ this./** Components waiting for props/calls application (Stage 4). */ pendingComponents = [];
699
684
  this._loaded = 0;
700
685
  this._total = 0;
701
686
  this.resourceManager = engine.resourceManager;
702
687
  }
703
688
  var _proto = ParserContext.prototype;
704
689
  _proto.clear = function clear() {
705
- this.entityMap.clear();
706
- this.components.clear();
707
- this.componentConfigMap.clear();
708
- this.entityConfigMap.clear();
709
- this.rootIds.length = 0;
710
- this.strippedIds.length = 0;
711
- };
712
- /** @internal */ _proto._addDependentAsset = function _addDependentAsset(url, promise) {
690
+ this.entityInstances.length = 0;
691
+ this.pendingComponents.length = 0;
692
+ };
693
+ /** @internal */ _proto._addDependentAsset = function _addDependentAsset(promise) {
713
694
  var _this = this;
714
- var tasks = this._tasks;
715
- if (tasks.has(url)) return;
716
695
  ++this._total;
717
- tasks.add(url);
718
696
  promise.finally(function() {
719
697
  ++_this._loaded;
720
698
  _this._setTaskCompleteProgress(_this._loaded, _this._total);
@@ -724,230 +702,214 @@ var ParserType = /*#__PURE__*/ function(ParserType) {
724
702
  }();
725
703
 
726
704
  var ReflectionParser = /*#__PURE__*/ function() {
727
- function ReflectionParser(_context) {
705
+ function ReflectionParser(_context, _refs) {
728
706
  this._context = _context;
707
+ this._refs = _refs;
729
708
  }
730
709
  var _proto = ReflectionParser.prototype;
731
- _proto.parseEntity = function parseEntity(entityConfig) {
732
- var _this = this;
733
- return this._getEntityByConfig(entityConfig).then(function(entity) {
734
- var _entityConfig_isActive;
735
- entity.isActive = (_entityConfig_isActive = entityConfig.isActive) != null ? _entityConfig_isActive : true;
736
- var transform = entity.transform;
737
- var transformConfig = entityConfig.transform;
738
- if (transformConfig) {
739
- _this.parsePropsAndMethods(transform, transformConfig);
740
- } else {
741
- var position = entityConfig.position, rotation = entityConfig.rotation, scale = entityConfig.scale;
742
- if (position) transform.position.copyFrom(position);
743
- if (rotation) transform.rotation.copyFrom(rotation);
744
- if (scale) transform.scale.copyFrom(scale);
745
- }
746
- var _entityConfig_layer;
747
- entity.layer = (_entityConfig_layer = entityConfig.layer) != null ? _entityConfig_layer : entity.layer;
748
- // @ts-ignore
749
- _this._context.type === ParserType.Prefab && entity._markAsTemplate(_this._context.resource);
750
- return entity;
751
- });
752
- };
753
- _proto.parseClassObject = function parseClassObject(item) {
754
- var _this = this;
755
- var Class = engineCore.Loader.getClass(item.class);
756
- var _item_constructParams;
757
- var params = (_item_constructParams = item.constructParams) != null ? _item_constructParams : [];
758
- return Promise.all(params.map(function(param) {
759
- return _this.parseBasicType(param);
760
- })).then(function(resultParams) {
761
- return _construct(Class, [].concat(resultParams));
762
- }).then(function(instance) {
763
- return _this.parsePropsAndMethods(instance, item);
764
- });
765
- };
766
- _proto.parsePropsAndMethods = function parsePropsAndMethods(instance, item) {
710
+ /**
711
+ * Apply v2 props to a component/object instance.
712
+ * Each prop value is resolved recursively (handling $ref, $type, $class, $entity, $component, $signal).
713
+ */ _proto.parseProps = function parseProps(instance, props) {
767
714
  var promises = [];
768
- if (item.methods) {
769
- for(var methodName in item.methods){
770
- var methodParams = item.methods[methodName];
771
- for(var i = 0, count = methodParams.length; i < count; i++){
772
- promises.push(this.parseMethod(instance, methodName, methodParams[i]));
773
- }
774
- }
775
- }
776
- if (item.props) {
715
+ if (props) {
777
716
  var _this, _loop = function(key) {
778
- var value = item.props[key];
779
- var promise = _this.parseBasicType(value, instance[key]).then(function(v) {
780
- return instance[key] = v;
717
+ var promise = _this._resolveValue(props[key], instance[key]).then(function(v) {
718
+ instance[key] = v;
781
719
  });
782
720
  promises.push(promise);
783
721
  };
784
- for(var key in item.props)_this = this, _loop(key);
722
+ for(var key in props)_this = this, _loop(key);
785
723
  }
786
724
  return Promise.all(promises).then(function() {
787
725
  return instance;
788
726
  });
789
727
  };
790
- _proto.parseMethod = function parseMethod(instance, methodName, methodParams) {
728
+ /**
729
+ * Execute calls sequentially on a target instance.
730
+ * Call args are resolved with the same v2 value rules as props.
731
+ */ _proto.parseCalls = function parseCalls(instance, calls) {
732
+ var _loop = function(i, n) {
733
+ var call = calls[i];
734
+ chain = chain.then(function() {
735
+ var method = instance == null ? void 0 : instance[call.method];
736
+ if (typeof method !== "function") {
737
+ return Promise.reject(new Error('Call target does not have method "' + call.method + '"'));
738
+ }
739
+ var _call_args;
740
+ return Promise.all(((_call_args = call.args) != null ? _call_args : []).map(function(arg) {
741
+ return _this._resolveValue(arg);
742
+ })).then(function(resolvedArgs) {
743
+ return Promise.resolve(method.apply(instance, resolvedArgs));
744
+ }).then(function(result) {
745
+ if (!call.result) return result;
746
+ if (result == null || (typeof result === "undefined" ? "undefined" : _type_of(result)) !== "object" && typeof result !== "function") {
747
+ return Promise.reject(new Error('Call "' + call.method + '" returned ' + result + " and cannot be mutated by result"));
748
+ }
749
+ return _this.parseMutationBlock(result, call.result);
750
+ });
751
+ });
752
+ };
791
753
  var _this = this;
792
- var isMethodObject = ReflectionParser._isMethodObject(methodParams);
793
- var params = isMethodObject ? methodParams.params : methodParams;
794
- return Promise.all(params.map(function(param) {
795
- return _this.parseBasicType(param);
796
- })).then(function(result) {
797
- var _instance;
798
- var methodResult = (_instance = instance)[methodName].apply(_instance, [].concat(result));
799
- if (isMethodObject && methodParams.result) {
800
- return _this.parsePropsAndMethods(methodResult, methodParams.result);
801
- } else {
802
- return methodResult;
803
- }
754
+ if (!(calls == null ? void 0 : calls.length)) return Promise.resolve(instance);
755
+ var chain = Promise.resolve();
756
+ for(var i = 0, n = calls.length; i < n; i++)_loop(i);
757
+ return chain.then(function() {
758
+ return instance;
804
759
  });
805
760
  };
806
- _proto.parseSignal = function parseSignal(signalRef) {
807
- var _this = this;
808
- var signal = new engineCore.Signal();
809
- return Promise.all(signalRef.listeners.map(function(listener) {
810
- return Promise.all([
811
- _this.parseBasicType(listener.target),
812
- listener.arguments ? Promise.all(listener.arguments.map(function(a) {
813
- return _this.parseBasicType(a);
814
- })) : Promise.resolve([])
815
- ]).then(function(param) {
816
- var target = param[0], resolvedArgs = param[1];
817
- if (target) {
818
- var _signal;
819
- (_signal = signal).on.apply(_signal, [].concat([
820
- target,
821
- listener.methodName
822
- ], resolvedArgs));
823
- }
824
- });
825
- })).then(function() {
826
- return signal;
761
+ /**
762
+ * Apply props and calls from the same mutation block without imposing ordering between them.
763
+ */ _proto.parseMutationBlock = function parseMutationBlock(target, block) {
764
+ 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() {
769
+ return target;
827
770
  });
828
771
  };
829
- _proto.parseBasicType = function parseBasicType(value, originValue) {
830
- var _this = this;
831
- if (Array.isArray(value)) {
832
- return Promise.all(value.map(function(item) {
833
- return _this.parseBasicType(item);
772
+ /**
773
+ * Resolve a v2 value with $ prefix detection.
774
+ *
775
+ * Priority:
776
+ * 1. null/undefined/primitive → passthrough
777
+ * 2. Array → recurse each element
778
+ * 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)
785
+ */ _proto._resolveValue = function _resolveValue(value, originValue) {
786
+ var _this, _loop = function(key) {
787
+ promises.push(_this._resolveValue(obj[key], target[key]).then(function(v) {
788
+ return target[key] = v;
834
789
  }));
835
- } else if ((typeof value === "undefined" ? "undefined" : _type_of(value)) === "object" && value != null) {
836
- if (ReflectionParser._isClassType(value)) {
837
- return Promise.resolve(engineCore.Loader.getClass(value["classType"]));
838
- } else if (ReflectionParser._isClass(value)) {
839
- // class object
840
- return this.parseClassObject(value);
841
- } else if (ReflectionParser._isAssetRef(value)) {
842
- var _this1 = this, context = _this1._context;
843
- // reference object
844
- // @ts-ignore
845
- return context.resourceManager.getResourceByRef(value).then(function(resource) {
846
- if (resource && context.type === ParserType.Prefab) {
847
- // @ts-ignore
848
- context.resource._addDependenceAsset(resource);
849
- }
850
- return resource;
851
- });
852
- } else if (ReflectionParser._isComponentRef(value)) {
853
- var entity = this._resolveEntityByPath(value.entityPath);
854
- if (!entity) return Promise.resolve(null);
855
- var type = engineCore.Loader.getClass(value.componentType);
856
- if (!type) return Promise.resolve(null);
857
- var _entity_getComponents_value_componentIndex;
858
- return Promise.resolve((_entity_getComponents_value_componentIndex = entity.getComponents(type, [])[value.componentIndex]) != null ? _entity_getComponents_value_componentIndex : null);
859
- } else if (ReflectionParser._isEntityRef(value)) {
860
- return Promise.resolve(this._resolveEntityByPath(value.entityPath));
861
- } else if (ReflectionParser._isSignalRef(value)) {
862
- return this.parseSignal(value);
863
- } else if (originValue) {
864
- var _this2, _loop = function(key) {
865
- if (key === "methods") {
866
- var methods = value[key];
867
- for(var methodName in methods){
868
- var methodParams = methods[methodName];
869
- for(var i = 0, count = methodParams.length; i < count; i++){
870
- var params = methodParams[i];
871
- var promise = _this2.parseMethod(originValue, methodName, params);
872
- promises.push(promise);
873
- }
874
- }
875
- } else {
876
- promises.push(_this2.parseBasicType(value[key], originValue[key]).then(function(v) {
877
- return originValue[key] = v;
878
- }));
879
- }
880
- };
881
- var promises = [];
882
- for(var key in value)_this2 = this, _loop(key);
883
- return Promise.all(promises).then(function() {
884
- return originValue;
885
- });
790
+ };
791
+ var _this1 = this;
792
+ if (value == null || (typeof value === "undefined" ? "undefined" : _type_of(value)) !== "object") return Promise.resolve(value);
793
+ if (Array.isArray(value)) return Promise.all(value.map(function(v) {
794
+ return _this1._resolveValue(v);
795
+ }));
796
+ var obj = value;
797
+ // $ref asset reference (index into refs array)
798
+ if ("$ref" in obj) {
799
+ var _this2 = this, context = _this2._context;
800
+ var refItem;
801
+ try {
802
+ refItem = resolveRefItem(this._refs, obj.$ref, "ReflectionParser", "$ref");
803
+ } catch (error) {
804
+ return Promise.reject(error);
886
805
  }
887
- }
888
- // primitive type
889
- return Promise.resolve(value);
890
- };
891
- _proto._getEntityByConfig = function _getEntityByConfig(entityConfig) {
892
- var _this = this;
893
- // @ts-ignore
894
- var assetUrl = entityConfig.assetUrl;
895
- var engine = this._context.engine;
896
- if (assetUrl) {
897
- return engine.resourceManager// @ts-ignore
898
- .getResourceByRef({
899
- url: assetUrl,
900
- key: entityConfig.key,
901
- isClone: entityConfig.isClone
902
- }).then(function(entity) {
903
- // @ts-ignore
904
- var resource = engine.resourceManager._objectPool[assetUrl];
905
- if (resource && _this._context.type === ParserType.Prefab) {
806
+ // @ts-ignore
807
+ return context.resourceManager.getResourceByRef(refItem).then(function(resource) {
808
+ if (resource && context.type === ParserType.Prefab) {
906
809
  // @ts-ignore
907
- _this._context.resource._addDependenceAsset(resource);
810
+ context.resource._addDependenceAsset(resource);
908
811
  }
909
- entity.name = entityConfig.name;
910
- return entity;
812
+ return resource;
911
813
  });
912
- } else {
913
- var transform = entityConfig.transform;
914
- var entity = new engineCore.Entity(engine, entityConfig.name, transform ? engineCore.Loader.getClass(transform.class) : engineCore.Transform);
915
- return Promise.resolve(entity);
916
814
  }
917
- };
918
- _proto._resolveEntityByPath = function _resolveEntityByPath(entityPath) {
919
- var _this__context = this._context, rootIds = _this__context.rootIds, entityMap = _this__context.entityMap;
920
- if (!entityPath.length || entityPath[0] >= rootIds.length) return null;
921
- var entity = entityMap.get(rootIds[entityPath[0]]);
922
- for(var i = 1; i < entityPath.length; i++){
923
- if (!entity || entityPath[i] >= entity.children.length) return null;
924
- entity = entity.children[entityPath[i]];
815
+ // $type — polymorphic type: construct instance and apply remaining props
816
+ if ("$type" in obj) {
817
+ var $type = obj.$type, rest = _object_without_properties_loose(obj, [
818
+ "$type"
819
+ ]);
820
+ 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;
823
+ });
925
824
  }
926
- return entity;
927
- };
928
- ReflectionParser._isClass = function _isClass(value) {
929
- return value["class"] !== undefined;
930
- };
931
- ReflectionParser._isClassType = function _isClassType(value) {
932
- return value["classType"] !== undefined;
825
+ // $class — registered class constructor for factory-style methods.
826
+ if ("$class" in obj) {
827
+ return this._resolveRegisteredClass(obj.$class, "$class");
828
+ }
829
+ // $entity — entity reference by path (first element = flat index, subsequent = children indices)
830
+ if ("$entity" in obj) {
831
+ return Promise.resolve(this._resolveEntityRef(obj.$entity));
832
+ }
833
+ // $component — component reference: { entity, type, index }
834
+ if ("$component" in obj) {
835
+ return Promise.resolve(this._resolveComponent(obj.$component));
836
+ }
837
+ // $signal — signal binding: register listeners on the existing Signal instance
838
+ if ("$signal" in obj) {
839
+ return this._resolveSignal(originValue, obj.$signal);
840
+ }
841
+ // Plain object — recurse each value, modifying originValue in place or building a new object
842
+ var target = originValue && (typeof originValue === "undefined" ? "undefined" : _type_of(originValue)) === "object" && !Array.isArray(originValue) ? originValue : {};
843
+ var promises = [];
844
+ for(var key in obj)_this = this, _loop(key);
845
+ return Promise.all(promises).then(function() {
846
+ return target;
847
+ });
933
848
  };
934
- ReflectionParser._isAssetRef = function _isAssetRef(value) {
935
- return value["url"] !== undefined;
849
+ _proto._getRegisteredClass = function _getRegisteredClass(value, sentinel) {
850
+ if (typeof value !== "string" || value.length === 0) {
851
+ throw new Error("" + sentinel + " must be a non-empty registered class name string");
852
+ }
853
+ var Class = engineCore.Loader.getClass(value);
854
+ if (!Class) throw new Error('Loader.getClass: class "' + value + '" is not registered');
855
+ return Class;
936
856
  };
937
- ReflectionParser._isEntityRef = function _isEntityRef(value) {
938
- return Array.isArray(value["entityPath"]) && value["componentType"] === undefined;
857
+ /** Promise-adapt {@link _getRegisteredClass} so $type/$class branches can fold into the resolver chain. */ _proto._resolveRegisteredClass = function _resolveRegisteredClass(value, sentinel) {
858
+ try {
859
+ return Promise.resolve(this._getRegisteredClass(value, sentinel));
860
+ } catch (error) {
861
+ return Promise.reject(error);
862
+ }
939
863
  };
940
- ReflectionParser._isComponentRef = function _isComponentRef(value) {
941
- return Array.isArray(value["entityPath"]) && value["componentType"] !== undefined;
864
+ _proto._resolveSignal = function _resolveSignal(signal, listeners) {
865
+ var _this = this;
866
+ if (!signal || typeof signal.on !== "function") {
867
+ return Promise.reject(new Error("$signal requires a pre-initialized Signal instance on the target property"));
868
+ }
869
+ var promises = listeners.map(function(listener) {
870
+ var targetComponent = _this._resolveComponent(listener.target.$component);
871
+ if (!targetComponent) return Promise.resolve();
872
+ var _listener_args;
873
+ return Promise.all(((_listener_args = listener.args) != null ? _listener_args : []).map(function(a) {
874
+ return _this._resolveValue(a);
875
+ })).then(function(resolvedArgs) {
876
+ var _signal;
877
+ (_signal = signal).on.apply(_signal, [].concat([
878
+ targetComponent,
879
+ listener.methodName
880
+ ], resolvedArgs));
881
+ });
882
+ });
883
+ return Promise.all(promises).then(function() {
884
+ return signal;
885
+ });
942
886
  };
943
- ReflectionParser._isSignalRef = function _isSignalRef(value) {
944
- return value["listeners"] !== undefined;
887
+ _proto._resolveComponent = function _resolveComponent(comp) {
888
+ var entity = this._resolveEntityRef(comp.entity);
889
+ if (!entity) return null;
890
+ var type = engineCore.Loader.getClass(comp.type);
891
+ if (!type) return null;
892
+ var buffer = ReflectionParser._componentBuffer;
893
+ buffer.length = 0;
894
+ entity.getComponents(type, buffer);
895
+ var _buffer_comp_index;
896
+ var result = (_buffer_comp_index = buffer[comp.index]) != null ? _buffer_comp_index : null;
897
+ buffer.length = 0;
898
+ return result;
945
899
  };
946
- ReflectionParser._isMethodObject = function _isMethodObject(value) {
947
- return Array.isArray(value == null ? void 0 : value.params);
900
+ _proto._resolveEntityRef = function _resolveEntityRef(path) {
901
+ if (!path || path.length === 0) return null;
902
+ var _this__context_entityInstances_path_;
903
+ var entity = (_this__context_entityInstances_path_ = this._context.entityInstances[path[0]]) != null ? _this__context_entityInstances_path_ : null;
904
+ for(var i = 1, n = path.length; entity && i < n; i++){
905
+ var _entity_children_path_i;
906
+ entity = (_entity_children_path_i = entity.children[path[i]]) != null ? _entity_children_path_i : null;
907
+ }
908
+ return entity;
948
909
  };
949
910
  return ReflectionParser;
950
911
  }();
912
+ /** @internal shared with HierarchyParser; each use must length=0 -> getComponents -> read -> length=0 synchronously. */ ReflectionParser._componentBuffer = [];
951
913
 
952
914
  /**
953
915
  * HDR (Radiance RGBE) image decoder.
@@ -1249,45 +1211,6 @@ function _instanceof(left, right) {
1249
1211
  } else return left instanceof right;
1250
1212
  }
1251
1213
 
1252
- function _array_like_to_array(arr, len) {
1253
- if (len == null || len > arr.length) len = arr.length;
1254
-
1255
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
1256
-
1257
- return arr2;
1258
- }
1259
-
1260
- function _unsupported_iterable_to_array(o, minLen) {
1261
- if (!o) return;
1262
- if (typeof o === "string") return _array_like_to_array(o, minLen);
1263
-
1264
- var n = Object.prototype.toString.call(o).slice(8, -1);
1265
-
1266
- if (n === "Object" && o.constructor) n = o.constructor.name;
1267
- if (n === "Map" || n === "Set") return Array.from(n);
1268
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
1269
- }
1270
-
1271
- function _create_for_of_iterator_helper_loose(o, allowArrayLike) {
1272
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
1273
-
1274
- if (it) return (it = it.call(o)).next.bind(it);
1275
- // Fallback for engines without symbol support
1276
- if (Array.isArray(o) || (it = _unsupported_iterable_to_array(o)) || allowArrayLike && o && typeof o.length === "number") {
1277
- if (it) o = it;
1278
-
1279
- var i = 0;
1280
-
1281
- return function() {
1282
- if (i >= o.length) return { done: true };
1283
-
1284
- return { done: false, value: o[i++] };
1285
- };
1286
- }
1287
-
1288
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
1289
- }
1290
-
1291
1214
  /**
1292
1215
  * The Prefab resource.
1293
1216
  */ var PrefabResource = /*#__PURE__*/ function(ReferResource1) {
@@ -1319,8 +1242,9 @@ function _create_for_of_iterator_helper_loose(o, allowArrayLike) {
1319
1242
  };
1320
1243
  _proto._onDestroy = function _onDestroy() {
1321
1244
  var _this = this;
1245
+ var _this__root;
1322
1246
  ReferResource1.prototype._onDestroy.call(this);
1323
- this._root.destroy();
1247
+ (_this__root = this._root) == null ? void 0 : _this__root.destroy();
1324
1248
  this._dependenceAssets.forEach(function(asset) {
1325
1249
  if (_instanceof(asset, engineCore.ReferResource)) {
1326
1250
  // @ts-ignore
@@ -1336,319 +1260,260 @@ function _create_for_of_iterator_helper_loose(o, allowArrayLike) {
1336
1260
  var _this = this;
1337
1261
  this.data = data;
1338
1262
  this.context = context;
1339
- this._prefabContextMap = new WeakMap();
1340
- this._prefabPromiseMap = new Map();
1263
+ if (data.version !== "2.0") {
1264
+ var resourceType = context.type === ParserType.Scene ? "scene" : "prefab";
1265
+ throw new Error("Unsupported " + resourceType + ' format version "' + data.version + '". Expected "2.0".');
1266
+ }
1341
1267
  this._engine = this.context.engine;
1342
- this._organizeEntities = this._organizeEntities.bind(this);
1343
- this._parseComponents = this._parseComponents.bind(this);
1344
- this._parsePrefabModification = this._parsePrefabModification.bind(this);
1345
- this._parseAddedComponents = this._parseAddedComponents.bind(this);
1346
- this._parseComponentsPropsAndMethods = this._parseComponentsPropsAndMethods.bind(this);
1347
- this._parsePrefabRemovedEntities = this._parsePrefabRemovedEntities.bind(this);
1348
- this._parsePrefabRemovedComponents = this._parsePrefabRemovedComponents.bind(this);
1349
- this._clearAndResolve = this._clearAndResolve.bind(this);
1350
1268
  this.promise = new Promise(function(resolve, reject) {
1351
1269
  _this._reject = reject;
1352
1270
  _this._resolve = resolve;
1353
1271
  });
1354
- this._reflectionParser = new ReflectionParser(context);
1272
+ this._reflectionParser = new ReflectionParser(context, data.refs);
1355
1273
  }
1356
1274
  var _proto = HierarchyParser.prototype;
1357
- /** start parse the scene or prefab or others */ _proto.start = function start() {
1358
- this._parseEntities().then(this._organizeEntities).then(this._parseComponents).then(this._parseAddedComponents).then(this._parseComponentsPropsAndMethods).then(this._parsePrefabModification).then(this._parsePrefabRemovedEntities).then(this._parsePrefabRemovedComponents).then(this._clearAndResolve).then(this._resolve).catch(this._reject);
1359
- };
1360
- _proto._applyEntityData = function _applyEntityData(entity, entityConfig) {
1361
- if (entityConfig === void 0) entityConfig = {};
1362
- var _entityConfig_isActive;
1363
- entity.isActive = (_entityConfig_isActive = entityConfig.isActive) != null ? _entityConfig_isActive : entity.isActive;
1364
- var _entityConfig_name;
1365
- entity.name = (_entityConfig_name = entityConfig.name) != null ? _entityConfig_name : entity.name;
1366
- var transform = entity.transform;
1367
- var transformConfig = entityConfig.transform;
1368
- if (transformConfig) {
1369
- this._reflectionParser.parsePropsAndMethods(transform, transformConfig);
1370
- } else {
1371
- var position = entityConfig.position, rotation = entityConfig.rotation, scale = entityConfig.scale;
1372
- if (position) transform.position.copyFrom(position);
1373
- if (rotation) transform.rotation.copyFrom(rotation);
1374
- if (scale) transform.scale.copyFrom(scale);
1375
- }
1376
- if (entityConfig.layer) entity.layer = entityConfig.layer;
1377
- return entity;
1378
- };
1379
- _proto._parseEntities = function _parseEntities() {
1275
+ _proto.start = function start() {
1380
1276
  var _this = this;
1381
- var entitiesConfig = this.data.entities;
1382
- var entityConfigMap = this.context.entityConfigMap;
1383
- var entityMap = this.context.entityMap;
1384
- var engine = this._engine;
1385
- var promises = entitiesConfig.map(function(entityConfig) {
1386
- var _entityConfig_strippedId;
1387
- var id = (_entityConfig_strippedId = entityConfig.strippedId) != null ? _entityConfig_strippedId : entityConfig.id;
1388
- entityConfig.id = id;
1389
- entityConfigMap.set(id, entityConfig);
1390
- return _this._getEntityByConfig(entityConfig, engine);
1391
- });
1392
- return Promise.all(promises).then(function(entities) {
1393
- for(var i = 0, l = entities.length; i < l; i++){
1394
- entityMap.set(entitiesConfig[i].id, entities[i]);
1395
- }
1396
- // Build rootIds in serialization order (not async completion order)
1397
- var rootIds = _this.context.rootIds;
1398
- for(var i1 = 0, l1 = entitiesConfig.length; i1 < l1; i1++){
1399
- if (!entitiesConfig[i1].parent && !entitiesConfig[i1].strippedId) {
1400
- rootIds.push(entitiesConfig[i1].id);
1401
- }
1402
- }
1403
- return entities;
1404
- });
1405
- };
1406
- _proto._parseComponents = function _parseComponents() {
1407
- var entitiesConfig = this.data.entities;
1408
- var entityMap = this.context.entityMap;
1409
- for(var i = 0, l = entitiesConfig.length; i < l; i++){
1410
- var entityConfig = entitiesConfig[i];
1411
- if (entityConfig.strippedId) {
1412
- continue;
1413
- }
1414
- var entity = entityMap.get(entityConfig.id);
1415
- this._addComponents(entity, entityConfig.components);
1416
- }
1417
- };
1418
- _proto._parsePrefabModification = function _parsePrefabModification() {
1419
- var _loop = function(i, l) {
1420
- var entityConfig = entitiesConfig[i];
1421
- var id = entityConfig.id, modifications = entityConfig.modifications;
1422
- if (modifications == null ? void 0 : modifications.length) {
1423
- var _promises;
1424
- var rootEntity = entityMap.get(id);
1425
- (_promises = promises).push.apply(_promises, [].concat(modifications.map(function(modification) {
1426
- var target = modification.target, props = modification.props, methods = modification.methods;
1427
- var entityId = target.entityId, componentId = target.componentId;
1428
- var context = _this._prefabContextMap.get(rootEntity);
1429
- var targetEntity = context.entityMap.get(entityId);
1430
- var targetComponent = context.components.get(componentId);
1431
- if (targetComponent) {
1432
- return _this._reflectionParser.parsePropsAndMethods(targetComponent, {
1433
- props: props,
1434
- methods: methods
1435
- });
1436
- } else if (targetEntity) {
1437
- return Promise.resolve(_this._applyEntityData(targetEntity, props));
1438
- }
1439
- })));
1277
+ this._parseEntities().then(function() {
1278
+ return _this._organizeEntities();
1279
+ }).then(function() {
1280
+ return _this._parseComponents();
1281
+ }).then(function() {
1282
+ return _this._parseComponentsPropsAndCalls();
1283
+ }).then(function() {
1284
+ return _this._parsePrefabOverrides();
1285
+ }).then(function() {
1286
+ return _this._clearAndResolve();
1287
+ }).then(this._resolve).catch(this._reject);
1288
+ };
1289
+ _proto._onEntityCreated = function _onEntityCreated(_entity) {};
1290
+ // ---------------------------------------------------------------------------
1291
+ // Stage 1: Create entity instances
1292
+ // ---------------------------------------------------------------------------
1293
+ _proto._parseEntities = function _parseEntities() {
1294
+ var _this, _loop = function(i, n) {
1295
+ var entityConfig = entities[i];
1296
+ if (HierarchyParser._isPrefabInstanceEntity(entityConfig)) {
1297
+ promises.push(_this._loadPrefabInstance(entityConfig, engine).then(function(entity) {
1298
+ entityInstances[i] = entity;
1299
+ }));
1300
+ } else {
1301
+ var entity = new engineCore.Entity(engine, entityConfig.name);
1302
+ HierarchyParser._applyEntityProps(entity, entityConfig);
1303
+ _this._onEntityCreated(entity);
1304
+ entityInstances[i] = entity;
1440
1305
  }
1441
1306
  };
1442
- var _this = this;
1443
- var entitiesConfig = this.data.entities;
1444
- var entityMap = this.context.entityMap;
1307
+ var entities = this.data.entities;
1308
+ var entityInstances = this.context.entityInstances;
1309
+ var engine = this._engine;
1445
1310
  var promises = [];
1446
- for(var i = 0, l = entitiesConfig.length; i < l; i++)_loop(i);
1311
+ for(var i = 0, n = entities.length; i < n; i++)_this = this, _loop(i);
1447
1312
  return Promise.all(promises);
1448
1313
  };
1449
- _proto._parseAddedComponents = function _parseAddedComponents() {
1450
- var entityMap = this.context.entityMap;
1451
- var entityConfigMap = this.context.entityConfigMap;
1452
- var strippedIds = this.context.strippedIds;
1453
- for(var i = 0, n = strippedIds.length; i < n; i++){
1454
- var entityConfig = entityConfigMap.get(strippedIds[i]);
1455
- var prefabContext = this._prefabContextMap.get(entityMap.get(entityConfig.prefabInstanceId));
1456
- var entity = prefabContext.entityMap.get(entityConfig.prefabSource.entityId);
1457
- this._addComponents(entity, entityConfig.components);
1458
- }
1459
- };
1460
- _proto._parsePrefabRemovedEntities = function _parsePrefabRemovedEntities() {
1461
- var entitiesConfig = this.data.entities;
1462
- var entityMap = this.context.entityMap;
1463
- for(var i = 0, l = entitiesConfig.length; i < l; i++){
1464
- var entityConfig = entitiesConfig[i];
1465
- var id = entityConfig.id, removedEntities = entityConfig.removedEntities;
1466
- if (removedEntities == null ? void 0 : removedEntities.length) {
1467
- var rootEntity = entityMap.get(id);
1468
- for(var j = 0, m = removedEntities.length; j < m; j++){
1469
- var target = removedEntities[j];
1470
- var entityId = target.entityId;
1471
- var context = this._prefabContextMap.get(rootEntity);
1472
- var targetEntity = context.entityMap.get(entityId);
1473
- if (targetEntity) {
1474
- targetEntity.destroy();
1475
- }
1476
- }
1314
+ // ---------------------------------------------------------------------------
1315
+ // Stage 2: Build parent-child hierarchy
1316
+ // ---------------------------------------------------------------------------
1317
+ _proto._organizeEntities = function _organizeEntities() {
1318
+ var entities = this.data.entities;
1319
+ var entityInstances = this.context.entityInstances;
1320
+ for(var i = 0, n = entities.length; i < n; i++){
1321
+ var entityConfig = entities[i];
1322
+ // Prefab instance entities manage their own children.
1323
+ if (HierarchyParser._isPrefabInstanceEntity(entityConfig)) continue;
1324
+ var children = entityConfig.children;
1325
+ if (!children) continue;
1326
+ var parent = entityInstances[i];
1327
+ for(var j = 0, m = children.length; j < m; j++){
1328
+ parent.addChild(entityInstances[children[j]]);
1477
1329
  }
1478
1330
  }
1331
+ var rootIndices = this._getRootIndices();
1332
+ for(var i1 = 0, n1 = rootIndices.length; i1 < n1; i1++){
1333
+ this._handleRootEntity(rootIndices[i1]);
1334
+ }
1479
1335
  };
1480
- _proto._parsePrefabRemovedComponents = function _parsePrefabRemovedComponents() {
1481
- var entitiesConfig = this.data.entities;
1482
- var entityMap = this.context.entityMap;
1483
- var prefabContextMap = this._prefabContextMap;
1484
- for(var i = 0, l = entitiesConfig.length; i < l; i++){
1485
- var entityConfig = entitiesConfig[i];
1486
- var id = entityConfig.id, removedComponents = entityConfig.removedComponents;
1487
- if (removedComponents == null ? void 0 : removedComponents.length) {
1488
- var rootEntity = entityMap.get(id);
1489
- for(var j = 0, m = removedComponents.length; j < m; j++){
1490
- var target = removedComponents[j];
1491
- var componentId = target.componentId;
1492
- var context = prefabContextMap.get(rootEntity);
1493
- var targetComponent = context.components.get(componentId);
1494
- if (targetComponent) {
1495
- targetComponent.destroy();
1496
- }
1497
- }
1336
+ // ---------------------------------------------------------------------------
1337
+ // Stage 3: Add components to entities
1338
+ // ---------------------------------------------------------------------------
1339
+ _proto._parseComponents = function _parseComponents() {
1340
+ var entities = this.data.entities;
1341
+ var allComponents = this.data.components;
1342
+ var entityInstances = this.context.entityInstances;
1343
+ var pendingComponents = this.context.pendingComponents;
1344
+ var refs = this.data.refs;
1345
+ for(var i = 0, n = entities.length; i < n; i++){
1346
+ var entityConfig = entities[i];
1347
+ if (HierarchyParser._isPrefabInstanceEntity(entityConfig)) continue;
1348
+ var entity = entityInstances[i];
1349
+ var componentIndices = entityConfig.components;
1350
+ if (!componentIndices) continue;
1351
+ for(var j = 0, m = componentIndices.length; j < m; j++){
1352
+ var config = allComponents[componentIndices[j]];
1353
+ var instance = HierarchyParser._addComponentFromConfig(entity, config, refs);
1354
+ pendingComponents.push({
1355
+ instance: instance,
1356
+ config: config
1357
+ });
1498
1358
  }
1499
1359
  }
1500
1360
  };
1501
- _proto._organizeEntities = function _organizeEntities() {
1502
- var _this_context = this.context, rootIds = _this_context.rootIds, strippedIds = _this_context.strippedIds;
1503
- var parentIds = rootIds.concat(strippedIds);
1504
- for(var i = 0, l = parentIds.length; i < l; i++){
1505
- this._parseChildren(parentIds[i]);
1506
- }
1507
- for(var i1 = 0; i1 < rootIds.length; i1++){
1508
- this._handleRootEntity(rootIds[i1]);
1361
+ // ---------------------------------------------------------------------------
1362
+ // Stage 4: Apply props and execute calls on components
1363
+ // ---------------------------------------------------------------------------
1364
+ _proto._parseComponentsPropsAndCalls = function _parseComponentsPropsAndCalls() {
1365
+ var pendingComponents = this.context.pendingComponents;
1366
+ var reflectionParser = this._reflectionParser;
1367
+ var promises = [];
1368
+ for(var i = 0, n = pendingComponents.length; i < n; i++){
1369
+ var _pendingComponents_i = pendingComponents[i], instance = _pendingComponents_i.instance, config = _pendingComponents_i.config;
1370
+ promises.push(reflectionParser.parseMutationBlock(instance, config));
1509
1371
  }
1372
+ return Promise.all(promises);
1510
1373
  };
1511
- _proto._getEntityByConfig = function _getEntityByConfig(entityConfig, engine) {
1512
- var _this = this;
1513
- var entityPromise;
1514
- if (entityConfig.assetUrl) {
1515
- entityPromise = this._parsePrefab(entityConfig, engine);
1516
- } else if (entityConfig.strippedId) {
1517
- entityPromise = this._parseStrippedEntity(entityConfig);
1518
- } else {
1519
- entityPromise = this._parseEntity(entityConfig, engine);
1374
+ // ---------------------------------------------------------------------------
1375
+ // Stage 5: Apply prefab instance overrides
1376
+ // ---------------------------------------------------------------------------
1377
+ _proto._parsePrefabOverrides = function _parsePrefabOverrides() {
1378
+ var entities = this.data.entities;
1379
+ var entityInstances = this.context.entityInstances;
1380
+ var promises = [];
1381
+ for(var i = 0, n = entities.length; i < n; i++){
1382
+ var entityConfig = entities[i];
1383
+ if (!HierarchyParser._isPrefabInstanceEntity(entityConfig)) continue;
1384
+ var overrides = entityConfig.instance.overrides;
1385
+ if (!overrides) continue;
1386
+ this._applyOverrides(entityInstances[i], overrides, promises);
1520
1387
  }
1521
- return entityPromise.then(function(entity) {
1522
- return _this._applyEntityData(entity, entityConfig);
1523
- });
1388
+ return Promise.all(promises);
1524
1389
  };
1525
- _proto._parseEntity = function _parseEntity(entityConfig, engine) {
1526
- var transform = entityConfig.transform;
1527
- var entity = new engineCore.Entity(engine, entityConfig.name, transform ? engineCore.Loader.getClass(transform.class) : engineCore.Transform);
1528
- this._addEntityPlugin(entityConfig.id, entity);
1529
- return Promise.resolve(entity);
1390
+ _proto._applyOverrides = function _applyOverrides(rootEntity, overrides, promises) {
1391
+ var refs = this.data.refs;
1392
+ var reflectionParser = this._reflectionParser;
1393
+ // entityProps — entity-level property overrides
1394
+ if (overrides.entityProps) {
1395
+ for(var j = 0, m = overrides.entityProps.length; j < m; j++){
1396
+ var override = overrides.entityProps[j];
1397
+ HierarchyParser._applyEntityProps(HierarchyParser._resolveEntity(rootEntity, override.path), override);
1398
+ }
1399
+ }
1400
+ // componentProps — component-level property overrides
1401
+ if (overrides.componentProps) {
1402
+ for(var j1 = 0, m1 = overrides.componentProps.length; j1 < m1; j1++){
1403
+ var override1 = overrides.componentProps[j1];
1404
+ var entity = HierarchyParser._resolveEntity(rootEntity, override1.path);
1405
+ var target = HierarchyParser._resolveComponent(entity, override1.selector);
1406
+ promises.push(reflectionParser.parseMutationBlock(target, override1));
1407
+ }
1408
+ }
1409
+ // addedComponents — attach top-level components[index] to a prefab entity and parse props
1410
+ if (overrides.addedComponents) {
1411
+ var allComponents = this.data.components;
1412
+ for(var j2 = 0, m2 = overrides.addedComponents.length; j2 < m2; j2++){
1413
+ var added = overrides.addedComponents[j2];
1414
+ var entity1 = HierarchyParser._resolveEntity(rootEntity, added.target);
1415
+ var config = allComponents[added.component];
1416
+ var component = HierarchyParser._addComponentFromConfig(entity1, config, refs);
1417
+ promises.push(reflectionParser.parseMutationBlock(component, config));
1418
+ }
1419
+ }
1420
+ // addedEntities — attach already-created top-level entityInstances[index] as a child
1421
+ if (overrides.addedEntities) {
1422
+ var entityInstances = this.context.entityInstances;
1423
+ for(var j3 = 0, m3 = overrides.addedEntities.length; j3 < m3; j3++){
1424
+ var added1 = overrides.addedEntities[j3];
1425
+ var parent = HierarchyParser._resolveEntity(rootEntity, added1.parent);
1426
+ parent.addChild(entityInstances[added1.entity]);
1427
+ }
1428
+ }
1429
+ // removedEntities — pre-resolve all targets then destroy (destroy shifts sibling indices)
1430
+ if (overrides.removedEntities) {
1431
+ var removed = overrides.removedEntities;
1432
+ var targets = new Array(removed.length);
1433
+ for(var j4 = 0, m4 = removed.length; j4 < m4; j4++){
1434
+ targets[j4] = HierarchyParser._resolveEntity(rootEntity, removed[j4]);
1435
+ }
1436
+ for(var j5 = 0, m5 = targets.length; j5 < m5; j5++){
1437
+ targets[j5].destroy();
1438
+ }
1439
+ }
1440
+ // removedComponents — pre-resolve all targets then destroy (destroy shifts component indices)
1441
+ if (overrides.removedComponents) {
1442
+ var targets1 = [];
1443
+ for(var j6 = 0, m6 = overrides.removedComponents.length; j6 < m6; j6++){
1444
+ var override2 = overrides.removedComponents[j6];
1445
+ var entity2 = HierarchyParser._resolveEntity(rootEntity, override2.path);
1446
+ var selectors = override2.selectors;
1447
+ for(var k = 0, p = selectors.length; k < p; k++){
1448
+ targets1.push(HierarchyParser._resolveComponent(entity2, selectors[k]));
1449
+ }
1450
+ }
1451
+ for(var j7 = 0, m7 = targets1.length; j7 < m7; j7++){
1452
+ targets1[j7].destroy();
1453
+ }
1454
+ }
1530
1455
  };
1531
- _proto._parsePrefab = function _parsePrefab(entityConfig, engine) {
1456
+ // ---------------------------------------------------------------------------
1457
+ // Prefab instance loading
1458
+ // ---------------------------------------------------------------------------
1459
+ _proto._loadPrefabInstance = function _loadPrefabInstance(entityConfig, engine) {
1532
1460
  var _this = this;
1533
- var assetUrl = entityConfig.assetUrl;
1461
+ var instance = entityConfig.instance;
1462
+ var refItem;
1463
+ try {
1464
+ refItem = resolveRefItem(this.data.refs, instance.asset, "HierarchyParser", "instance.asset");
1465
+ } catch (error) {
1466
+ return Promise.reject(error);
1467
+ }
1534
1468
  return engine.resourceManager// @ts-ignore
1535
- .getResourceByRef({
1536
- url: assetUrl
1537
- }).then(function(prefabResource) {
1469
+ .getResourceByRef(refItem).then(function(prefabResource) {
1538
1470
  var entity = _instanceof(prefabResource, PrefabResource) ? prefabResource.instantiate() : prefabResource.instantiateSceneRoot();
1539
- var instanceContext = new ParserContext(engine, ParserType.Prefab, null);
1540
- _this._generateInstanceContext(entity, instanceContext, "");
1541
- _this._prefabContextMap.set(entity, instanceContext);
1542
- var cbArray = _this._prefabPromiseMap.get(entityConfig.id);
1543
- if (cbArray) {
1544
- for(var i = 0, n = cbArray.length; i < n; i++){
1545
- cbArray[i].resolve(instanceContext);
1546
- }
1547
- }
1471
+ _this._onEntityCreated(entity);
1548
1472
  return entity;
1549
1473
  });
1550
1474
  };
1551
- _proto._parseStrippedEntity = function _parseStrippedEntity(entityConfig) {
1552
- var _this = this;
1553
- this.context.strippedIds.push(entityConfig.id);
1554
- return new Promise(function(resolve, reject) {
1555
- var _this__prefabPromiseMap_get;
1556
- var cbArray = (_this__prefabPromiseMap_get = _this._prefabPromiseMap.get(entityConfig.prefabInstanceId)) != null ? _this__prefabPromiseMap_get : [];
1557
- cbArray.push({
1558
- resolve: resolve,
1559
- reject: reject
1560
- });
1561
- _this._prefabPromiseMap.set(entityConfig.prefabInstanceId, cbArray);
1562
- }).then(function(context) {
1563
- var entityId = entityConfig.prefabSource.entityId;
1564
- return context.entityMap.get(entityId);
1565
- });
1566
- };
1567
- _proto._parseChildren = function _parseChildren(parentId) {
1568
- var _this_context = this.context, entityConfigMap = _this_context.entityConfigMap, entityMap = _this_context.entityMap;
1569
- var children = entityConfigMap.get(parentId).children;
1570
- if (children && children.length > 0) {
1571
- var parent = entityMap.get(parentId);
1572
- for(var i = 0; i < children.length; i++){
1573
- var childId = children[i];
1574
- var entity = entityMap.get(childId);
1575
- parent.addChild(entity);
1576
- this._parseChildren(childId);
1577
- }
1475
+ // ---------------------------------------------------------------------------
1476
+ // Utilities
1477
+ // ---------------------------------------------------------------------------
1478
+ /** Resolve an entity inside a prefab instance by walking the child-index path from root */ HierarchyParser._resolveEntity = function _resolveEntity(root, path) {
1479
+ var entity = root;
1480
+ for(var i = 0, n = path.length; i < n; i++){
1481
+ entity = entity.children[path[i]];
1482
+ if (!entity) throw new Error("HierarchyParser: override target entity not found at path [" + path + "], failed at depth " + i);
1578
1483
  }
1484
+ return entity;
1579
1485
  };
1580
- _proto._addComponents = function _addComponents(entity, components) {
1581
- var context = this.context;
1582
- var componentMap = context.components;
1583
- var componentConfigMap = context.componentConfigMap;
1584
- for(var i = 0, n = components.length; i < n; i++){
1585
- var componentConfig = components[i];
1586
- var key = !componentConfig.url ? componentConfig.class : componentConfig.url;
1587
- var componentId = componentConfig.id;
1588
- var component = entity.addComponent(engineCore.Loader.getClass(key));
1589
- componentMap.set(componentId, component);
1590
- componentConfigMap.set(componentId, componentConfig);
1591
- this._addComponentPlugin(componentId, component);
1592
- }
1593
- };
1594
- _proto._generateInstanceContext = function _generateInstanceContext(entity, context, path) {
1595
- var entityMap = context.entityMap, components = context.components;
1596
- var componentsMap = {};
1597
- var componentIndexMap = {};
1598
- entityMap.set(path, entity);
1599
- // @ts-ignore
1600
- entity._components.forEach(function(component) {
1601
- // @ts-ignore
1602
- var name = engineCore.Loader.getClassName(component.constructor);
1603
- if (!componentsMap[name]) {
1604
- componentsMap[name] = entity.getComponents(component.constructor, []);
1605
- componentIndexMap[name] = 0;
1606
- }
1607
- components.set(path + ":" + name + "/" + componentIndexMap[name]++, component);
1608
- });
1609
- for(var i = 0, n = entity.children.length; i < n; i++){
1610
- var child = entity.children[i];
1611
- var childPath = path ? path + "/" + i : "" + i;
1612
- this._generateInstanceContext(child, context, childPath);
1613
- }
1486
+ /** Resolve a component on an entity by type name + per-type index */ HierarchyParser._resolveComponent = function _resolveComponent(entity, selector) {
1487
+ var type = engineCore.Loader.getClass(selector.type);
1488
+ if (!type) throw new Error('HierarchyParser: override target component type "' + selector.type + '" is not registered');
1489
+ var buffer = ReflectionParser._componentBuffer;
1490
+ buffer.length = 0;
1491
+ entity.getComponents(type, buffer);
1492
+ var result = buffer[selector.index];
1493
+ buffer.length = 0;
1494
+ if (!result) throw new Error("HierarchyParser: override target component not found: " + selector.type + "/" + selector.index);
1495
+ return result;
1614
1496
  };
1615
- _proto._parseComponentsPropsAndMethods = function _parseComponentsPropsAndMethods() {
1616
- var context = this.context;
1617
- var componentConfigMap = context.componentConfigMap;
1618
- var reflectionParser = this._reflectionParser;
1619
- var promises = [];
1620
- for(var _iterator = _create_for_of_iterator_helper_loose(context.components), _step; !(_step = _iterator()).done;){
1621
- var _step_value = _step.value, componentId = _step_value[0], component = _step_value[1];
1622
- var componentConfig = componentConfigMap.get(componentId);
1623
- if (componentConfig) {
1624
- promises.push(reflectionParser.parsePropsAndMethods(component, componentConfig));
1625
- }
1626
- }
1627
- return Promise.all(promises);
1497
+ /** Resolve component class from config and add to entity. Throws if class is not registered. */ HierarchyParser._addComponentFromConfig = function _addComponentFromConfig(entity, config, refs) {
1498
+ var key = config.script != null ? resolveRefItem(refs, config.script, "HierarchyParser", "component.script").url : config.type;
1499
+ var Class = engineCore.Loader.getClass(key);
1500
+ if (!Class) throw new Error('Loader.getClass: class "' + key + '" is not registered');
1501
+ return entity.addComponent(Class);
1502
+ };
1503
+ HierarchyParser._isPrefabInstanceEntity = function _isPrefabInstanceEntity(entityConfig) {
1504
+ return "instance" in entityConfig;
1505
+ };
1506
+ /** Apply entity-level props (name, isActive, layer, transform) to an entity. */ HierarchyParser._applyEntityProps = function _applyEntityProps(entity, props) {
1507
+ if (props.name != null) entity.name = props.name;
1508
+ if (props.isActive != null) entity.isActive = props.isActive;
1509
+ if (props.layer != null) entity.layer = props.layer;
1510
+ if (props.position) entity.transform.position.set(props.position[0], props.position[1], props.position[2]);
1511
+ if (props.rotation) entity.transform.rotation.set(props.rotation[0], props.rotation[1], props.rotation[2]);
1512
+ if (props.scale) entity.transform.scale.set(props.scale[0], props.scale[1], props.scale[2]);
1628
1513
  };
1629
- _proto._addComponentPlugin = function _addComponentPlugin(componentId, component) {};
1630
- _proto._addEntityPlugin = function _addEntityPlugin(entityId, entity) {};
1631
1514
  return HierarchyParser;
1632
1515
  }();
1633
1516
 
1634
- var MaterialLoaderType = /*#__PURE__*/ function(MaterialLoaderType) {
1635
- MaterialLoaderType["Vector2"] = "Vector2";
1636
- MaterialLoaderType["Vector3"] = "Vector3";
1637
- MaterialLoaderType["Vector4"] = "Vector4";
1638
- MaterialLoaderType["Color"] = "Color";
1639
- MaterialLoaderType["Float"] = "Float";
1640
- MaterialLoaderType["Texture"] = "Texture";
1641
- MaterialLoaderType["Boolean"] = "Boolean";
1642
- MaterialLoaderType["Integer"] = "Integer";
1643
- return MaterialLoaderType;
1644
- }({});
1645
-
1646
- var SpecularMode = /*#__PURE__*/ function(SpecularMode) {
1647
- SpecularMode["Sky"] = "Sky";
1648
- SpecularMode["Custom"] = "Custom";
1649
- return SpecularMode;
1650
- }({});
1651
-
1652
1517
  /** @Internal */ var SceneParser = /*#__PURE__*/ function(HierarchyParser) {
1653
1518
  _inherits(SceneParser, HierarchyParser);
1654
1519
  function SceneParser(data, context, scene) {
@@ -1662,91 +1527,43 @@ var SpecularMode = /*#__PURE__*/ function(SpecularMode) {
1662
1527
  */ _proto._collectDependentAssets = function _collectDependentAssets(data) {
1663
1528
  var context = this.context;
1664
1529
  var resourceManager = context.resourceManager;
1665
- this._parseDependentAssets(data);
1666
- var scene = data.scene;
1667
- var ambient = scene.ambient;
1668
- if (ambient) {
1669
- var useCustomAmbient = ambient.specularMode === SpecularMode.Custom;
1670
- var useSH = ambient.diffuseMode === engineCore.DiffuseMode.SphericalHarmonics;
1671
- var customAmbientLight = ambient.customAmbientLight, ambientLight = ambient.ambientLight;
1672
- if (useCustomAmbient && customAmbientLight) {
1673
- // @ts-ignore
1674
- context._addDependentAsset(customAmbientLight.url, resourceManager.getResourceByRef(customAmbientLight));
1675
- }
1676
- if (ambientLight && (!useCustomAmbient || useSH)) {
1677
- // @ts-ignore
1678
- context._addDependentAsset(ambientLight.url, resourceManager.getResourceByRef(ambientLight));
1679
- }
1680
- }
1681
- var background = scene.background;
1682
- var backgroundMode = background.mode;
1683
- if (backgroundMode === engineCore.BackgroundMode.Texture) {
1684
- var texture = background.texture;
1530
+ var refs = data.refs;
1531
+ for(var i = 0, n = refs.length; i < n; i++){
1685
1532
  // @ts-ignore
1686
- texture && context._addDependentAsset(texture.url, resourceManager.getResourceByRef(texture));
1687
- } else if (backgroundMode === engineCore.BackgroundMode.Sky) {
1688
- var skyMesh = background.skyMesh, skyMaterial = background.skyMaterial;
1689
- if (skyMesh && skyMaterial) {
1690
- // @ts-ignore
1691
- context._addDependentAsset(skyMesh.url, resourceManager.getResourceByRef(skyMesh));
1692
- // @ts-ignore
1693
- context._addDependentAsset(skyMaterial.url, resourceManager.getResourceByRef(skyMaterial));
1694
- }
1533
+ context._addDependentAsset(resourceManager.getResourceByRef(refs[i]));
1695
1534
  }
1696
1535
  };
1697
- _proto._handleRootEntity = function _handleRootEntity(id) {
1698
- var entityMap = this.context.entityMap;
1699
- this.scene.addRootEntity(entityMap.get(id));
1536
+ _proto._getRootIndices = function _getRootIndices() {
1537
+ return this.data.scene.entities;
1538
+ };
1539
+ _proto._handleRootEntity = function _handleRootEntity(index) {
1540
+ this.scene.addRootEntity(this.context.entityInstances[index]);
1700
1541
  };
1701
1542
  _proto._clearAndResolve = function _clearAndResolve() {
1702
1543
  this.context.clear();
1703
1544
  return this.scene;
1704
1545
  };
1705
- _proto._parseDependentAssets = function _parseDependentAssets(file) {
1706
- var entities = file.entities;
1707
- for(var i = 0, n = entities.length; i < n; i++){
1708
- var entity = entities[i];
1709
- if (!!entity.assetUrl) {
1710
- var context = this.context;
1711
- var url = entity.assetUrl, key = entity.key;
1712
- // @ts-ignore
1713
- context._addDependentAsset(url, context.resourceManager.getResourceByRef({
1714
- url: url,
1715
- key: key
1716
- }));
1717
- } else if (entity.strippedId) {
1718
- continue;
1719
- } else {
1720
- var components = entity.components;
1721
- for(var j = 0, m = components.length; j < m; j++){
1722
- var component = components[j];
1723
- this._searchDependentAssets(component.methods);
1724
- this._searchDependentAssets(component.props);
1725
- }
1726
- }
1727
- }
1728
- };
1729
- _proto._searchDependentAssets = function _searchDependentAssets(value) {
1730
- if (Array.isArray(value)) {
1731
- for(var i = 0, n = value.length; i < n; i++){
1732
- this._searchDependentAssets(value[i]);
1733
- }
1734
- } else if (!!value && (typeof value === "undefined" ? "undefined" : _type_of(value)) === "object") {
1735
- // @ts-ignore
1736
- if (ReflectionParser._isAssetRef(value)) {
1737
- var context = this.context;
1738
- // @ts-ignore
1739
- context._addDependentAsset(value.url, context.resourceManager.getResourceByRef(value));
1740
- } else {
1741
- for(var key in value){
1742
- this._searchDependentAssets(value[key]);
1743
- }
1744
- }
1745
- }
1746
- };
1747
1546
  return SceneParser;
1748
1547
  }(HierarchyParser);
1749
1548
 
1549
+ var SpecularMode = /*#__PURE__*/ function(SpecularMode) {
1550
+ SpecularMode[SpecularMode["Sky"] = 0] = "Sky";
1551
+ SpecularMode[SpecularMode["Custom"] = 1] = "Custom";
1552
+ return SpecularMode;
1553
+ }({});
1554
+
1555
+ var MaterialLoaderType = /*#__PURE__*/ function(MaterialLoaderType) {
1556
+ MaterialLoaderType["Vector2"] = "Vector2";
1557
+ MaterialLoaderType["Vector3"] = "Vector3";
1558
+ MaterialLoaderType["Vector4"] = "Vector4";
1559
+ MaterialLoaderType["Color"] = "Color";
1560
+ MaterialLoaderType["Float"] = "Float";
1561
+ MaterialLoaderType["Texture"] = "Texture";
1562
+ MaterialLoaderType["Boolean"] = "Boolean";
1563
+ MaterialLoaderType["Integer"] = "Integer";
1564
+ return MaterialLoaderType;
1565
+ }({});
1566
+
1750
1567
  /**
1751
1568
  * Decode engine binary resource.
1752
1569
  * @param arrayBuffer - array buffer of decode binary file
@@ -3228,6 +3045,45 @@ function getMeshoptDecoder() {
3228
3045
  return GLTFResource;
3229
3046
  }(engineCore.ReferResource);
3230
3047
 
3048
+ function _array_like_to_array(arr, len) {
3049
+ if (len == null || len > arr.length) len = arr.length;
3050
+
3051
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
3052
+
3053
+ return arr2;
3054
+ }
3055
+
3056
+ function _unsupported_iterable_to_array(o, minLen) {
3057
+ if (!o) return;
3058
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
3059
+
3060
+ var n = Object.prototype.toString.call(o).slice(8, -1);
3061
+
3062
+ if (n === "Object" && o.constructor) n = o.constructor.name;
3063
+ if (n === "Map" || n === "Set") return Array.from(n);
3064
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
3065
+ }
3066
+
3067
+ function _create_for_of_iterator_helper_loose(o, allowArrayLike) {
3068
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
3069
+
3070
+ if (it) return (it = it.call(o)).next.bind(it);
3071
+ // Fallback for engines without symbol support
3072
+ if (Array.isArray(o) || (it = _unsupported_iterable_to_array(o)) || allowArrayLike && o && typeof o.length === "number") {
3073
+ if (it) o = it;
3074
+
3075
+ var i = 0;
3076
+
3077
+ return function() {
3078
+ if (i >= o.length) return { done: true };
3079
+
3080
+ return { done: false, value: o[i++] };
3081
+ };
3082
+ }
3083
+
3084
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
3085
+ }
3086
+
3231
3087
  /**
3232
3088
  * Module for glTF 2.0 Interface
3233
3089
  */ /**
@@ -5497,15 +5353,17 @@ var PrefabParser = /*#__PURE__*/ function(HierarchyParser) {
5497
5353
  return _this;
5498
5354
  }
5499
5355
  var _proto = PrefabParser.prototype;
5500
- _proto._applyEntityData = function _applyEntityData(entity, entityConfig) {
5501
- if (entityConfig === void 0) entityConfig = {};
5502
- HierarchyParser.prototype._applyEntityData.call(this, entity, entityConfig);
5356
+ _proto._onEntityCreated = function _onEntityCreated(entity) {
5503
5357
  // @ts-ignore
5504
5358
  entity._markAsTemplate(this.context.resource);
5505
- return entity;
5506
5359
  };
5507
- _proto._handleRootEntity = function _handleRootEntity(id) {
5508
- this.prefabResource._root = this.context.entityMap.get(id);
5360
+ _proto._getRootIndices = function _getRootIndices() {
5361
+ return [
5362
+ this.data.root
5363
+ ];
5364
+ };
5365
+ _proto._handleRootEntity = function _handleRootEntity(index) {
5366
+ this.prefabResource._root = this.context.entityInstances[index];
5509
5367
  };
5510
5368
  _proto._clearAndResolve = function _clearAndResolve() {
5511
5369
  this.context.clear();
@@ -5516,9 +5374,7 @@ var PrefabParser = /*#__PURE__*/ function(HierarchyParser) {
5516
5374
  var context = new ParserContext(engine, ParserType.Prefab, prefabResource);
5517
5375
  var parser = new PrefabParser(data, context, prefabResource);
5518
5376
  parser.start();
5519
- return parser.promise.then(function() {
5520
- return prefabResource;
5521
- });
5377
+ return parser.promise;
5522
5378
  };
5523
5379
  return PrefabParser;
5524
5380
  }(HierarchyParser);
@@ -5858,15 +5714,6 @@ var KTXContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
5858
5714
  return KTXContentRestorer;
5859
5715
  }(engineCore.ContentRestorer);
5860
5716
 
5861
- function parseProperty(object, key, value) {
5862
- if ((typeof value === "undefined" ? "undefined" : _type_of(value)) === "object") {
5863
- for(var subKey in value){
5864
- parseProperty(object[key], subKey, value[subKey]);
5865
- }
5866
- } else {
5867
- object[key] = value;
5868
- }
5869
- }
5870
5717
  var MaterialLoader = /*#__PURE__*/ function(Loader) {
5871
5718
  _inherits(MaterialLoader, Loader);
5872
5719
  function MaterialLoader() {
@@ -5927,7 +5774,7 @@ var MaterialLoader = /*#__PURE__*/ function(Loader) {
5927
5774
  break;
5928
5775
  }
5929
5776
  };
5930
- var name = materialSchema.name, shaderData = materialSchema.shaderData, macros = materialSchema.macros, renderState = materialSchema.renderState;
5777
+ var name = materialSchema.name, shaderData = materialSchema.shaderData, macros = materialSchema.macros;
5931
5778
  var material = new engineCore.Material(engine, shader);
5932
5779
  material.name = name;
5933
5780
  var texturePromises = new Array();
@@ -5941,7 +5788,6 @@ var MaterialLoader = /*#__PURE__*/ function(Loader) {
5941
5788
  materialShaderData.enableMacro(name1, value);
5942
5789
  }
5943
5790
  }
5944
- parseProperty(material, "renderState", renderState);
5945
5791
  return Promise.all(texturePromises).then(function() {
5946
5792
  return material;
5947
5793
  });
@@ -6483,51 +6329,6 @@ AudioLoader = __decorate([
6483
6329
  ])
6484
6330
  ], AudioLoader);
6485
6331
 
6486
- var ShaderChunkLoader = /*#__PURE__*/ function(Loader) {
6487
- _inherits(ShaderChunkLoader, Loader);
6488
- function ShaderChunkLoader() {
6489
- return Loader.apply(this, arguments) || this;
6490
- }
6491
- var _proto = ShaderChunkLoader.prototype;
6492
- _proto.load = function load(item, resourceManager) {
6493
- var url = item.url;
6494
- // @ts-ignore
6495
- return resourceManager._request(url, _extends({}, item, {
6496
- type: "text"
6497
- })).then(function(code) {
6498
- engineCore.ShaderFactory.registerInclude(url.substring(1), code);
6499
- return ShaderChunkLoader._loadChunksInCode(code, url, resourceManager);
6500
- });
6501
- };
6502
- /**
6503
- * @internal
6504
- */ ShaderChunkLoader._loadChunksInCode = function _loadChunksInCode(code, basePath, resourceManager) {
6505
- var shaderChunkPaths = new Array();
6506
- var matches = code.matchAll(ShaderChunkLoader._shaderIncludeRegex);
6507
- for(var _iterator = _create_for_of_iterator_helper_loose(matches), _step; !(_step = _iterator()).done;){
6508
- var match = _step.value;
6509
- var chunkPath = engineCore.Utils.resolveAbsoluteUrl(basePath, match[1]);
6510
- if (!engineCore.ShaderLib[chunkPath.substring(1)]) {
6511
- shaderChunkPaths.push(chunkPath);
6512
- }
6513
- }
6514
- return Promise.all(shaderChunkPaths.map(function(chunkPath) {
6515
- // @ts-ignore
6516
- return resourceManager.load({
6517
- type: "ShaderChunk",
6518
- url: chunkPath
6519
- });
6520
- }));
6521
- };
6522
- return ShaderChunkLoader;
6523
- }(engineCore.Loader);
6524
- ShaderChunkLoader._shaderIncludeRegex = /#include\s+"([./][^\\"]+)"/gm;
6525
- ShaderChunkLoader = __decorate([
6526
- engineCore.resourceLoader("ShaderChunk", [
6527
- "glsl"
6528
- ])
6529
- ], ShaderChunkLoader);
6530
-
6531
6332
  var ShaderLoader = /*#__PURE__*/ function(Loader) {
6532
6333
  _inherits(ShaderLoader, Loader);
6533
6334
  function ShaderLoader() {
@@ -6537,7 +6338,7 @@ var ShaderLoader = /*#__PURE__*/ function(Loader) {
6537
6338
  _proto.load = function load(item, resourceManager) {
6538
6339
  var _this = this;
6539
6340
  var url = item.url;
6540
- if (url.endsWith(".gsp")) {
6341
+ if (url.endsWith(".shaderc")) {
6541
6342
  // @ts-ignore
6542
6343
  return resourceManager._request(url, _extends({}, item, {
6543
6344
  type: "json"
@@ -6554,10 +6355,7 @@ var ShaderLoader = /*#__PURE__*/ function(Loader) {
6554
6355
  if (builtinShader) {
6555
6356
  return engineCore.Shader.find(builtinShader);
6556
6357
  }
6557
- return ShaderChunkLoader._loadChunksInCode(code, url, resourceManager).then(function() {
6558
- var shader = engineCore.Shader.create(code, undefined, url);
6559
- return shader;
6560
- });
6358
+ return engineCore.Shader.create(code, undefined, url);
6561
6359
  });
6562
6360
  };
6563
6361
  _proto._getBuiltinShader = function _getBuiltinShader(code) {
@@ -6569,7 +6367,8 @@ var ShaderLoader = /*#__PURE__*/ function(Loader) {
6569
6367
  ShaderLoader._builtinRegex = /^\s*\/\/\s*@builtin\s+(\w+)/;
6570
6368
  ShaderLoader = __decorate([
6571
6369
  engineCore.resourceLoader(engineCore.AssetType.Shader, [
6572
- "shader"
6370
+ "shader",
6371
+ "shaderc"
6573
6372
  ])
6574
6373
  ], ShaderLoader);
6575
6374
 
@@ -6649,6 +6448,122 @@ RenderTargetLoader = __decorate([
6649
6448
  ])
6650
6449
  ], RenderTargetLoader);
6651
6450
 
6451
+ function loadRef(refs, index, resourceManager, label) {
6452
+ var ref = resolveRefItem(refs, index, "SceneLoader", label);
6453
+ // @ts-ignore
6454
+ return resourceManager.getResourceByRef(ref);
6455
+ }
6456
+ /**
6457
+ * Apply scene-level data (ambient, background, shadow, fog, AO) to a Scene.
6458
+ * @internal
6459
+ */ function applySceneData(scene, sceneData, resourceManager, refs) {
6460
+ var promises = [];
6461
+ try {
6462
+ // parse ambient light
6463
+ var ambient = sceneData.ambient;
6464
+ if (ambient) {
6465
+ var useCustomAmbient = ambient.specularMode === SpecularMode.Custom;
6466
+ var useSH = ambient.diffuseMode === engineCore.DiffuseMode.SphericalHarmonics;
6467
+ scene.ambientLight.diffuseIntensity = ambient.diffuseIntensity;
6468
+ scene.ambientLight.specularIntensity = ambient.specularIntensity;
6469
+ scene.ambientLight.diffuseMode = ambient.diffuseMode;
6470
+ var solidColor = ambient.diffuseSolidColor;
6471
+ if (solidColor) {
6472
+ scene.ambientLight.diffuseSolidColor.set(solidColor[0], solidColor[1], solidColor[2], solidColor[3]);
6473
+ }
6474
+ if (useCustomAmbient && ambient.customAmbientLight != null) {
6475
+ promises.push(loadRef(refs, ambient.customAmbientLight, resourceManager, "scene.ambient.customAmbientLight").then(function(ambientLight) {
6476
+ scene.ambientLight.specularTexture = ambientLight == null ? void 0 : ambientLight.specularTexture;
6477
+ }));
6478
+ }
6479
+ if (ambient.ambientLight != null && (!useCustomAmbient || useSH)) {
6480
+ promises.push(loadRef(refs, ambient.ambientLight, resourceManager, "scene.ambient.ambientLight").then(function(ambientLight) {
6481
+ if (!useCustomAmbient) {
6482
+ scene.ambientLight.specularTexture = ambientLight == null ? void 0 : ambientLight.specularTexture;
6483
+ }
6484
+ if (useSH) {
6485
+ scene.ambientLight.diffuseSphericalHarmonics = ambientLight == null ? void 0 : ambientLight.diffuseSphericalHarmonics;
6486
+ }
6487
+ }));
6488
+ }
6489
+ }
6490
+ // parse background
6491
+ var background = sceneData.background;
6492
+ scene.background.mode = background.mode;
6493
+ switch(scene.background.mode){
6494
+ case engineCore.BackgroundMode.SolidColor:
6495
+ {
6496
+ var color = background.color;
6497
+ scene.background.solidColor.set(color[0], color[1], color[2], color[3]);
6498
+ break;
6499
+ }
6500
+ case engineCore.BackgroundMode.Sky:
6501
+ if (background.skyMesh != null && background.skyMaterial != null) {
6502
+ promises.push(loadRef(refs, background.skyMesh, resourceManager, "scene.background.skyMesh").then(function(mesh) {
6503
+ scene.background.sky.mesh = mesh;
6504
+ }), loadRef(refs, background.skyMaterial, resourceManager, "scene.background.skyMaterial").then(function(material) {
6505
+ scene.background.sky.material = material;
6506
+ }));
6507
+ } else {
6508
+ engineCore.Logger.warn("Sky background mode requires skyMesh and skyMaterial");
6509
+ }
6510
+ break;
6511
+ case engineCore.BackgroundMode.Texture:
6512
+ if (background.texture != null) {
6513
+ promises.push(loadRef(refs, background.texture, resourceManager, "scene.background.texture").then(function(texture) {
6514
+ scene.background.texture = texture;
6515
+ }));
6516
+ var _background_textureFillMode;
6517
+ scene.background.textureFillMode = (_background_textureFillMode = background.textureFillMode) != null ? _background_textureFillMode : scene.background.textureFillMode;
6518
+ }
6519
+ break;
6520
+ }
6521
+ } catch (error) {
6522
+ return Promise.reject(error);
6523
+ }
6524
+ // parse shadow
6525
+ var shadow = sceneData.shadow;
6526
+ if (shadow) {
6527
+ if (shadow.castShadows != undefined) scene.castShadows = shadow.castShadows;
6528
+ if (shadow.shadowResolution != undefined) scene.shadowResolution = shadow.shadowResolution;
6529
+ if (shadow.shadowDistance != undefined) scene.shadowDistance = shadow.shadowDistance;
6530
+ if (shadow.shadowCascades != undefined) scene.shadowCascades = shadow.shadowCascades;
6531
+ if (shadow.enableTransparentShadow != undefined) scene.enableTransparentShadow = shadow.enableTransparentShadow;
6532
+ if (shadow.shadowTwoCascadeSplits != undefined) scene.shadowTwoCascadeSplits = shadow.shadowTwoCascadeSplits;
6533
+ if (shadow.shadowFourCascadeSplits) {
6534
+ var splits = shadow.shadowFourCascadeSplits;
6535
+ scene.shadowFourCascadeSplits.set(splits[0], splits[1], splits[2]);
6536
+ }
6537
+ if (shadow.shadowFadeBorder != undefined) scene.shadowFadeBorder = shadow.shadowFadeBorder;
6538
+ }
6539
+ // parse fog
6540
+ var fog = sceneData.fog;
6541
+ if (fog) {
6542
+ if (fog.fogMode != undefined) scene.fogMode = fog.fogMode;
6543
+ if (fog.fogStart != undefined) scene.fogStart = fog.fogStart;
6544
+ if (fog.fogEnd != undefined) scene.fogEnd = fog.fogEnd;
6545
+ if (fog.fogDensity != undefined) scene.fogDensity = fog.fogDensity;
6546
+ if (fog.fogColor) scene.fogColor.set(fog.fogColor[0], fog.fogColor[1], fog.fogColor[2], fog.fogColor[3]);
6547
+ }
6548
+ // Post Process
6549
+ if (sceneData.postProcess) {
6550
+ engineCore.Logger.warn("Post Process is not supported in scene yet, please add PostProcess component in entity instead.");
6551
+ }
6552
+ // Ambient Occlusion
6553
+ var ambientOcclusion = sceneData.ambientOcclusion;
6554
+ if (ambientOcclusion) {
6555
+ var sceneAO = scene.ambientOcclusion;
6556
+ if (ambientOcclusion.enabledAmbientOcclusion != undefined) sceneAO.enabled = ambientOcclusion.enabledAmbientOcclusion;
6557
+ if (ambientOcclusion.quality != undefined) sceneAO.quality = ambientOcclusion.quality;
6558
+ if (ambientOcclusion.intensity != undefined) sceneAO.intensity = ambientOcclusion.intensity;
6559
+ if (ambientOcclusion.radius != undefined) sceneAO.radius = ambientOcclusion.radius;
6560
+ if (ambientOcclusion.bias != undefined) sceneAO.bias = ambientOcclusion.bias;
6561
+ if (ambientOcclusion.power != undefined) sceneAO.power = ambientOcclusion.power;
6562
+ if (ambientOcclusion.bilateralThreshold != undefined) sceneAO.bilateralThreshold = ambientOcclusion.bilateralThreshold;
6563
+ if (ambientOcclusion.minHorizonAngle != undefined) sceneAO.minHorizonAngle = ambientOcclusion.minHorizonAngle;
6564
+ }
6565
+ return Promise.all(promises).then(function() {});
6566
+ }
6652
6567
  var SceneLoader = /*#__PURE__*/ function(Loader) {
6653
6568
  _inherits(SceneLoader, Loader);
6654
6569
  function SceneLoader() {
@@ -6662,122 +6577,15 @@ var SceneLoader = /*#__PURE__*/ function(Loader) {
6662
6577
  ._request(item.url, _extends({}, item, {
6663
6578
  type: "json"
6664
6579
  })).then(function(data) {
6665
- var _data_name;
6666
- var scene = new engineCore.Scene(engine, (_data_name = data.name) != null ? _data_name : "");
6580
+ var _data_scene_name;
6581
+ var scene = new engineCore.Scene(engine, (_data_scene_name = data.scene.name) != null ? _data_scene_name : "");
6667
6582
  var context = new ParserContext(engine, ParserType.Scene, scene);
6668
6583
  var parser = new SceneParser(data, context, scene);
6669
6584
  parser._collectDependentAssets(data);
6670
6585
  context._setTaskCompleteProgress = setTaskCompleteProgress;
6671
6586
  parser.start();
6672
6587
  return parser.promise.then(function() {
6673
- var promises = [];
6674
- // parse ambient light
6675
- var ambient = data.scene.ambient;
6676
- if (ambient) {
6677
- var useCustomAmbient = ambient.specularMode === SpecularMode.Custom;
6678
- var useSH = ambient.diffuseMode === engineCore.DiffuseMode.SphericalHarmonics;
6679
- scene.ambientLight.diffuseIntensity = ambient.diffuseIntensity;
6680
- scene.ambientLight.specularIntensity = ambient.specularIntensity;
6681
- scene.ambientLight.diffuseMode = ambient.diffuseMode;
6682
- scene.ambientLight.diffuseSolidColor.copyFrom(ambient.diffuseSolidColor);
6683
- if (useCustomAmbient && ambient.customAmbientLight) {
6684
- promises.push(// @ts-ignore
6685
- resourceManager.getResourceByRef(ambient.customAmbientLight).then(function(ambientLight) {
6686
- scene.ambientLight.specularTexture = ambientLight == null ? void 0 : ambientLight.specularTexture;
6687
- }));
6688
- }
6689
- if (ambient.ambientLight && (!useCustomAmbient || useSH)) {
6690
- promises.push(// @ts-ignore
6691
- resourceManager.getResourceByRef(ambient.ambientLight).then(function(ambientLight) {
6692
- if (!useCustomAmbient) {
6693
- scene.ambientLight.specularTexture = ambientLight == null ? void 0 : ambientLight.specularTexture;
6694
- }
6695
- if (useSH) {
6696
- scene.ambientLight.diffuseSphericalHarmonics = ambientLight == null ? void 0 : ambientLight.diffuseSphericalHarmonics;
6697
- }
6698
- }));
6699
- }
6700
- }
6701
- // parse background
6702
- var background = data.scene.background;
6703
- scene.background.mode = background.mode;
6704
- switch(scene.background.mode){
6705
- case engineCore.BackgroundMode.SolidColor:
6706
- scene.background.solidColor.copyFrom(background.color);
6707
- break;
6708
- case engineCore.BackgroundMode.Sky:
6709
- if (background.skyMesh && background.skyMaterial) {
6710
- // @ts-ignore
6711
- var skyMeshPromise = resourceManager.getResourceByRef(background.skyMesh).then(function(mesh) {
6712
- scene.background.sky.mesh = mesh;
6713
- });
6714
- // @ts-ignore
6715
- // prettier-ignore
6716
- var skyMaterialPromise = resourceManager.getResourceByRef(background.skyMaterial).then(function(material) {
6717
- scene.background.sky.material = material;
6718
- });
6719
- promises.push(skyMeshPromise, skyMaterialPromise);
6720
- } else {
6721
- engineCore.Logger.warn("Sky background mode requires skyMesh and skyMaterial");
6722
- }
6723
- break;
6724
- case engineCore.BackgroundMode.Texture:
6725
- if (background.texture) {
6726
- // @ts-ignore
6727
- // prettier-ignore
6728
- var backgroundPromise = resourceManager.getResourceByRef(background.texture).then(function(texture) {
6729
- scene.background.texture = texture;
6730
- });
6731
- promises.push(backgroundPromise);
6732
- var _background_textureFillMode;
6733
- scene.background.textureFillMode = (_background_textureFillMode = background.textureFillMode) != null ? _background_textureFillMode : scene.background.textureFillMode;
6734
- }
6735
- break;
6736
- }
6737
- // parse shadow
6738
- var shadow = data.scene.shadow;
6739
- if (shadow) {
6740
- if (shadow.castShadows != undefined) scene.castShadows = shadow.castShadows;
6741
- if (shadow.shadowResolution != undefined) scene.shadowResolution = shadow.shadowResolution;
6742
- if (shadow.shadowDistance != undefined) scene.shadowDistance = shadow.shadowDistance;
6743
- if (shadow.shadowCascades != undefined) scene.shadowCascades = shadow.shadowCascades;
6744
- if (shadow.enableTransparentShadow != undefined) {
6745
- scene.enableTransparentShadow = shadow.enableTransparentShadow;
6746
- }
6747
- var _shadow_shadowTwoCascadeSplits;
6748
- scene.shadowTwoCascadeSplits = (_shadow_shadowTwoCascadeSplits = shadow.shadowTwoCascadeSplits) != null ? _shadow_shadowTwoCascadeSplits : scene.shadowTwoCascadeSplits;
6749
- shadow.shadowFourCascadeSplits && scene.shadowFourCascadeSplits.copyFrom(shadow.shadowFourCascadeSplits);
6750
- var _shadow_shadowFadeBorder;
6751
- scene.shadowFadeBorder = (_shadow_shadowFadeBorder = shadow.shadowFadeBorder) != null ? _shadow_shadowFadeBorder : scene.shadowFadeBorder;
6752
- }
6753
- // parse fog
6754
- var fog = data.scene.fog;
6755
- if (fog) {
6756
- if (fog.fogMode != undefined) scene.fogMode = fog.fogMode;
6757
- if (fog.fogStart != undefined) scene.fogStart = fog.fogStart;
6758
- if (fog.fogEnd != undefined) scene.fogEnd = fog.fogEnd;
6759
- if (fog.fogDensity != undefined) scene.fogDensity = fog.fogDensity;
6760
- if (fog.fogColor != undefined) scene.fogColor.copyFrom(fog.fogColor);
6761
- }
6762
- // Post Process
6763
- var postProcessData = data.scene.postProcess;
6764
- if (postProcessData) {
6765
- engineCore.Logger.warn("Post Process is not supported in scene yet, please add PostProcess component in entity instead.");
6766
- }
6767
- // Ambient Occlusion
6768
- var ambientOcclusion = data.scene.ambientOcclusion;
6769
- if (ambientOcclusion) {
6770
- var sceneAmbientOcclusion = scene.ambientOcclusion;
6771
- sceneAmbientOcclusion.enabled = ambientOcclusion.enabledAmbientOcclusion;
6772
- sceneAmbientOcclusion.intensity = ambientOcclusion.intensity;
6773
- sceneAmbientOcclusion.radius = ambientOcclusion.radius;
6774
- sceneAmbientOcclusion.bias = ambientOcclusion.bias;
6775
- sceneAmbientOcclusion.power = ambientOcclusion.power;
6776
- sceneAmbientOcclusion.quality = ambientOcclusion.quality;
6777
- sceneAmbientOcclusion.bilateralThreshold = ambientOcclusion.bilateralThreshold;
6778
- sceneAmbientOcclusion.minHorizonAngle = ambientOcclusion.minHorizonAngle;
6779
- }
6780
- return Promise.all(promises).then(function() {
6588
+ return applySceneData(scene, data.scene, resourceManager, data.refs).then(function() {
6781
6589
  resolve(scene);
6782
6590
  });
6783
6591
  });