浏览代码

Updating getChildren confusion

Node now holds all getChildren functions.
getChildren is internally using the alread-implemented getDecendants
function.
the getChildMeshes was also moved to the node and has now a "direct
decendants only" variable for direct children only.
Raanan Weber 9 年之前
父节点
当前提交
0b9a2eb209

+ 0 - 32
src/Mesh/babylon.abstractMesh.ts

@@ -735,38 +735,6 @@
             return this._boundingInfo.intersectsPoint(point);
             return this._boundingInfo.intersectsPoint(point);
         }
         }
 
 
-        public getChildMeshes(): AbstractMesh[] {
-            return this.getScene().meshes.filter((m) => {
-                return m.parent === this;
-            });
-        }
-
-        public getChildren(): Node[] {
-            var results = [];
-            for (var index = 0; index < this.getScene().meshes.length; index++) {
-                var mesh = this.getScene().meshes[index];
-                if (mesh.parent === this) {
-                    results.push(mesh);
-                }
-            }
-
-            for (var index = 0; index < this.getScene().lights.length; index++) {
-                var light = this.getScene().lights[index];
-                if (light.parent === this) {
-                    results.push(mesh);
-                }
-            }
-
-            for (var index = 0; index < this.getScene().cameras.length; index++) {
-                var camera = this.getScene().cameras[index];
-                if (camera.parent === this) {
-                    results.push(mesh);
-                }
-            }
-
-            return results;
-        }
-
         // Physics
         // Physics
         /**
         /**
          *  @Deprecated. Use new PhysicsImpostor instead.
          *  @Deprecated. Use new PhysicsImpostor instead.

+ 4 - 4
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -103,14 +103,14 @@
         }
         }
 
 
         private _processChildMeshes(mainImpostor: PhysicsImpostor) {
         private _processChildMeshes(mainImpostor: PhysicsImpostor) {
-            var meshChildren = mainImpostor.mesh.getChildMeshes();
+            var meshChildren = mainImpostor.mesh.getChildMeshes(true);
             if (meshChildren.length) {
             if (meshChildren.length) {
-                var processMesh = (localPosition: Vector3, mesh: AbstractMesh) => {
+                var processMesh = (relativePosition: Vector3, mesh: AbstractMesh) => {
                     var childImpostor = mesh.getPhysicsImpostor();
                     var childImpostor = mesh.getPhysicsImpostor();
                     if (childImpostor) {
                     if (childImpostor) {
                         var parent = childImpostor.parent;
                         var parent = childImpostor.parent;
                         if (parent !== mainImpostor) {
                         if (parent !== mainImpostor) {
-                            var localPosition = mesh.position;
+                            var localPosition = mesh.position.add(relativePosition);
                             if (childImpostor.physicsBody) {
                             if (childImpostor.physicsBody) {
                                 this.removePhysicsBody(childImpostor);
                                 this.removePhysicsBody(childImpostor);
                                 childImpostor.physicsBody = null;
                                 childImpostor.physicsBody = null;
@@ -122,7 +122,7 @@
                             mainImpostor.physicsBody.mass += childImpostor.getParam("mass");
                             mainImpostor.physicsBody.mass += childImpostor.getParam("mass");
                         }
                         }
                     }
                     }
-                    mesh.getChildMeshes().forEach(processMesh.bind(this, mesh.position));
+                    mesh.getChildMeshes(true).forEach(processMesh.bind(this, mesh.position));
                 }
                 }
                 meshChildren.forEach(processMesh.bind(this, Vector3.Zero()));
                 meshChildren.forEach(processMesh.bind(this, Vector3.Zero()));
             }
             }

+ 0 - 1
src/Physics/Plugins/babylon.oimoJSPlugin.ts

@@ -104,7 +104,6 @@ module BABYLON {
                             impostors.push(m.physicsImpostor);
                             impostors.push(m.physicsImpostor);
                             m.physicsImpostor._init();
                             m.physicsImpostor._init();
                         }
                         }
-                        addToArray(m);
                     });
                     });
                 }
                 }
                 addToArray(impostor.mesh)
                 addToArray(impostor.mesh)

+ 23 - 7
src/babylon.node.ts

@@ -171,16 +171,15 @@
                     return true;
                     return true;
                 }
                 }
 
 
-
                 return this.parent.isDescendantOf(ancestor);
                 return this.parent.isDescendantOf(ancestor);
             }
             }
             return false;
             return false;
         }
         }
 
 
-        public _getDescendants(list: Node[], results: Node[]): void {
+        public _getDescendants(list: Node[], results: Node[], directDecendantsOnly: boolean = false): void {
             for (var index = 0; index < list.length; index++) {
             for (var index = 0; index < list.length; index++) {
                 var item = list[index];
                 var item = list[index];
-                if (item.isDescendantOf(this)) {
+                if((directDecendantsOnly && item.parent === this) || (!directDecendantsOnly && item.isDescendantOf(this))) {
                     results.push(item);
                     results.push(item);
                 }
                 }
             }
             }
@@ -190,14 +189,31 @@
          * Will return all nodes that have this node as parent.
          * Will return all nodes that have this node as parent.
          * @return {BABYLON.Node[]} all children nodes of all types.
          * @return {BABYLON.Node[]} all children nodes of all types.
          */
          */
-        public getDescendants(): Node[] {
+        public getDescendants(directDecendantsOnly?: boolean): Node[] {
             var results = [];
             var results = [];
-            this._getDescendants(this._scene.meshes, results);
-            this._getDescendants(this._scene.lights, results);
-            this._getDescendants(this._scene.cameras, results);
+            this._getDescendants(this._scene.meshes, results, directDecendantsOnly);
+            this._getDescendants(this._scene.lights, results, directDecendantsOnly);
+            this._getDescendants(this._scene.cameras, results, directDecendantsOnly);
 
 
             return results;
             return results;
         }
         }
+        
+        /**
+         * @Deprecated, legacy support.
+         * use getDecendants instead.
+         */
+        public getChildren(): Node[] {
+            return this.getDescendants(true);
+        }
+        
+        /**
+         * Get all child-meshes of this node.
+         */
+        public getChildMeshes(directDecendantsOnly?: boolean): AbstractMesh[] {
+            var results : Array<AbstractMesh> = [];
+            this._getDescendants(this._scene.meshes, results, false);
+            return results;
+        }
 
 
         public _setReady(state: boolean): void {
         public _setReady(state: boolean): void {
             if (state === this._isReady) {
             if (state === this._isReady) {