Quellcode durchsuchen

Merge pull request #3590 from TrevorDev/assetContainerMoveAllFromScene

Add suggested assetContainer.moveAllFromScene method
sebavan vor 7 Jahren
Ursprung
Commit
959004f8c9

+ 2 - 117
dist/preview release/typedocValidationBaseline.json

@@ -1,7 +1,7 @@
 {
-  "errors": 8892,
+  "errors": 8871,
   "babylon.typedoc.json": {
-    "errors": 8892,
+    "errors": 8871,
     "AnimationKeyInterpolation": {
       "Enumeration": {
         "Comments": {
@@ -4058,121 +4058,6 @@
         }
       }
     },
-    "AssetContainer": {
-      "Class": {
-        "Comments": {
-          "MissingText": true
-        }
-      },
-      "Constructor": {
-        "new AssetContainer": {
-          "Comments": {
-            "MissingText": true
-          },
-          "Parameter": {
-            "scene": {
-              "Comments": {
-                "MissingText": true
-              }
-            }
-          }
-        }
-      },
-      "Property": {
-        "actionManagers": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "animations": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "cameras": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "geometries": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "lensFlareSystems": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "lights": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "materials": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "meshes": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "morphTargetManagers": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "multiMaterials": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "particleSystems": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "scene": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "shadowGenerators": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "skeletons": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "sounds": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "transformNodes": {
-          "Comments": {
-            "MissingText": true
-          }
-        }
-      },
-      "Method": {
-        "addAllToScene": {
-          "Comments": {
-            "MissingText": true
-          }
-        },
-        "removeAllFromScene": {
-          "Comments": {
-            "MissingText": true
-          }
-        }
-      }
-    },
     "AsyncLoop": {
       "Property": {
         "index": {

+ 176 - 3
src/babylon.assetContainer.ts

@@ -1,29 +1,153 @@
 module BABYLON {
+    /**
+     * Set of assets to keep when moving a scene into an asset container.
+     */
+    export class KeepAssets {
+        /**
+         * Cameras to keep.
+         */
+        public cameras = new Array<Camera>();
+        /**
+         * Lights to keep.
+         */
+        public lights = new Array<Light>();
+        /**
+         * Meshes to keep.
+         */
+        public meshes = new Array<AbstractMesh>();
+        /**
+         * Skeletons to keep.
+         */
+        public skeletons = new Array<Skeleton>();
+        /**
+         * ParticleSystems to keep.
+         */
+        public particleSystems = new Array<ParticleSystem>();
+        /**
+         * Animations to keep.
+         */
+        public animations = new Array<Animation>();
+        /**
+         * MultiMaterials to keep.
+         */
+        public multiMaterials = new Array<MultiMaterial>();
+        /**
+         * Materials to keep.
+         */
+        public materials = new Array<Material>();
+        /**
+         * MorphTargetManagers to keep.
+         */
+        public morphTargetManagers = new Array<MorphTargetManager>();
+        /**
+         * Geometries to keep.
+         */
+        public geometries = new Array<Geometry>();
+        /**
+         * TransformNodes to keep.
+         */
+        public transformNodes = new Array<TransformNode>();
+        /**
+         * LensFlareSystems to keep.
+         */
+        public lensFlareSystems = new Array<LensFlareSystem>();
+        /**
+         * ShadowGenerators to keep.
+         */
+        public shadowGenerators = new Array<ShadowGenerator>();
+        /**
+         * ActionManagers to keep.
+         */
+        public actionManagers = new Array<ActionManager>();
+        /**
+         * Sounds to keep.
+         */
+        public sounds = new Array<Sound>();
+    }
+
+    /**
+     * Container with a set of assets that can be added or removed from a scene.
+     */
     export class AssetContainer {
+        /**
+         * The scene the AssetContainer belongs to.
+         */
         public scene: Scene;
 
         // Objects
+        /**
+         * Cameras populated in the container.
+         */
         public cameras = new Array<Camera>();
+        /**
+         * Lights populated in the container.
+         */
         public lights = new Array<Light>();
+        /**
+         * Meshes populated in the container.
+         */
         public meshes = new Array<AbstractMesh>();
+        /**
+         * Skeletons populated in the container.
+         */
         public skeletons = new Array<Skeleton>();
+        /**
+         * ParticleSystems populated in the container.
+         */
         public particleSystems = new Array<ParticleSystem>();
+        /**
+         * Animations populated in the container.
+         */
         public animations = new Array<Animation>();
+        /**
+         * MultiMaterials populated in the container.
+         */
         public multiMaterials = new Array<MultiMaterial>();
+        /**
+         * Materials populated in the container.
+         */
         public materials = new Array<Material>();
+        /**
+         * MorphTargetManagers populated in the container.
+         */
         public morphTargetManagers = new Array<MorphTargetManager>();
+        /**
+         * Geometries populated in the container.
+         */
         public geometries = new Array<Geometry>();
+        /**
+         * TransformNodes populated in the container.
+         */
         public transformNodes = new Array<TransformNode>();
+        /**
+         * LensFlareSystems populated in the container.
+         */
         public lensFlareSystems = new Array<LensFlareSystem>();
+        /**
+         * ShadowGenerators populated in the container.
+         */
         public shadowGenerators = new Array<ShadowGenerator>();
+        /**
+         * ActionManagers populated in the container.
+         */
         public actionManagers = new Array<ActionManager>();
+        /**
+         * Sounds populated in the container.
+         */
         public sounds = new Array<Sound>();
         
+        /**
+         * Instantiates an AssetContainer.
+         * @param scene The scene the AssetContainer belongs to.
+         */
         constructor(scene:Scene){
             this.scene = scene;
         }
         
-        addAllToScene(){
+        /**
+         * Adds all the assets from the container to the scene.
+         */
+        public addAllToScene(){
             this.cameras.forEach((o)=>{
                 this.scene.addCamera(o);
             });
@@ -69,7 +193,11 @@ module BABYLON {
                 this.scene.mainSoundTrack.AddSound(o);
             })
         }
-        removeAllFromScene(){
+
+        /**
+         * Removes all the assets in the container from the scene
+         */
+        public removeAllFromScene(){
             this.cameras.forEach((o)=>{
                 this.scene.removeCamera(o);
             });
@@ -115,5 +243,50 @@ module BABYLON {
                 this.scene.mainSoundTrack.RemoveSound(o);
             })
         }
+        
+        private _moveAssets<T>(sourceAssets: T[], targetAssets: T[], keepAssets: T[]): void {
+            for (let asset of sourceAssets) {
+                let move = true;
+                for (let keepAsset of keepAssets) {
+                    if (asset === keepAsset) {
+                        move = false;
+                        break;
+                    }
+                }
+    
+                if (move) {
+                    targetAssets.push(asset);
+                }
+            }
+        }
+
+        /**
+         * Removes all the assets contained in the scene and adds them to the container.
+         * @param keepAssets Set of assets to keep in the scene. (default: empty)
+         */
+        public moveAllFromScene(keepAssets?: KeepAssets): void {
+
+            if (keepAssets === undefined) {
+                keepAssets = new KeepAssets();
+            }
+    
+            this._moveAssets(this.scene.cameras, this.cameras, keepAssets.cameras);
+            this._moveAssets(this.scene.meshes, this.meshes, keepAssets.meshes);
+            this._moveAssets(this.scene.getGeometries(), this.geometries, keepAssets.geometries);
+            this._moveAssets(this.scene.materials, this.materials, keepAssets.materials);
+    
+            Array.prototype.push.apply(this.actionManagers, this.scene._actionManagers);
+            Array.prototype.push.apply(this.animations, this.scene.animations);
+            Array.prototype.push.apply(this.lensFlareSystems, this.scene.lensFlareSystems);
+            Array.prototype.push.apply(this.lights, this.scene.lights);
+            Array.prototype.push.apply(this.morphTargetManagers, this.scene.morphTargetManagers);
+            Array.prototype.push.apply(this.multiMaterials, this.scene.multiMaterials);
+            Array.prototype.push.apply(this.skeletons, this.scene.skeletons);
+            Array.prototype.push.apply(this.particleSystems, this.scene.particleSystems);
+            Array.prototype.push.apply(this.sounds, this.scene.mainSoundTrack.soundCollection);
+            Array.prototype.push.apply(this.transformNodes, this.scene.transformNodes);
+    
+            this.removeAllFromScene();
+        }
     }
-}
+}

+ 2 - 2
tests/unit/babylon/babylonReference.ts

@@ -1,5 +1,5 @@
-/// <reference path="../../../dist/babylon.d.ts" />
-/// <reference path="../../../dist/loaders/babylon.glTF2FileLoader.d.ts" />
+/// <reference path="../../../dist/preview release/babylon.d.ts" />
+/// <reference path="../../../dist/preview release/loaders/babylon.glTF2FileLoader.d.ts" />
 /// <reference path="../../../dist/babylon.glTFInterface.d.ts"/>
 /// <reference path="../../../dist/preview release/serializers/babylon.glTF2Serializer.d.ts" />
 

+ 41 - 1
tests/unit/babylon/loading/babylon.sceneloader.tests.ts

@@ -1,7 +1,7 @@
 /**
  * Describes the test suite.
  */
-describe('Babylon Tools', () => {
+describe('Babylon SceneLoader', () => {
     var subject : BABYLON.Engine;
 
     /**
@@ -48,4 +48,44 @@ describe('Babylon Tools', () => {
             });
         });
     });
+
+    describe('#AssetContainer', () => {
+        it('should be loaded from BoomBox GLTF', (done) => {
+            var scene = new BABYLON.Scene(subject);
+            BABYLON.SceneLoader.LoadAssetContainer("/Playground/scenes/BoomBox/", "BoomBox.gltf", scene, function (container) {
+                expect(container.meshes.length).to.eq(2);
+                done();
+            });
+        });
+        it('should be adding and removing objects from scene', () => {
+            // Create a scene with some assets
+            var scene = new BABYLON.Scene(subject);
+            var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
+            var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
+            var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);
+            var ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);
+
+            // Move all the assets from the scene into a container
+            var container = new BABYLON.AssetContainer(scene);
+            var keepAssets = new BABYLON.KeepAssets();
+            keepAssets.cameras.push(camera)
+            container.moveAllFromScene(keepAssets)
+            expect(scene.cameras.length).to.eq(1)
+            expect(scene.meshes.length).to.eq(0)
+            expect(scene.lights.length).to.eq(0)
+            expect(container.cameras.length).to.eq(0)
+            expect(container.meshes.length).to.eq(2)
+            expect(container.lights.length).to.eq(1)
+
+            // Add them back and then remove again
+            container.addAllToScene();
+            expect(scene.cameras.length).to.eq(1)
+            expect(scene.meshes.length).to.eq(2)
+            expect(scene.lights.length).to.eq(1)
+            container.removeAllFromScene();
+            expect(scene.cameras.length).to.eq(1)
+            expect(scene.meshes.length).to.eq(0)
+            expect(scene.lights.length).to.eq(0)
+        });
+    });
 });