Kacey Coley 7 роки тому
батько
коміт
075771f7c0

+ 4 - 12
serializers/src/glTF/2.0/babylon.glTFAnimation.ts

@@ -107,8 +107,8 @@ module BABYLON.GLTF2 {
                     inputs: inputs,
                     outputs: outputs,
                     samplerInterpolation: interpolation,
-                    inputsMin: shouldBakeAnimation ? minMaxKeyFrames.min : this.fround(minMaxKeyFrames.min / animation.framePerSecond),
-                    inputsMax: shouldBakeAnimation ? minMaxKeyFrames.max : this.fround(minMaxKeyFrames.max / animation.framePerSecond)
+                    inputsMin: shouldBakeAnimation ? minMaxKeyFrames.min : Tools.FloatRound(minMaxKeyFrames.min / animation.framePerSecond),
+                    inputsMax: shouldBakeAnimation ? minMaxKeyFrames.max : Tools.FloatRound(minMaxKeyFrames.max / animation.framePerSecond)
                 }
 
                 return result;
@@ -117,14 +117,6 @@ module BABYLON.GLTF2 {
             return null;
         }
 
-        /**
-         * Math.fround is not supported in IE or PhantomJS, so this helper function is used instead
-         * @param num value to fround
-         */
-        private static fround(num: number) {
-            return new Float32Array([num])[0];
-        }
-
         private static _DeduceAnimationInfo(animation: Animation): Nullable<_IAnimationInfo> {
             let animationChannelTargetPath: Nullable<AnimationChannelTargetPath> = null;
             let dataAccessorType = AccessorType.VEC3;
@@ -348,7 +340,7 @@ module BABYLON.GLTF2 {
             let nextKeyFrame: Nullable<IAnimationKey> = null;
             let prevKeyFrame: Nullable<IAnimationKey> = null;
             let endFrame: Nullable<number> = null;
-            minMaxFrames.min = this.fround(minFrame / fps);
+            minMaxFrames.min = Tools.FloatRound(minFrame / fps);
 
             let keyFrames = animation.getKeys();
 
@@ -382,7 +374,7 @@ module BABYLON.GLTF2 {
                 }
                 if (endFrame) {
                     for (let f = currKeyFrame.frame; f <= endFrame; f += sampleRate) {
-                        time = this.fround(f / fps);
+                        time = Tools.FloatRound(f / fps);
                         if (time === previousTime) {
                             continue;
                         }

+ 16 - 2
src/Tools/babylon.tools.ts

@@ -146,6 +146,21 @@
             return count === value;
         }
 
+        private static _tmpFloatArray = new Float32Array(1);
+        /**
+         * Returns the nearest 32-bit single precision float representation of a Number
+         * @param value A Number.  IF the parameter is of a different type, it will get converted
+         * to a number or to NAN if it cannot be converted
+         * @returns number
+         */
+        public static FloatRound(value: number): number {
+            if (Math.fround) {
+                return Math.fround(value);
+            }
+
+            return (Tools._tmpFloatArray[0] = value);
+        }
+
 		/**
 		 * Find the next highest power of two.
 		 * @param x Number to start search from.
@@ -824,8 +839,7 @@
                     continue;
                 }
 
-                try
-                {
+                try {
                     if (typeOfSourceValue === "object") {
                         if (sourceValue instanceof Array) {
                             destination[prop] = [];