Przeglądaj źródła

Bone.setScale now works while animation is running

Adam Bowman 8 lat temu
rodzic
commit
67a94eaab7
1 zmienionych plików z 38 dodań i 7 usunięć
  1. 38 7
      src/Bones/babylon.bone.ts

+ 38 - 7
src/Bones/babylon.bone.ts

@@ -15,6 +15,7 @@
 
         private _scaleMatrix: Matrix = BABYLON.Matrix.Identity();
         private _scaleVector: Vector3 = new BABYLON.Vector3(1, 1, 1);
+        private _negateScaleChildren = new BABYLON.Vector3(1, 1, 1);
         
         constructor(public name: string, skeleton: Skeleton, parentBone: Bone, matrix: Matrix, restPose?: Matrix) {
             super(name, skeleton.getScene());
@@ -218,6 +219,15 @@
 
         public setScale (x: number, y: number, z: number, scaleChildren = false): void {
 
+            if (this.animations[0] && !this.animations[0].isStopped()) {
+                if (!scaleChildren) {
+                    this._negateScaleChildren.x = 1/x;
+                    this._negateScaleChildren.y = 1/y;
+                    this._negateScaleChildren.z = 1/z;
+                }
+                this._syncScaleVector();
+            }
+
 	        this.scale(x / this._scaleVector.x, y / this._scaleVector.y, z / this._scaleVector.z, scaleChildren);
 
         }
@@ -253,15 +263,10 @@
 
             var len = this.children.length;
 
-            for (var i = 0; i < len; i++) {
-                var parentAbsMat = this.children[i]._parent.getAbsoluteTransform();
-                this.children[i].getLocalMatrix().multiplyToRef(parentAbsMat, this.children[i].getAbsoluteTransform());
-            }
-
             scaleMat.invert();
             
-            if (this.children[0]) {
-                var child = this.children[0];
+            for (var i = 0; i < len; i++) {
+                var child = this.children[i];
                 var cm = child.getLocalMatrix();
                 cm.multiplyToRef(scaleMat, cm);
                 var lm = child.getLocalMatrix();
@@ -269,6 +274,8 @@
                 lm.m[13] *= y;
                 lm.m[14] *= z;
             }
+
+            this.computeAbsoluteTransforms();
             
             if (scaleChildren) {
                 for (var i = 0; i < len; i++) {
@@ -460,5 +467,29 @@
 
         }
 
+        private _syncScaleVector = function(): void{
+            
+            var lm = this.getLocalMatrix();
+            
+            var xsq = (lm.m[0] * lm.m[0] + lm.m[1] * lm.m[1] + lm.m[2] * lm.m[2]);
+            var ysq = (lm.m[4] * lm.m[4] + lm.m[5] * lm.m[5] + lm.m[6] * lm.m[6]);
+            var zsq = (lm.m[8] * lm.m[8] + lm.m[9] * lm.m[9] + lm.m[10] * lm.m[10]);
+            
+            var xs = lm.m[0] * lm.m[1] * lm.m[2] * lm.m[3] < 0 ? -1 : 1;
+            var ys = lm.m[4] * lm.m[5] * lm.m[6] * lm.m[7] < 0 ? -1 : 1;
+            var zs = lm.m[8] * lm.m[9] * lm.m[10] * lm.m[11] < 0 ? -1 : 1;
+            
+            this._scaleVector.x = xs * Math.sqrt(xsq);
+            this._scaleVector.y = ys * Math.sqrt(ysq);
+            this._scaleVector.z = zs * Math.sqrt(zsq);
+            
+            if (this._parent) {
+                this._scaleVector.x /= this._parent._negateScaleChildren.x;
+                this._scaleVector.y /= this._parent._negateScaleChildren.y;
+                this._scaleVector.z /= this._parent._negateScaleChildren.z;
+            }
+
+        }
+
     }
 }