Browse Source

Animation's key can now be functions

David Catuhe 10 years ago
parent
commit
6a88b86417

+ 16 - 9
Babylon/Animations/babylon.animation.js

@@ -42,7 +42,7 @@
 
             mesh.animations.push(animation);
 
-            mesh.getScene().beginAnimation(mesh, 0, totalFrame, (animation.loopMode == 1));
+            mesh.getScene().beginAnimation(mesh, 0, totalFrame, (animation.loopMode === 1));
         };
 
         // Methods
@@ -96,6 +96,14 @@
             this._highLimitsCache = {};
         };
 
+        Animation.prototype._getKeyValue = function (value) {
+            if (typeof value === "function") {
+                return value();
+            }
+
+            return value;
+        };
+
         Animation.prototype._interpolate = function (currentFrame, repeatCount, loopMode, offsetValue, highLimitValue) {
             if (loopMode === Animation.ANIMATIONLOOPMODE_CONSTANT && repeatCount > 0) {
                 return highLimitValue.clone ? highLimitValue.clone() : highLimitValue;
@@ -106,8 +114,8 @@
             for (var key = 0; key < this._keys.length; key++) {
                 // for each frame, we need the key just before the frame superior
                 if (this._keys[key + 1].frame >= currentFrame) {
-                    var startValue = this._keys[key].value;
-                    var endValue = this._keys[key + 1].value;
+                    var startValue = this._getKeyValue(this._keys[key].value);
+                    var endValue = this._getKeyValue(this._keys[key + 1].value);
 
                     // gradient : percent of currentFrame between the frame inf and the frame sup
                     var gradient = (currentFrame - this._keys[key].frame) / (this._keys[key + 1].frame - this._keys[key].frame);
@@ -182,7 +190,7 @@
                     break;
                 }
             }
-            return this._keys[this._keys.length - 1].value;
+            return this._getKeyValue(this._keys[this._keys.length - 1].value);
         };
 
         Animation.prototype.animate = function (delay, from, to, loop, speedRatio) {
@@ -193,7 +201,7 @@
             var returnValue = true;
 
             // Adding a start key at frame 0 if missing
-            if (this._keys[0].frame != 0) {
+            if (this._keys[0].frame !== 0) {
                 var newKey = { frame: 0, value: this._keys[0].value };
                 this._keys.splice(0, 0, newKey);
             }
@@ -212,15 +220,14 @@
 
             // ratio represents the frame delta between from and to
             var ratio = delay * (this.framePerSecond * speedRatio) / 1000.0;
+            var highLimitValue = 0;
 
             if (ratio > range && !loop) {
                 returnValue = false;
-                highLimitValue = this._keys[this._keys.length - 1].value;
+                highLimitValue = this._getKeyValue(this._keys[this._keys.length - 1].value);
             } else {
                 // Get max value if required
-                var highLimitValue = 0;
-
-                if (this.loopMode != Animation.ANIMATIONLOOPMODE_CYCLE) {
+                if (this.loopMode !== Animation.ANIMATIONLOOPMODE_CYCLE) {
                     var keyOffset = to.toString() + from.toString();
                     if (!this._offsetsCache[keyOffset]) {
                         var fromValue = this._interpolate(from, 0, Animation.ANIMATIONLOOPMODE_CYCLE);

+ 26 - 20
Babylon/Animations/babylon.animation.ts

@@ -5,12 +5,12 @@
         private _highLimitsCache = {};
         private _stopped = false;
         public _target;
-        private _easingFunction: BABYLON.IEasingFunction;
+        private _easingFunction: IEasingFunction;
 
         public targetPropertyPath: string[];
         public currentFrame: number;
 
-        public static CreateAndStartAnimation(name: string, mesh: BABYLON.AbstractMesh, tartgetProperty: string,
+        public static CreateAndStartAnimation(name: string, mesh: AbstractMesh, tartgetProperty: string,
             framePerSecond: number, totalFrame: number,
             from: any, to: any, loopMode?: number) {
 
@@ -18,13 +18,13 @@
 
             if (!isNaN(parseFloat(from)) && isFinite(from)) {
                 dataType = Animation.ANIMATIONTYPE_FLOAT;
-            } else if (from instanceof BABYLON.Quaternion) {
+            } else if (from instanceof Quaternion) {
                 dataType = Animation.ANIMATIONTYPE_QUATERNION;
-            } else if (from instanceof BABYLON.Vector3) {
+            } else if (from instanceof Vector3) {
                 dataType = Animation.ANIMATIONTYPE_VECTOR3;
-            } else if (from instanceof BABYLON.Vector2) {
+            } else if (from instanceof Vector2) {
                 dataType = Animation.ANIMATIONTYPE_VECTOR2;
-            } else if (from instanceof BABYLON.Color3) {
+            } else if (from instanceof Color3) {
                 dataType = Animation.ANIMATIONTYPE_COLOR3;
             }
 
@@ -41,11 +41,10 @@
 
             mesh.animations.push(animation);
 
-            mesh.getScene().beginAnimation(mesh, 0, totalFrame, (animation.loopMode == 1));
+            mesh.getScene().beginAnimation(mesh, 0, totalFrame, (animation.loopMode === 1));
 
         }
 
-
         constructor(public name: string, public targetProperty: string, public framePerSecond: number, public dataType: number, public loopMode?: number) {
             this.targetPropertyPath = targetProperty.split(".");
             this.dataType = dataType;
@@ -65,7 +64,7 @@
             return this._easingFunction;
         }
 
-        public setEasingFunction(easingFunction: BABYLON.EasingFunction) {
+        public setEasingFunction(easingFunction: EasingFunction) {
             this._easingFunction = easingFunction;
         }
 
@@ -74,19 +73,19 @@
         }
 
         public quaternionInterpolateFunction(startValue: Quaternion, endValue: Quaternion, gradient: number): Quaternion {
-            return BABYLON.Quaternion.Slerp(startValue, endValue, gradient);
+            return Quaternion.Slerp(startValue, endValue, gradient);
         }
 
         public vector3InterpolateFunction(startValue: Vector3, endValue: Vector3, gradient: number): Vector3 {
-            return BABYLON.Vector3.Lerp(startValue, endValue, gradient);
+            return Vector3.Lerp(startValue, endValue, gradient);
         }
 
         public vector2InterpolateFunction(startValue: Vector2, endValue: Vector2, gradient: number): Vector2 {
-            return BABYLON.Vector2.Lerp(startValue, endValue, gradient);
+            return Vector2.Lerp(startValue, endValue, gradient);
         }
 
         public color3InterpolateFunction(startValue: Color3, endValue: Color3, gradient: number): Color3 {
-            return BABYLON.Color3.Lerp(startValue, endValue, gradient);
+            return Color3.Lerp(startValue, endValue, gradient);
         }
 
         public clone(): Animation {
@@ -103,6 +102,13 @@
             this._highLimitsCache = {};
         }
 
+        private _getKeyValue(value: any): any {
+            if (typeof value === "function") {
+                return value();
+            }
+
+            return value;
+        }
 
         private _interpolate(currentFrame: number, repeatCount: number, loopMode: number, offsetValue?, highLimitValue?) {
             if (loopMode === Animation.ANIMATIONLOOPMODE_CONSTANT && repeatCount > 0) {
@@ -115,8 +121,8 @@
                 // for each frame, we need the key just before the frame superior
                 if (this._keys[key + 1].frame >= currentFrame) {
 
-                    var startValue = this._keys[key].value;
-                    var endValue = this._keys[key + 1].value;
+                    var startValue = this._getKeyValue(this._keys[key].value);
+                    var endValue = this._getKeyValue(this._keys[key + 1].value);
 
                     // gradient : percent of currentFrame between the frame inf and the frame sup
                     var gradient = (currentFrame - this._keys[key].frame) / (this._keys[key + 1].frame - this._keys[key].frame);
@@ -192,7 +198,7 @@
                     break;
                 }
             }
-            return this._keys[this._keys.length - 1].value;
+            return this._getKeyValue(this._keys[this._keys.length - 1].value);
         }
 
 
@@ -204,7 +210,7 @@
             var returnValue = true;
 
             // Adding a start key at frame 0 if missing
-            if (this._keys[0].frame != 0) {
+            if (this._keys[0].frame !== 0) {
                 var newKey = { frame: 0, value: this._keys[0].value };
                 this._keys.splice(0, 0, newKey);
             }
@@ -222,15 +228,15 @@
             var offsetValue;
             // ratio represents the frame delta between from and to
             var ratio = delay * (this.framePerSecond * speedRatio) / 1000.0;
+            var highLimitValue = 0;
 
             if (ratio > range && !loop) { // If we are out of range and not looping get back to caller
                 returnValue = false;
-                highLimitValue = this._keys[this._keys.length - 1].value;
+                highLimitValue = this._getKeyValue(this._keys[this._keys.length - 1].value);
             } else {
                 // Get max value if required
-                var highLimitValue = 0;
 
-                if (this.loopMode != Animation.ANIMATIONLOOPMODE_CYCLE) {
+                if (this.loopMode !== Animation.ANIMATIONLOOPMODE_CYCLE) {
 
                     var keyOffset = to.toString() + from.toString();
                     if (!this._offsetsCache[keyOffset]) {

+ 16 - 9
babylon.2.0-alpha.debug.js

@@ -17539,7 +17539,7 @@ var BABYLON;
 
             mesh.animations.push(animation);
 
-            mesh.getScene().beginAnimation(mesh, 0, totalFrame, (animation.loopMode == 1));
+            mesh.getScene().beginAnimation(mesh, 0, totalFrame, (animation.loopMode === 1));
         };
 
         // Methods
@@ -17593,6 +17593,14 @@ var BABYLON;
             this._highLimitsCache = {};
         };
 
+        Animation.prototype._getKeyValue = function (value) {
+            if (typeof value === "function") {
+                return value();
+            }
+
+            return value;
+        };
+
         Animation.prototype._interpolate = function (currentFrame, repeatCount, loopMode, offsetValue, highLimitValue) {
             if (loopMode === Animation.ANIMATIONLOOPMODE_CONSTANT && repeatCount > 0) {
                 return highLimitValue.clone ? highLimitValue.clone() : highLimitValue;
@@ -17603,8 +17611,8 @@ var BABYLON;
             for (var key = 0; key < this._keys.length; key++) {
                 // for each frame, we need the key just before the frame superior
                 if (this._keys[key + 1].frame >= currentFrame) {
-                    var startValue = this._keys[key].value;
-                    var endValue = this._keys[key + 1].value;
+                    var startValue = this._getKeyValue(this._keys[key].value);
+                    var endValue = this._getKeyValue(this._keys[key + 1].value);
 
                     // gradient : percent of currentFrame between the frame inf and the frame sup
                     var gradient = (currentFrame - this._keys[key].frame) / (this._keys[key + 1].frame - this._keys[key].frame);
@@ -17679,7 +17687,7 @@ var BABYLON;
                     break;
                 }
             }
-            return this._keys[this._keys.length - 1].value;
+            return this._getKeyValue(this._keys[this._keys.length - 1].value);
         };
 
         Animation.prototype.animate = function (delay, from, to, loop, speedRatio) {
@@ -17690,7 +17698,7 @@ var BABYLON;
             var returnValue = true;
 
             // Adding a start key at frame 0 if missing
-            if (this._keys[0].frame != 0) {
+            if (this._keys[0].frame !== 0) {
                 var newKey = { frame: 0, value: this._keys[0].value };
                 this._keys.splice(0, 0, newKey);
             }
@@ -17709,15 +17717,14 @@ var BABYLON;
 
             // ratio represents the frame delta between from and to
             var ratio = delay * (this.framePerSecond * speedRatio) / 1000.0;
+            var highLimitValue = 0;
 
             if (ratio > range && !loop) {
                 returnValue = false;
-                highLimitValue = this._keys[this._keys.length - 1].value;
+                highLimitValue = this._getKeyValue(this._keys[this._keys.length - 1].value);
             } else {
                 // Get max value if required
-                var highLimitValue = 0;
-
-                if (this.loopMode != Animation.ANIMATIONLOOPMODE_CYCLE) {
+                if (this.loopMode !== Animation.ANIMATIONLOOPMODE_CYCLE) {
                     var keyOffset = to.toString() + from.toString();
                     if (!this._offsetsCache[keyOffset]) {
                         var fromValue = this._interpolate(from, 0, Animation.ANIMATIONLOOPMODE_CYCLE);

File diff suppressed because it is too large
+ 1 - 1
babylon.2.0-alpha.js