From 2cdeb6ebffa4befe604b07360012a324966d1ee3 Mon Sep 17 00:00:00 2001 From: "Sha..rd" Date: Sun, 25 Mar 2012 18:41:38 +0000 Subject: [PATCH] * 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 --- .../core/com/jme3/animation/AnimChannel.java | 84 ++++++++++++------- .../core/com/jme3/animation/Animation.java | 31 ------- 2 files changed, 55 insertions(+), 60 deletions(-) diff --git a/engine/src/core/com/jme3/animation/AnimChannel.java b/engine/src/core/com/jme3/animation/AnimChannel.java index cce6aeeea..f22631499 100644 --- a/engine/src/core/com/jme3/animation/AnimChannel.java +++ b/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; } - - } } diff --git a/engine/src/core/com/jme3/animation/Animation.java b/engine/src/core/com/jme3/animation/Animation.java index 0fb505a2f..a0999f260 100644 --- a/engine/src/core/com/jme3/animation/Animation.java +++ b/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 meshes = new ArrayList(); - 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); - } - } - } - } - */ } /**