Parcourir la source

Allow bone matrix and parent to be set outside of constructor

Gary Hsu il y a 8 ans
Parent
commit
ac25d504e5
1 fichiers modifiés avec 39 ajouts et 19 suppressions
  1. 39 19
      src/Bones/babylon.bone.ts

+ 39 - 19
src/Bones/babylon.bone.ts

@@ -25,46 +25,64 @@ module BABYLON {
         private _negateScaleChildren = Vector3.One();
         private _scalingDeterminant = 1;
 
-        get _matrix():Matrix{
+        get _matrix(): Matrix {
             return this._localMatrix;
         }
 
-        set _matrix(val:Matrix){
-            if(this._localMatrix){
+        set _matrix(val: Matrix) {
+            if (this._localMatrix) {
                 this._localMatrix.copyFrom(val);
-            }else{
+            } else {
                 this._localMatrix = val;
             }
         }
-        
-        constructor(public name: string, skeleton: Skeleton, parentBone: Bone, matrix: Matrix, restPose?: Matrix) {
+
+        constructor(public name: string, skeleton: Skeleton, parentBone: Bone = null, matrix?: Matrix, restPose?: Matrix) {
             super(name, skeleton.getScene());
             this._skeleton = skeleton;
-            this._localMatrix = matrix;
-            this._baseMatrix = matrix.clone();
-            this._restPose = restPose ? restPose : matrix.clone();
+            this._localMatrix = matrix ? matrix : Matrix.Identity();
+            this._baseMatrix = this._localMatrix.clone();
+            this._restPose = restPose ? restPose : this._localMatrix.clone();
 
             skeleton.bones.push(this);
 
-            if (parentBone) {
-                this._parent = parentBone;
-                parentBone.children.push(this);
-            } else {
-                this._parent = null;
-            }
+            this.setParent(parentBone, false);
 
             this._updateDifferenceMatrix();
-
-            if (this.getAbsoluteTransform().determinant() < 0) {
-                this._scalingDeterminant *= -1;
-            }
         }
 
         // Members
+        public getSkeleton(): Skeleton {
+            return this._skeleton;
+        }
+
         public getParent(): Bone {
             return this._parent;
         }
 
+        public setParent(parent: Bone, updateDifferenceMatrix: boolean = true): void {
+            if (this._parent === parent) {
+                return;
+            }
+
+            if (this._parent) {
+                var index = this._parent.children.indexOf(this);
+                if (index !== -1) {
+                    this._parent.children.splice(index);
+                }
+            }
+
+            this._parent = parent;
+
+            if (this._parent) {
+                this._parent.children.push(this);
+            }
+
+            if (updateDifferenceMatrix) {
+                this._updateDifferenceMatrix();
+            }
+        }
+
         public getLocalMatrix(): Matrix {
             return this._localMatrix;
         }
@@ -154,6 +172,8 @@ module BABYLON {
             for (var index = 0; index < this.children.length; index++) {
                 this.children[index]._updateDifferenceMatrix();
             }
+
+            this._scalingDeterminant = (this._absoluteTransform.determinant() < 0 ? -1 : 1);
         }
 
         public markAsDirty(): void {