Bläddra i källkod

* Fix animation blending from bind pose issue
* Fix look/cycle modes for animations to prevent result time from being out of range

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9262 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

Sha..rd 13 år sedan
förälder
incheckning
2cdeb6ebff

+ 55 - 29
engine/src/core/com/jme3/animation/AnimChannel.java

@@ -68,31 +68,53 @@ public final class AnimChannel {
     private float blendRate   = 0;
 
     private static float clampWrapTime(float t, float max, LoopMode loopMode){
-        if (max == Float.POSITIVE_INFINITY)
-            return t;
+        if (t == 0) {
+            return 0; // prevent division by 0 errors
+        }
         
-        if (t < 0f){
-            //float tMod = -(-t % max);
-            switch (loopMode){
-                case DontLoop:
-                    return 0;
-                case Cycle:
-                    return t;
-                case Loop:
-                    return max - t;
-            }
-        }else if (t > max){
-            switch (loopMode){
-                case DontLoop:
-                    return max;
-                case Cycle:
-                    return /*-max;*/-(2f * max - t);
-                case Loop:
-                    return t - max;
-            }
+        switch (loopMode) {
+            case Cycle:
+                boolean sign = ((int) (t / max) % 2) != 0;
+                float result;
+                if (t < 0){
+                    result = sign ? t % max : -(max + (t % max));
+                } else {
+                    result = sign ? -(max - (t % max)) : t % max;
+                }
+                return result;
+            case DontLoop:
+                return t > max ? max : (t < 0 ? 0 : t);
+            case Loop:
+                return t % max;
         }
-
         return t;
+        
+        
+//        if (max == Float.POSITIVE_INFINITY)
+//            return t;
+//        
+//        if (t < 0f){
+//            //float tMod = -(-t % max);
+//            switch (loopMode){
+//                case DontLoop:
+//                    return 0;
+//                case Cycle:
+//                    return t;
+//                case Loop:
+//                    return max - t;
+//            }
+//        }else if (t > max){
+//            switch (loopMode){
+//                case DontLoop:
+//                    return max;
+//                case Cycle:
+//                    return -(2f * max - t) % max;
+//                case Loop:
+//                    return t % max;
+//            }
+//        }
+//
+//        return t;
     }
 
     AnimChannel(AnimControl control){
@@ -225,6 +247,8 @@ public final class AnimChannel {
             loopModeBlendFrom = loopMode;
             blendAmount = 0f;
             blendRate   = 1f / blendTime;
+        }else{
+            blendFrom = null;
         }
 
         animation = anim;
@@ -335,9 +359,12 @@ public final class AnimChannel {
         if (animation == null)
             return;
 
-        if (blendFrom != null){
-            blendFrom.setTime(timeBlendFrom, 1f - blendAmount, control, this, vars);
-            //blendFrom.setTime(timeBlendFrom, control.skeleton, 1f - blendAmount, affectedBones);
+        if (blendFrom != null && blendAmount != 1.0f){
+            // The blendFrom anim is set, the actual animation
+            // playing will be set 
+            blendFrom.setTime(timeBlendFrom, 1f, control, this, vars);
+//            blendFrom.setTime(timeBlendFrom, 1f - blendAmount, control, this, vars);
+            
             timeBlendFrom += tpf * speedBlendFrom;
             timeBlendFrom = clampWrapTime(timeBlendFrom,
                                           blendFrom.getLength(),
@@ -353,9 +380,8 @@ public final class AnimChannel {
                 blendFrom = null;
             }
         }
-
+        
         animation.setTime(time, blendAmount, control, this, vars);
-        //animation.setTime(time, control.skeleton, blendAmount, affectedBones);
         time += tpf * speed;
 
         if (animation.getLength() > 0){
@@ -368,10 +394,10 @@ public final class AnimChannel {
 
         time = clampWrapTime(time, animation.getLength(), loopMode);
         if (time < 0){
+            // Negative time indicates that speed should be inverted
+            // (for cycle loop mode only)
             time = -time;
             speed = -speed;
         }
-
-        
     }
 }

+ 0 - 31
engine/src/core/com/jme3/animation/Animation.java

@@ -107,37 +107,6 @@ public class Animation implements Savable, Cloneable {
         for (int i = 0; i < tracks.length; i++){
             tracks[i].setTime(time, blendAmount, control, channel, vars);
         }
-        
-        /*
-        if (tracks != null && tracks.length > 0) {
-            Track<?> trackInstance = tracks[0];
-            
-            if (trackInstance instanceof SpatialTrack) {
-                Spatial spatial = control.getSpatial();
-                if (spatial != null) {
-                    ((SpatialTrack) tracks[0]).setTime(time, spatial, blendAmount);
-                }
-            } else if (trackInstance instanceof BoneTrack) {
-                BitSet affectedBones = channel.getAffectedBones();
-                Skeleton skeleton = control.getSkeleton();
-                for (int i = 0; i < tracks.length; ++i) {
-                    if (affectedBones == null || affectedBones.get(((BoneTrack) tracks[i]).getTargetIndex())) {
-                        ((BoneTrack) tracks[i]).setTime(time, skeleton, blendAmount);
-                    }
-                }
-            } else if (trackInstance instanceof PoseTrack) {
-                Spatial spatial = control.getSpatial();
-                List<Mesh> meshes = new ArrayList<Mesh>();
-                this.getMeshes(spatial, meshes);
-                if (meshes.size() > 0) {
-                    Mesh[] targets = meshes.toArray(new Mesh[meshes.size()]);
-                    for (int i = 0; i < tracks.length; ++i) {
-                        ((PoseTrack) tracks[i]).setTime(time, targets, blendAmount);
-                    }
-                }
-            }
-        }
-        */
     }
     
     /**