From 382c1c85f5154cdbeca51caa3e6def0c666e17b8 Mon Sep 17 00:00:00 2001 From: "Kae..pl" Date: Thu, 28 Nov 2013 20:03:46 +0000 Subject: [PATCH] Bugfix: added tracks equalisation method (inequal tracks' frame count caused exceptions during animation simulations). git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10921 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../blender/animations/ArmatureHelper.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/animations/ArmatureHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/animations/ArmatureHelper.java index 8b26f30d5..0864a1250 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/animations/ArmatureHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/animations/ArmatureHelper.java @@ -41,6 +41,8 @@ import java.util.logging.Logger; import com.jme3.animation.Bone; import com.jme3.animation.BoneTrack; import com.jme3.animation.Skeleton; +import com.jme3.math.Quaternion; +import com.jme3.math.Vector3f; import com.jme3.scene.plugins.blender.AbstractBlenderHelper; import com.jme3.scene.plugins.blender.BlenderContext; import com.jme3.scene.plugins.blender.curves.BezierCurve; @@ -184,6 +186,7 @@ public class ArmatureHelper extends AbstractBlenderHelper { tracks.add((BoneTrack) ipo.calculateTrack(boneIndex, bone.getLocalRotation(), 0, ipo.getLastFrame(), fps, false)); } } + this.equaliseBoneTracks(tracks); return tracks.toArray(new BoneTrack[tracks.size()]); } @@ -223,6 +226,51 @@ public class ArmatureHelper extends AbstractBlenderHelper { } } } + this.equaliseBoneTracks(tracks); return tracks.toArray(new BoneTrack[tracks.size()]); } + + /** + * The method makes all the tracks to have equal frame lengths. + * @param tracks + * the tracks to be equalized + */ + private void equaliseBoneTracks(List tracks) { + // first compute the maximum amount of frames + int maximumFrameCount = -1; + float[] maximumTrackTimes = null; + for (BoneTrack track : tracks) { + if (track.getTimes().length > maximumFrameCount) { + maximumTrackTimes = track.getTimes(); + maximumFrameCount = maximumTrackTimes.length; + } + } + + // now widen all the tracks that have less frames by repeating the last values in the frame + for (BoneTrack track : tracks) { + int currentTrackLength = track.getTimes().length; + if (currentTrackLength < maximumFrameCount) { + Vector3f[] translations = new Vector3f[maximumFrameCount]; + Quaternion[] rotations = new Quaternion[maximumFrameCount]; + Vector3f[] scales = new Vector3f[maximumFrameCount]; + + Vector3f[] currentTranslations = track.getTranslations(); + Quaternion[] currentRotations = track.getRotations(); + Vector3f[] currentScales = track.getScales(); + for (int i = 0; i < currentTrackLength; ++i) { + translations[i] = currentTranslations[i]; + rotations[i] = currentRotations[i]; + scales[i] = currentScales[i]; + } + + for (int i = currentTrackLength; i < maximumFrameCount; ++i) { + translations[i] = currentTranslations[currentTranslations.length - 1]; + rotations[i] = currentRotations[currentRotations.length - 1]; + scales[i] = currentScales[currentScales.length - 1]; + } + + track.setKeyframes(maximumTrackTimes, translations, rotations, scales); + } + } + } }