From 8634509a95ca4147e5b37014a64ff7dcfff57755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81my=20Bouquet?= Date: Tue, 2 Jan 2018 13:34:12 +0100 Subject: [PATCH] Speed support and some clean up --- .../main/java/com/jme3/anim/AnimComposer.java | 31 ++++++++++++++----- .../com/jme3/anim/tween/action/Action.java | 22 ++++++++++--- .../jme3/anim/tween/action/BaseAction.java | 11 ++----- .../jme3/anim/tween/action/BlendAction.java | 12 +++---- .../jme3/anim/tween/action/BlendSpace.java | 2 -- .../anim/tween/action/BlendableAction.java | 11 ++++++- .../jme3/anim/tween/action/ClipAction.java | 4 +-- .../anim/tween/action/LinearBlendSpace.java | 9 +++--- .../model/anim/TestAnimMigration.java | 23 ++++++++------ 9 files changed, 80 insertions(+), 45 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/anim/AnimComposer.java b/jme3-core/src/main/java/com/jme3/anim/AnimComposer.java index 1814faf72..6a8f92304 100644 --- a/jme3-core/src/main/java/com/jme3/anim/AnimComposer.java +++ b/jme3-core/src/main/java/com/jme3/anim/AnimComposer.java @@ -21,6 +21,7 @@ public class AnimComposer extends AbstractControl { private Action currentAction; private Map actions = new HashMap<>(); + private float globalSpeed = 1f; private float time; /** @@ -66,16 +67,22 @@ public class AnimComposer extends AbstractControl { public Action action(String name) { Action action = actions.get(name); if (action == null) { - AnimClip clip = animClipMap.get(name); - if (clip == null) { - throw new IllegalArgumentException("Cannot find clip named " + name); - } - action = new ClipAction(clip); + action = makeAction(name); actions.put(name, action); } return action; } + public Action makeAction(String name) { + Action action; + AnimClip clip = animClipMap.get(name); + if (clip == null) { + throw new IllegalArgumentException("Cannot find clip named " + name); + } + action = new ClipAction(clip); + return action; + } + public BaseAction actionSequence(String name, Tween... tweens) { BaseAction action = new BaseAction(Tweens.sequence(tweens)); @@ -86,7 +93,7 @@ public class AnimComposer extends AbstractControl { public BlendAction actionBlended(String name, BlendSpace blendSpace, String... clips) { BlendableAction[] acts = new BlendableAction[clips.length]; for (int i = 0; i < acts.length; i++) { - BlendableAction ba = (BlendableAction) action(clips[i]); + BlendableAction ba = (BlendableAction) makeAction(clips[i]); acts[i] = ba; } BlendAction action = new BlendAction(blendSpace, acts); @@ -111,9 +118,9 @@ public class AnimComposer extends AbstractControl { protected void controlUpdate(float tpf) { if (currentAction != null) { time += tpf; - boolean running = currentAction.interpolate(time); + boolean running = currentAction.interpolate(time * globalSpeed); if (!running) { - time -= currentAction.getLength(); + time = 0; } } } @@ -123,6 +130,14 @@ public class AnimComposer extends AbstractControl { } + public float getGlobalSpeed() { + return globalSpeed; + } + + public void setGlobalSpeed(float globalSpeed) { + this.globalSpeed = globalSpeed; + } + @Override public Object jmeClone() { try { diff --git a/jme3-core/src/main/java/com/jme3/anim/tween/action/Action.java b/jme3-core/src/main/java/com/jme3/anim/tween/action/Action.java index d445abf09..21d5bdc62 100644 --- a/jme3-core/src/main/java/com/jme3/anim/tween/action/Action.java +++ b/jme3-core/src/main/java/com/jme3/anim/tween/action/Action.java @@ -5,8 +5,8 @@ import com.jme3.anim.tween.Tween; public abstract class Action implements Tween { protected Action[] actions; - protected float weight = 1; - protected double length; + private double length; + private double speed = 1; protected Action(Tween... tweens) { this.actions = new Action[tweens.length]; @@ -20,13 +20,27 @@ public abstract class Action implements Tween { } } + @Override + public boolean interpolate(double t) { + return subInterpolate(t * speed); + } + + public abstract boolean subInterpolate(double t); + @Override public double getLength() { return length; } - public void setWeight(float weight) { - this.weight = weight; + protected void setLength(double length) { + this.length = length; } + public double getSpeed() { + return speed; + } + + public void setSpeed(double speed) { + this.speed = speed; + } } diff --git a/jme3-core/src/main/java/com/jme3/anim/tween/action/BaseAction.java b/jme3-core/src/main/java/com/jme3/anim/tween/action/BaseAction.java index 6e59ce091..c3c021530 100644 --- a/jme3-core/src/main/java/com/jme3/anim/tween/action/BaseAction.java +++ b/jme3-core/src/main/java/com/jme3/anim/tween/action/BaseAction.java @@ -11,7 +11,7 @@ public class BaseAction extends Action { public BaseAction(Tween tween) { this.tween = tween; - length = tween.getLength(); + setLength(tween.getLength()); gatherActions(tween); } @@ -27,14 +27,7 @@ public class BaseAction extends Action { } @Override - public void setWeight(float weight) { - for (Action action : subActions.getArray()) { - action.setWeight(weight); - } - } - - @Override - public boolean interpolate(double t) { + public boolean subInterpolate(double t) { return tween.interpolate(t); } } diff --git a/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendAction.java b/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendAction.java index 97188a11f..9c5848e4d 100644 --- a/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendAction.java +++ b/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendAction.java @@ -23,8 +23,8 @@ public class BlendAction extends BlendableAction { blendSpace.setBlendAction(this); for (BlendableAction action : actions) { - if (action.getLength() > length) { - length = action.getLength(); + if (action.getLength() > getLength()) { + setLength(action.getLength()); } Collection targets = action.getTargets(); for (HasLocalTransform target : targets) { @@ -40,10 +40,10 @@ public class BlendAction extends BlendableAction { //Stretching any action that doesn't have the same length. for (int i = 0; i < this.actions.length; i++) { this.timeFactor[i] = 1; - if (this.actions[i].getLength() != length) { + if (this.actions[i].getLength() != getLength()) { double actionLength = this.actions[i].getLength(); - if (actionLength > 0 && length > 0) { - this.timeFactor[i] = this.actions[i].getLength() / length; + if (actionLength > 0 && getLength() > 0) { + this.timeFactor[i] = this.actions[i].getLength() / getLength(); } } } @@ -114,7 +114,7 @@ public class BlendAction extends BlendableAction { private void collect(HasLocalTransform target, Transform tr) { if (collectTransformDelegate != null) { - collectTransformDelegate.collectTransform(target, tr, this.weight, this); + collectTransformDelegate.collectTransform(target, tr, this.getWeight(), this); } else { if (getTransitionWeight() == 1) { target.setLocalTransform(tr); diff --git a/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendSpace.java b/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendSpace.java index 956e74c3b..a88be7529 100644 --- a/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendSpace.java +++ b/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendSpace.java @@ -1,7 +1,5 @@ package com.jme3.anim.tween.action; -import com.jme3.anim.tween.action.BlendAction; - public interface BlendSpace { public void setBlendAction(BlendAction action); diff --git a/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendableAction.java b/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendableAction.java index 6ad68edc2..f1bcef9f2 100644 --- a/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendableAction.java +++ b/jme3-core/src/main/java/com/jme3/anim/tween/action/BlendableAction.java @@ -12,6 +12,7 @@ public abstract class BlendableAction extends Action { protected BlendableAction collectTransformDelegate; private float transitionWeight = 1.0f; private double transitionLength = 0.4f; + private float weight = 1f; private TransitionTween transition = new TransitionTween(transitionLength); public BlendableAction(Tween... tweens) { @@ -24,7 +25,7 @@ public abstract class BlendableAction extends Action { } @Override - public boolean interpolate(double t) { + public boolean subInterpolate(double t) { // Sanity check the inputs if (t < 0) { return true; @@ -49,6 +50,14 @@ public abstract class BlendableAction extends Action { return t < getLength(); } + public float getWeight() { + return weight; + } + + public void setWeight(float weight) { + this.weight = weight; + } + protected abstract void doInterpolate(double t); public abstract Collection getTargets(); diff --git a/jme3-core/src/main/java/com/jme3/anim/tween/action/ClipAction.java b/jme3-core/src/main/java/com/jme3/anim/tween/action/ClipAction.java index 2eb9aefc7..23cc9743e 100644 --- a/jme3-core/src/main/java/com/jme3/anim/tween/action/ClipAction.java +++ b/jme3-core/src/main/java/com/jme3/anim/tween/action/ClipAction.java @@ -17,7 +17,7 @@ public class ClipAction extends BlendableAction { public ClipAction(AnimClip clip) { this.clip = clip; - length = clip.getLength(); + setLength(clip.getLength()); } @Override @@ -29,7 +29,7 @@ public class ClipAction extends BlendableAction { track.getTransformAtTime(t, transform); if (collectTransformDelegate != null) { - collectTransformDelegate.collectTransform(target, transform, weight, this); + collectTransformDelegate.collectTransform(target, transform, getWeight(), this); } else { this.collectTransform(target, transform, getTransitionWeight(), this); } diff --git a/jme3-core/src/main/java/com/jme3/anim/tween/action/LinearBlendSpace.java b/jme3-core/src/main/java/com/jme3/anim/tween/action/LinearBlendSpace.java index f9773f8a0..31d2931fe 100644 --- a/jme3-core/src/main/java/com/jme3/anim/tween/action/LinearBlendSpace.java +++ b/jme3-core/src/main/java/com/jme3/anim/tween/action/LinearBlendSpace.java @@ -5,24 +5,25 @@ public class LinearBlendSpace implements BlendSpace { private BlendAction action; private float value; private float maxValue; + private float minValue; private float step; - public LinearBlendSpace(float maxValue) { + public LinearBlendSpace(float minValue, float maxValue) { this.maxValue = maxValue; - + this.minValue = minValue; } @Override public void setBlendAction(BlendAction action) { this.action = action; Action[] actions = action.getActions(); - step = maxValue / (float) (actions.length - 1); + step = (maxValue - minValue) / (float) (actions.length - 1); } @Override public float getWeight() { Action[] actions = action.getActions(); - float lowStep = 0, highStep = 0; + float lowStep = minValue, highStep = minValue; int lowIndex = 0, highIndex = 0; for (int i = 0; i < actions.length && highStep < value; i++) { lowStep = highStep; diff --git a/jme3-examples/src/main/java/jme3test/model/anim/TestAnimMigration.java b/jme3-examples/src/main/java/jme3test/model/anim/TestAnimMigration.java index d3eea6de5..9a3782676 100644 --- a/jme3-examples/src/main/java/jme3test/model/anim/TestAnimMigration.java +++ b/jme3-examples/src/main/java/jme3test/model/anim/TestAnimMigration.java @@ -2,6 +2,7 @@ package jme3test.model.anim; import com.jme3.anim.AnimComposer; import com.jme3.anim.SkinningControl; +import com.jme3.anim.tween.action.Action; import com.jme3.anim.tween.action.BlendAction; import com.jme3.anim.tween.action.BlendableAction; import com.jme3.anim.tween.action.LinearBlendSpace; @@ -32,7 +33,7 @@ public class TestAnimMigration extends SimpleApplication { LinkedList anims = new LinkedList<>(); boolean playAnim = false; BlendAction action; - float blendValue = 2f; + float blendValue = 1f; public static void main(String... argv) { TestAnimMigration app = new TestAnimMigration(); @@ -132,13 +133,15 @@ public class TestAnimMigration extends SimpleApplication { public void onAnalog(String name, float value, float tpf) { if (name.equals("blendUp")) { blendValue += value; - blendValue = FastMath.clamp(blendValue, 0, 4); + blendValue = FastMath.clamp(blendValue, 1, 4); action.getBlendSpace().setValue(blendValue); + action.setSpeed(blendValue); } if (name.equals("blendDown")) { blendValue -= value; - blendValue = FastMath.clamp(blendValue, 0, 4); + blendValue = FastMath.clamp(blendValue, 1, 4); action.getBlendSpace().setValue(blendValue); + action.setSpeed(blendValue); } System.err.println(blendValue); } @@ -160,14 +163,16 @@ public class TestAnimMigration extends SimpleApplication { anims.add(name); } composer.actionSequence("Sequence", - composer.action("Walk"), - composer.action("Run"), - composer.action("Jumping")); + composer.makeAction("Walk"), + composer.makeAction("Run"), + composer.makeAction("Jumping")).setSpeed(4); - action = composer.actionBlended("Blend", new LinearBlendSpace(4), - "Walk", "Punches", "Jumping", "Taunt"); + action = composer.actionBlended("Blend", new LinearBlendSpace(1, 4), + "Walk", "Run"); - action.getBlendSpace().setValue(2); + action.getBlendSpace().setValue(1); + + composer.action("Walk").setSpeed(2); // composer.actionSequence("Sequence", // composer.tweenFromClip("Walk"),