瀏覽代碼

Merge pull request #1660 from abow/master

added AbstractMesh addChild, removeChild, setParent with option to ke…
David Catuhe 8 年之前
父節點
當前提交
3c47578509
共有 1 個文件被更改,包括 87 次插入0 次删除
  1. 87 0
      src/Mesh/babylon.abstractMesh.ts

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

@@ -1277,6 +1277,93 @@
 
         }
 
+        public setParent(mesh:AbstractMesh, keepWorldPositionRotation = false): void{
+
+            mesh.addChild(this, keepWorldPositionRotation);
+
+        }
+
+        public addChild(mesh:AbstractMesh, keepWorldPositionRotation = false): void{
+
+            var child = mesh;
+            var parent = this;
+
+            if(keepWorldPositionRotation){
+                
+                var rotation = Tmp.Quaternion[0];
+                var position = Tmp.Vector3[0];
+                var scale = Tmp.Vector3[1];
+                var m1 = Tmp.Matrix[0];
+                var m2 = Tmp.Matrix[1];
+
+                parent.getWorldMatrix().decompose(scale, rotation, position);
+
+                rotation.toRotationMatrix(m1);
+                m2.setTranslation(position);
+
+                m2.multiplyToRef(m1, m1);
+
+                var invParentMatrix = Matrix.Invert(m1);
+
+                var m = child.getWorldMatrix().multiply(invParentMatrix);
+
+                m.decompose(scale, rotation, position);
+
+                if (child.rotationQuaternion) {
+                    child.rotationQuaternion.copyFrom(rotation);
+                } else {
+                    rotation.toEulerAnglesToRef(child.rotation);
+                }
+
+                invParentMatrix = Matrix.Invert(parent.getWorldMatrix());
+
+                var m = child.getWorldMatrix().multiply(invParentMatrix);
+
+                m.decompose(Vector3.Zero(), Quaternion.Identity(), position);
+
+                child.position.x = position.x;
+                child.position.y = position.y;
+                child.position.z = position.z;
+
+            }
+
+            child.parent = parent;
+
+        }
+
+        public removeChild(mesh:AbstractMesh, keepWorldPositionRotation = false): void{
+
+            var child = mesh;
+            var parent = this;
+            
+            if (!child.parent) {
+                return;
+            }
+
+            if(keepWorldPositionRotation){
+
+                var rotation = Tmp.Quaternion[0];
+                var position = Tmp.Vector3[0];
+                var m1 = Tmp.Matrix[0];
+
+                child.getWorldMatrix().decompose(Vector3.Zero(), rotation, position);
+
+                if (child.rotationQuaternion) {
+                    child.rotationQuaternion.copyFrom(rotation);
+                } else {
+                    rotation.toEulerAnglesToRef(child.rotation);
+                }
+
+                child.position.x = position.x;
+                child.position.y = position.y;
+                child.position.z = position.z;
+
+            }
+
+            child.parent = null;
+
+        }
+
         public getAbsolutePivotPointToRef(result:Vector3): void{
 
             result.x = this._pivotMatrix.m[12];