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

Small optimization for matrix interpolations

David Catuhe пре 7 година
родитељ
комит
cd6843c899
3 измењених фајлова са 27 додато и 3 уклоњено
  1. 10 1
      src/Animations/babylon.animation.ts
  2. 4 2
      src/Animations/babylon.runtimeAnimation.ts
  3. 13 0
      src/Math/babylon.math.ts

+ 10 - 1
src/Animations/babylon.animation.ts

@@ -456,10 +456,19 @@
             return Color3.Lerp(startValue, endValue, gradient);
             return Color3.Lerp(startValue, endValue, gradient);
         }
         }
 
 
-        public matrixInterpolateFunction(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
+        public matrixInterpolateFunction(startValue: Matrix, endValue: Matrix, gradient: number, result?: Matrix): Matrix {
             if (Animation.AllowMatrixDecomposeForInterpolation) {
             if (Animation.AllowMatrixDecomposeForInterpolation) {
+                if (result) {
+                    Matrix.DecomposeLerpToRef(startValue, endValue, gradient, result);    
+                    return result;
+                }
                 return Matrix.DecomposeLerp(startValue, endValue, gradient);
                 return Matrix.DecomposeLerp(startValue, endValue, gradient);
             }
             }
+
+            if (result) {
+                Matrix.LerpToRef(startValue, endValue, gradient, result);
+                return result;
+            }
             return Matrix.Lerp(startValue, endValue, gradient);
             return Matrix.Lerp(startValue, endValue, gradient);
         }
         }
 
 

+ 4 - 2
src/Animations/babylon.runtimeAnimation.ts

@@ -15,6 +15,7 @@
         private _scene: Scene;
         private _scene: Scene;
 
 
         private _currentValue: any;
         private _currentValue: any;
+        private _workValue: any;
         private _activeTarget: any;
         private _activeTarget: any;
         private _targetPath: string = "";
         private _targetPath: string = "";
         private _weight = 1.0;
         private _weight = 1.0;
@@ -172,7 +173,7 @@
                                 case Animation.ANIMATIONLOOPMODE_CONSTANT:
                                 case Animation.ANIMATIONLOOPMODE_CONSTANT:
                                     return quatValue;
                                     return quatValue;
                                 case Animation.ANIMATIONLOOPMODE_RELATIVE:
                                 case Animation.ANIMATIONLOOPMODE_RELATIVE:
-                                    return quatValue.add(offsetValue.scale(repeatCount));
+                                    return quatValue.addInPlace(offsetValue.scale(repeatCount));
                             }
                             }
 
 
                             return quatValue;
                             return quatValue;
@@ -220,7 +221,8 @@
                                 case Animation.ANIMATIONLOOPMODE_CYCLE:
                                 case Animation.ANIMATIONLOOPMODE_CYCLE:
                                 case Animation.ANIMATIONLOOPMODE_CONSTANT:
                                 case Animation.ANIMATIONLOOPMODE_CONSTANT:
                                     if (Animation.AllowMatricesInterpolation) {
                                     if (Animation.AllowMatricesInterpolation) {
-                                        return this._animation.matrixInterpolateFunction(startValue, endValue, gradient);
+                                        this._workValue = this._animation.matrixInterpolateFunction(startValue, endValue, gradient, this._workValue);
+                                        return this._workValue;
                                     }
                                     }
                                 case Animation.ANIMATIONLOOPMODE_RELATIVE:
                                 case Animation.ANIMATIONLOOPMODE_RELATIVE:
                                     return startValue;
                                     return startValue;

+ 13 - 0
src/Math/babylon.math.ts

@@ -3159,6 +3159,19 @@
         public add(other: Quaternion): Quaternion {
         public add(other: Quaternion): Quaternion {
             return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
             return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
         }
         }
+
+        /**
+         * Add a quaternion to the current one
+         * @param other defines the quaternion to add
+         * @returns the current quaternion  
+         */
+        public addInPlace(other: Quaternion): Quaternion {
+            this.x += other.x;
+            this.y += other.y;
+            this.z += other.z;
+            this.w += other.w;
+            return this;
+        }        
         /**
         /**
          * Returns a new Quaternion as the subtraction result of the passed one from the current Quaternion.  
          * Returns a new Quaternion as the subtraction result of the passed one from the current Quaternion.  
          */
          */