浏览代码

Animation is working - still not quite right

Michael Dahrea 8 年之前
父节点
当前提交
bfb1e078c8
共有 2 个文件被更改,包括 34 次插入14 次删除
  1. 30 14
      loaders/src/glTF/2.0/babylon.glTFLoader.ts
  2. 4 0
      src/Morph/babylon.morphTargetManager.ts

+ 30 - 14
loaders/src/glTF/2.0/babylon.glTFLoader.ts

@@ -80,7 +80,7 @@ module BABYLON.GLTF2 {
                 }
 
                 var isBone = targetNode instanceof Bone;
-                var numInfluencers = 0;
+                var numTargets = 0;
 
                 // Get target path (position, rotation, scaling, or weights)
                 var targetPath = channel.target.path;
@@ -90,6 +90,8 @@ module BABYLON.GLTF2 {
                     targetPath = babylonAnimationPaths[targetPathIndex];
                 }
 
+                var isMorph = targetPath === "influence";
+
                 // Determine animation type
                 var animationType = Animation.ANIMATIONTYPE_MATRIX;
 
@@ -98,9 +100,9 @@ module BABYLON.GLTF2 {
                         animationType = Animation.ANIMATIONTYPE_QUATERNION;
                         targetNode.rotationQuaternion = new Quaternion();
                     }
-                    else if (targetPath === "influence") {
+                    else if (isMorph) {
                         animationType = Animation.ANIMATIONTYPE_FLOAT;
-                        numInfluencers = (<Mesh>targetNode).morphTargetManager.numInfluencers;
+                        numTargets = (<Mesh>targetNode).morphTargetManager.numTargets;
                     }
                     else {
                         animationType = Animation.ANIMATIONTYPE_VECTOR3;
@@ -118,6 +120,14 @@ module BABYLON.GLTF2 {
                     modifyKey = true;
                 }
 
+                // Each morph animation may have more than one more, so we need a
+                // multi dimensional array.
+                if (isMorph) {
+                    for (var influence = 0; influence < numTargets; influence++) {
+                        keys[influence] = [];
+                    }
+                }
+
                 // For each frame
                 for (var frameIndex = 0; frameIndex < bufferInput.length; frameIndex++) {
                     var value: any = null;
@@ -126,12 +136,13 @@ module BABYLON.GLTF2 {
                         value = Quaternion.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2], bufferOutput[arrayOffset + 3]]);
                         arrayOffset += 4;
                     }
-                    else if (targetPath === "influence") { // FLOAT
+                    else if (isMorph) { // FLOAT
+                        value = [];
                         // There is 1 value for each morph target for each frame
-                        for (var influence = 0; influence < numInfluencers; influence++) {
+                        for (var influence = 0; influence < numTargets; influence++) {
                             value.push(bufferOutput[arrayOffset + influence]);
                         }
-                        arrayOffset += numInfluencers;
+                        arrayOffset += numTargets;
                     }
                     else { // Position and scaling are VEC3
                         value = Vector3.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2]]);
@@ -167,8 +178,8 @@ module BABYLON.GLTF2 {
                     }
 
                     if (!modifyKey) {
-                        if (targetPath === "influence") {
-                            for (var influence = 0; influence < numInfluencers; influence++) {
+                        if (isMorph) {
+                            for (var influence = 0; influence < numTargets; influence++) {
                                 keys[influence].push({
                                     frame: bufferInput[frameIndex],
                                     value: value[influence]
@@ -189,13 +200,18 @@ module BABYLON.GLTF2 {
 
                 // Finish
                 if (!modifyKey) {
-                    if (targetPath === "influence") {
-                        for (var influence = 0; influence < numInfluencers; influence++) {
+                    if (isMorph) {
+                        for (var influence = 0; influence < numTargets; influence++) {
+                            var morphTarget = (<Mesh>targetNode).morphTargetManager.getTarget(influence);
+                            if ((<any>morphTarget).animations === undefined) {
+                                (<any>morphTarget).animations = [];
+                            }
+
                             var animationName = (animation.name || "anim" + animationIndex) + "_" + influence;
                             babylonAnimation = new Animation(animationName, targetPath, 1, animationType, Animation.ANIMATIONLOOPMODE_CYCLE);
 
                             babylonAnimation.setKeys(keys[influence]);
-                            (<any>(<Mesh>targetNode).morphTargetManager.getActiveTarget(influence)).animations.push(babylonAnimation);
+                            (<any>morphTarget).animations.push(babylonAnimation);
                         }
                     }
                     else {
@@ -209,9 +225,9 @@ module BABYLON.GLTF2 {
 
                 lastAnimation = babylonAnimation;
 
-                if (targetPath === "influence") {
-                    for (var influence = 0; influence < numInfluencers; influence++) {
-                        var morph = (<Mesh>targetNode).morphTargetManager.getActiveTarget(influence);
+                if (isMorph) {
+                    for (var influence = 0; influence < numTargets; influence++) {
+                        var morph = (<Mesh>targetNode).morphTargetManager.getTarget(influence);
                         runtime.babylonScene.stopAnimation(morph);
                         runtime.babylonScene.beginAnimation(morph, 0, bufferInput[bufferInput.length - 1], true, 1.0);
                     }

+ 4 - 0
src/Morph/babylon.morphTargetManager.ts

@@ -53,6 +53,10 @@ module BABYLON {
         public getActiveTarget(index: number): MorphTarget {
             return this._activeTargets.data[index];
         }
+
+        public getTarget(index: number): MorphTarget {
+            return this._targets[index];
+        }
        
         public addTarget(target: MorphTarget): void {
             if (this._vertexCount) {