瀏覽代碼

Asset Container Task (#8624)

* Asset Container Task

Added support to the AssetsManager to load Asset Containers

* Container Asset Fixes

Container Asset Fixes
MackeyK24 5 年之前
父節點
當前提交
e0099568fa
共有 2 個文件被更改,包括 101 次插入0 次删除
  1. 1 0
      dist/preview release/what's new.md
  2. 100 0
      src/Misc/assetsManager.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -38,6 +38,7 @@
 - Added support for the alpha component to the SSR post-process ([Popov72](https://github.com/Popov72))
 - Force compute world matrix of the newly-attached mesh of a ray helper ([RaananW](https://github.com/RaananW))
 - Allow 180 monoscopic videos on top of the video dome ([#8575](https://github.com/BabylonJS/Babylon.js/issues/8575)) ([RaananW](https://github.com/RaananW))
+- Added `AssetContainerTask` support to `AssetsManager` class ([MackeyK24](https://github.com/MackeyK24))
 
 ### Engine
 

+ 100 - 0
src/Misc/assetsManager.ts

@@ -12,6 +12,7 @@ import { HDRCubeTexture } from "../Materials/Textures/hdrCubeTexture";
 import { EquiRectangularCubeTexture } from "../Materials/Textures/equiRectangularCubeTexture";
 import { Logger } from "../Misc/logger";
 import { AnimationGroup } from '../Animations/animationGroup';
+import { AssetContainer } from "../assetContainer";
 
 /**
  * Defines the list of states available for a task inside a AssetsManager
@@ -213,6 +214,90 @@ export class AssetsProgressEvent implements IAssetsProgressEvent {
 }
 
 /**
+ * Define a task used by AssetsManager to load assets into a container
+ */
+export class ContainerAssetTask extends AbstractAssetTask {
+    /**
+     * Get the loaded asset container
+     */
+    public loadedContainer: AssetContainer;
+    /**
+     * Gets the list of loaded meshes
+     */
+    public loadedMeshes: Array<AbstractMesh>;
+    /**
+     * Gets the list of loaded particle systems
+     */
+    public loadedParticleSystems: Array<IParticleSystem>;
+    /**
+     * Gets the list of loaded skeletons
+     */
+    public loadedSkeletons: Array<Skeleton>;
+    /**
+     * Gets the list of loaded animation groups
+     */
+    public loadedAnimationGroups: Array<AnimationGroup>;
+
+    /**
+     * Callback called when the task is successful
+     */
+    public onSuccess: (task: ContainerAssetTask) => void;
+
+    /**
+     * Callback called when the task is successful
+     */
+    public onError: (task: ContainerAssetTask, message?: string, exception?: any) => void;
+
+    /**
+     * Creates a new ContainerAssetTask
+     * @param name defines the name of the task
+     * @param meshesNames defines the list of mesh's names you want to load
+     * @param rootUrl defines the root url to use as a base to load your meshes and associated resources
+     * @param sceneFilename defines the filename or File of the scene to load from
+     */
+    constructor(
+        /**
+         * Defines the name of the task
+         */
+        public name: string,
+        /**
+         * Defines the list of mesh's names you want to load
+         */
+        public meshesNames: any,
+        /**
+         * Defines the root url to use as a base to load your meshes and associated resources
+         */
+        public rootUrl: string,
+        /**
+         * Defines the filename or File of the scene to load from
+         */
+        public sceneFilename: string | File) {
+        super(name);
+    }
+
+    /**
+     * Execute the current task
+     * @param scene defines the scene where you want your assets to be loaded
+     * @param onSuccess is a callback called when the task is successfully executed
+     * @param onError is a callback called if an error occurs
+     */
+    public runTask(scene: Scene, onSuccess: () => void, onError: (message?: string, exception?: any) => void) {
+        SceneLoader.LoadAssetContainer(this.rootUrl, this.sceneFilename, scene,
+            (container: AssetContainer) => {
+                this.loadedContainer = container;
+                this.loadedMeshes = container.meshes;
+                this.loadedParticleSystems = container.particleSystems;
+                this.loadedSkeletons = container.skeletons;
+                this.loadedAnimationGroups = container.animationGroups;
+                onSuccess();
+            }, null, (scene, message, exception) => {
+                onError(message, exception);
+            }
+        );
+    }
+}
+
+/**
  * Define a task used by AssetsManager to load meshes
  */
 export class MeshAssetTask extends AbstractAssetTask {
@@ -835,6 +920,21 @@ export class AssetsManager {
     }
 
     /**
+     * Add a ContainerAssetTask to the list of active tasks
+     * @param taskName defines the name of the new task
+     * @param meshesNames defines the name of meshes to load
+     * @param rootUrl defines the root url to use to locate files
+     * @param sceneFilename defines the filename of the scene file
+     * @returns a new ContainerAssetTask object
+     */
+    public addContainerTask(taskName: string, meshesNames: any, rootUrl: string, sceneFilename: string): ContainerAssetTask {
+        var task = new ContainerAssetTask(taskName, meshesNames, rootUrl, sceneFilename);
+        this._tasks.push(task);
+
+        return task;
+    }
+
+    /**
      * Add a MeshAssetTask to the list of active tasks
      * @param taskName defines the name of the new task
      * @param meshesNames defines the name of meshes to load