Преглед изворни кода

Creating a method to compose matrices with given translation, rotation and scale

kpko пре 10 година
родитељ
комит
682a525c9e

+ 2 - 8
Babylon/Animations/babylon.animation.js

@@ -97,15 +97,9 @@
             var resultRotation = this.quaternionInterpolateFunction(startRotation, endRotation, gradient);
             var resultTranslation = this.vector3InterpolateFunction(startTranslation, endTranslation, gradient);
 
-            var m = BABYLON.Matrix.FromValues(resultScale.x, 0, 0, 0, 0, resultScale.y, 0, 0, 0, 0, resultScale.z, 0, 0, 0, 0, 1);
+            var result = BABYLON.Matrix.Compose(resultScale, resultRotation, resultTranslation);
 
-            var rotationMatrix = BABYLON.Matrix.Identity();
-            resultRotation.toRotationMatrix(rotationMatrix);
-            m = m.multiply(rotationMatrix);
-
-            m.setTranslation(resultTranslation);
-
-            return m;
+            return result;
         };
 
         Animation.prototype.clone = function () {

+ 2 - 11
Babylon/Animations/babylon.animation.ts

@@ -103,18 +103,9 @@
             var resultRotation = this.quaternionInterpolateFunction(startRotation, endRotation, gradient);
             var resultTranslation = this.vector3InterpolateFunction(startTranslation, endTranslation, gradient);
 
-            var m = Matrix.FromValues(resultScale.x, 0, 0, 0,
-                0, resultScale.y, 0, 0,
-                0, 0, resultScale.z, 0,
-                0, 0, 0, 1);
+            var result = Matrix.Compose(resultScale, resultRotation, resultTranslation);
 
-            var rotationMatrix = Matrix.Identity();
-            resultRotation.toRotationMatrix(rotationMatrix);
-            m = m.multiply(rotationMatrix);
-
-            m.setTranslation(resultTranslation);
-
-            return m;
+            return result;
         }
 
         public clone(): Animation {

+ 12 - 2
Babylon/Math/babylon.math.js

@@ -1690,8 +1690,6 @@
             var ys = BABYLON.Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
             var zs = BABYLON.Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
 
-            debugger;
-
             scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
             scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
             scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
@@ -1772,6 +1770,18 @@
             return result;
         };
 
+        Matrix.Compose = function (scale, rotation, translation) {
+            var result = Matrix.FromValues(scale.x, 0, 0, 0, 0, scale.y, 0, 0, 0, 0, scale.z, 0, 0, 0, 0, 1);
+
+            var rotationMatrix = Matrix.Identity();
+            rotation.toRotationMatrix(rotationMatrix);
+            result = result.multiply(rotationMatrix);
+
+            result.setTranslation(translation);
+
+            return result;
+        };
+
         Matrix.Identity = function () {
             return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
         };

+ 17 - 3
Babylon/Math/babylon.math.ts

@@ -1666,8 +1666,6 @@
             var ys = Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
             var zs = Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
 
-            debugger;
-
             scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
             scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
             scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
@@ -1680,7 +1678,8 @@
                 return false;
             }
 
-            var rotationMatrix = BABYLON.Matrix.FromValues(this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0,
+            var rotationMatrix = BABYLON.Matrix.FromValues(
+                this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0,
                 this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0,
                 this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0,
                 0, 0, 0, 1);
@@ -1760,6 +1759,21 @@
             return result;
         }
 
+        public static Compose(scale: Vector3, rotation: Quaternion, translation: Vector3): Matrix {
+            var result = Matrix.FromValues(scale.x, 0, 0, 0,
+                0, scale.y, 0, 0,
+                0, 0, scale.z, 0,
+                0, 0, 0, 1);
+
+            var rotationMatrix = Matrix.Identity();
+            rotation.toRotationMatrix(rotationMatrix);
+            result = result.multiply(rotationMatrix);
+
+            result.setTranslation(translation);
+
+            return result;
+        }
+
         public static Identity(): Matrix {
             return Matrix.FromValues(1.0, 0, 0, 0,
                 0, 1.0, 0, 0,