Speed support and some clean up
This commit is contained in:
parent
ce88350abf
commit
9dc87b71e9
@ -21,6 +21,7 @@ public class AnimComposer extends AbstractControl {
|
|||||||
|
|
||||||
private Action currentAction;
|
private Action currentAction;
|
||||||
private Map<String, Action> actions = new HashMap<>();
|
private Map<String, Action> actions = new HashMap<>();
|
||||||
|
private float globalSpeed = 1f;
|
||||||
private float time;
|
private float time;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,13 +67,19 @@ public class AnimComposer extends AbstractControl {
|
|||||||
public Action action(String name) {
|
public Action action(String name) {
|
||||||
Action action = actions.get(name);
|
Action action = actions.get(name);
|
||||||
if (action == null) {
|
if (action == null) {
|
||||||
|
action = makeAction(name);
|
||||||
|
actions.put(name, action);
|
||||||
|
}
|
||||||
|
return action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Action makeAction(String name) {
|
||||||
|
Action action;
|
||||||
AnimClip clip = animClipMap.get(name);
|
AnimClip clip = animClipMap.get(name);
|
||||||
if (clip == null) {
|
if (clip == null) {
|
||||||
throw new IllegalArgumentException("Cannot find clip named " + name);
|
throw new IllegalArgumentException("Cannot find clip named " + name);
|
||||||
}
|
}
|
||||||
action = new ClipAction(clip);
|
action = new ClipAction(clip);
|
||||||
actions.put(name, action);
|
|
||||||
}
|
|
||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +93,7 @@ public class AnimComposer extends AbstractControl {
|
|||||||
public BlendAction actionBlended(String name, BlendSpace blendSpace, String... clips) {
|
public BlendAction actionBlended(String name, BlendSpace blendSpace, String... clips) {
|
||||||
BlendableAction[] acts = new BlendableAction[clips.length];
|
BlendableAction[] acts = new BlendableAction[clips.length];
|
||||||
for (int i = 0; i < acts.length; i++) {
|
for (int i = 0; i < acts.length; i++) {
|
||||||
BlendableAction ba = (BlendableAction) action(clips[i]);
|
BlendableAction ba = (BlendableAction) makeAction(clips[i]);
|
||||||
acts[i] = ba;
|
acts[i] = ba;
|
||||||
}
|
}
|
||||||
BlendAction action = new BlendAction(blendSpace, acts);
|
BlendAction action = new BlendAction(blendSpace, acts);
|
||||||
@ -111,9 +118,9 @@ public class AnimComposer extends AbstractControl {
|
|||||||
protected void controlUpdate(float tpf) {
|
protected void controlUpdate(float tpf) {
|
||||||
if (currentAction != null) {
|
if (currentAction != null) {
|
||||||
time += tpf;
|
time += tpf;
|
||||||
boolean running = currentAction.interpolate(time);
|
boolean running = currentAction.interpolate(time * globalSpeed);
|
||||||
if (!running) {
|
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
|
@Override
|
||||||
public Object jmeClone() {
|
public Object jmeClone() {
|
||||||
try {
|
try {
|
||||||
|
@ -5,8 +5,8 @@ import com.jme3.anim.tween.Tween;
|
|||||||
public abstract class Action implements Tween {
|
public abstract class Action implements Tween {
|
||||||
|
|
||||||
protected Action[] actions;
|
protected Action[] actions;
|
||||||
protected float weight = 1;
|
private double length;
|
||||||
protected double length;
|
private double speed = 1;
|
||||||
|
|
||||||
protected Action(Tween... tweens) {
|
protected Action(Tween... tweens) {
|
||||||
this.actions = new Action[tweens.length];
|
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
|
@Override
|
||||||
public double getLength() {
|
public double getLength() {
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setWeight(float weight) {
|
protected void setLength(double length) {
|
||||||
this.weight = weight;
|
this.length = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getSpeed() {
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSpeed(double speed) {
|
||||||
|
this.speed = speed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ public class BaseAction extends Action {
|
|||||||
|
|
||||||
public BaseAction(Tween tween) {
|
public BaseAction(Tween tween) {
|
||||||
this.tween = tween;
|
this.tween = tween;
|
||||||
length = tween.getLength();
|
setLength(tween.getLength());
|
||||||
gatherActions(tween);
|
gatherActions(tween);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,14 +27,7 @@ public class BaseAction extends Action {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setWeight(float weight) {
|
public boolean subInterpolate(double t) {
|
||||||
for (Action action : subActions.getArray()) {
|
|
||||||
action.setWeight(weight);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean interpolate(double t) {
|
|
||||||
return tween.interpolate(t);
|
return tween.interpolate(t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,8 @@ public class BlendAction extends BlendableAction {
|
|||||||
blendSpace.setBlendAction(this);
|
blendSpace.setBlendAction(this);
|
||||||
|
|
||||||
for (BlendableAction action : actions) {
|
for (BlendableAction action : actions) {
|
||||||
if (action.getLength() > length) {
|
if (action.getLength() > getLength()) {
|
||||||
length = action.getLength();
|
setLength(action.getLength());
|
||||||
}
|
}
|
||||||
Collection<HasLocalTransform> targets = action.getTargets();
|
Collection<HasLocalTransform> targets = action.getTargets();
|
||||||
for (HasLocalTransform target : targets) {
|
for (HasLocalTransform target : targets) {
|
||||||
@ -40,10 +40,10 @@ public class BlendAction extends BlendableAction {
|
|||||||
//Stretching any action that doesn't have the same length.
|
//Stretching any action that doesn't have the same length.
|
||||||
for (int i = 0; i < this.actions.length; i++) {
|
for (int i = 0; i < this.actions.length; i++) {
|
||||||
this.timeFactor[i] = 1;
|
this.timeFactor[i] = 1;
|
||||||
if (this.actions[i].getLength() != length) {
|
if (this.actions[i].getLength() != getLength()) {
|
||||||
double actionLength = this.actions[i].getLength();
|
double actionLength = this.actions[i].getLength();
|
||||||
if (actionLength > 0 && length > 0) {
|
if (actionLength > 0 && getLength() > 0) {
|
||||||
this.timeFactor[i] = this.actions[i].getLength() / length;
|
this.timeFactor[i] = this.actions[i].getLength() / getLength();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,7 +114,7 @@ public class BlendAction extends BlendableAction {
|
|||||||
|
|
||||||
private void collect(HasLocalTransform target, Transform tr) {
|
private void collect(HasLocalTransform target, Transform tr) {
|
||||||
if (collectTransformDelegate != null) {
|
if (collectTransformDelegate != null) {
|
||||||
collectTransformDelegate.collectTransform(target, tr, this.weight, this);
|
collectTransformDelegate.collectTransform(target, tr, this.getWeight(), this);
|
||||||
} else {
|
} else {
|
||||||
if (getTransitionWeight() == 1) {
|
if (getTransitionWeight() == 1) {
|
||||||
target.setLocalTransform(tr);
|
target.setLocalTransform(tr);
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.jme3.anim.tween.action;
|
package com.jme3.anim.tween.action;
|
||||||
|
|
||||||
import com.jme3.anim.tween.action.BlendAction;
|
|
||||||
|
|
||||||
public interface BlendSpace {
|
public interface BlendSpace {
|
||||||
|
|
||||||
public void setBlendAction(BlendAction action);
|
public void setBlendAction(BlendAction action);
|
||||||
|
@ -12,6 +12,7 @@ public abstract class BlendableAction extends Action {
|
|||||||
protected BlendableAction collectTransformDelegate;
|
protected BlendableAction collectTransformDelegate;
|
||||||
private float transitionWeight = 1.0f;
|
private float transitionWeight = 1.0f;
|
||||||
private double transitionLength = 0.4f;
|
private double transitionLength = 0.4f;
|
||||||
|
private float weight = 1f;
|
||||||
private TransitionTween transition = new TransitionTween(transitionLength);
|
private TransitionTween transition = new TransitionTween(transitionLength);
|
||||||
|
|
||||||
public BlendableAction(Tween... tweens) {
|
public BlendableAction(Tween... tweens) {
|
||||||
@ -24,7 +25,7 @@ public abstract class BlendableAction extends Action {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean interpolate(double t) {
|
public boolean subInterpolate(double t) {
|
||||||
// Sanity check the inputs
|
// Sanity check the inputs
|
||||||
if (t < 0) {
|
if (t < 0) {
|
||||||
return true;
|
return true;
|
||||||
@ -49,6 +50,14 @@ public abstract class BlendableAction extends Action {
|
|||||||
return t < getLength();
|
return t < getLength();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public float getWeight() {
|
||||||
|
return weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWeight(float weight) {
|
||||||
|
this.weight = weight;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract void doInterpolate(double t);
|
protected abstract void doInterpolate(double t);
|
||||||
|
|
||||||
public abstract Collection<HasLocalTransform> getTargets();
|
public abstract Collection<HasLocalTransform> getTargets();
|
||||||
|
@ -17,7 +17,7 @@ public class ClipAction extends BlendableAction {
|
|||||||
|
|
||||||
public ClipAction(AnimClip clip) {
|
public ClipAction(AnimClip clip) {
|
||||||
this.clip = clip;
|
this.clip = clip;
|
||||||
length = clip.getLength();
|
setLength(clip.getLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -29,7 +29,7 @@ public class ClipAction extends BlendableAction {
|
|||||||
track.getTransformAtTime(t, transform);
|
track.getTransformAtTime(t, transform);
|
||||||
|
|
||||||
if (collectTransformDelegate != null) {
|
if (collectTransformDelegate != null) {
|
||||||
collectTransformDelegate.collectTransform(target, transform, weight, this);
|
collectTransformDelegate.collectTransform(target, transform, getWeight(), this);
|
||||||
} else {
|
} else {
|
||||||
this.collectTransform(target, transform, getTransitionWeight(), this);
|
this.collectTransform(target, transform, getTransitionWeight(), this);
|
||||||
}
|
}
|
||||||
|
@ -5,24 +5,25 @@ public class LinearBlendSpace implements BlendSpace {
|
|||||||
private BlendAction action;
|
private BlendAction action;
|
||||||
private float value;
|
private float value;
|
||||||
private float maxValue;
|
private float maxValue;
|
||||||
|
private float minValue;
|
||||||
private float step;
|
private float step;
|
||||||
|
|
||||||
public LinearBlendSpace(float maxValue) {
|
public LinearBlendSpace(float minValue, float maxValue) {
|
||||||
this.maxValue = maxValue;
|
this.maxValue = maxValue;
|
||||||
|
this.minValue = minValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBlendAction(BlendAction action) {
|
public void setBlendAction(BlendAction action) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
Action[] actions = action.getActions();
|
Action[] actions = action.getActions();
|
||||||
step = maxValue / (float) (actions.length - 1);
|
step = (maxValue - minValue) / (float) (actions.length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getWeight() {
|
public float getWeight() {
|
||||||
Action[] actions = action.getActions();
|
Action[] actions = action.getActions();
|
||||||
float lowStep = 0, highStep = 0;
|
float lowStep = minValue, highStep = minValue;
|
||||||
int lowIndex = 0, highIndex = 0;
|
int lowIndex = 0, highIndex = 0;
|
||||||
for (int i = 0; i < actions.length && highStep < value; i++) {
|
for (int i = 0; i < actions.length && highStep < value; i++) {
|
||||||
lowStep = highStep;
|
lowStep = highStep;
|
||||||
|
@ -2,6 +2,7 @@ package jme3test.model.anim;
|
|||||||
|
|
||||||
import com.jme3.anim.AnimComposer;
|
import com.jme3.anim.AnimComposer;
|
||||||
import com.jme3.anim.SkinningControl;
|
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.BlendAction;
|
||||||
import com.jme3.anim.tween.action.BlendableAction;
|
import com.jme3.anim.tween.action.BlendableAction;
|
||||||
import com.jme3.anim.tween.action.LinearBlendSpace;
|
import com.jme3.anim.tween.action.LinearBlendSpace;
|
||||||
@ -32,7 +33,7 @@ public class TestAnimMigration extends SimpleApplication {
|
|||||||
LinkedList<String> anims = new LinkedList<>();
|
LinkedList<String> anims = new LinkedList<>();
|
||||||
boolean playAnim = false;
|
boolean playAnim = false;
|
||||||
BlendAction action;
|
BlendAction action;
|
||||||
float blendValue = 2f;
|
float blendValue = 1f;
|
||||||
|
|
||||||
public static void main(String... argv) {
|
public static void main(String... argv) {
|
||||||
TestAnimMigration app = new TestAnimMigration();
|
TestAnimMigration app = new TestAnimMigration();
|
||||||
@ -132,13 +133,15 @@ public class TestAnimMigration extends SimpleApplication {
|
|||||||
public void onAnalog(String name, float value, float tpf) {
|
public void onAnalog(String name, float value, float tpf) {
|
||||||
if (name.equals("blendUp")) {
|
if (name.equals("blendUp")) {
|
||||||
blendValue += value;
|
blendValue += value;
|
||||||
blendValue = FastMath.clamp(blendValue, 0, 4);
|
blendValue = FastMath.clamp(blendValue, 1, 4);
|
||||||
action.getBlendSpace().setValue(blendValue);
|
action.getBlendSpace().setValue(blendValue);
|
||||||
|
action.setSpeed(blendValue);
|
||||||
}
|
}
|
||||||
if (name.equals("blendDown")) {
|
if (name.equals("blendDown")) {
|
||||||
blendValue -= value;
|
blendValue -= value;
|
||||||
blendValue = FastMath.clamp(blendValue, 0, 4);
|
blendValue = FastMath.clamp(blendValue, 1, 4);
|
||||||
action.getBlendSpace().setValue(blendValue);
|
action.getBlendSpace().setValue(blendValue);
|
||||||
|
action.setSpeed(blendValue);
|
||||||
}
|
}
|
||||||
System.err.println(blendValue);
|
System.err.println(blendValue);
|
||||||
}
|
}
|
||||||
@ -160,14 +163,16 @@ public class TestAnimMigration extends SimpleApplication {
|
|||||||
anims.add(name);
|
anims.add(name);
|
||||||
}
|
}
|
||||||
composer.actionSequence("Sequence",
|
composer.actionSequence("Sequence",
|
||||||
composer.action("Walk"),
|
composer.makeAction("Walk"),
|
||||||
composer.action("Run"),
|
composer.makeAction("Run"),
|
||||||
composer.action("Jumping"));
|
composer.makeAction("Jumping")).setSpeed(4);
|
||||||
|
|
||||||
action = composer.actionBlended("Blend", new LinearBlendSpace(4),
|
action = composer.actionBlended("Blend", new LinearBlendSpace(1, 4),
|
||||||
"Walk", "Punches", "Jumping", "Taunt");
|
"Walk", "Run");
|
||||||
|
|
||||||
action.getBlendSpace().setValue(2);
|
action.getBlendSpace().setValue(1);
|
||||||
|
|
||||||
|
composer.action("Walk").setSpeed(2);
|
||||||
|
|
||||||
// composer.actionSequence("Sequence",
|
// composer.actionSequence("Sequence",
|
||||||
// composer.tweenFromClip("Walk"),
|
// composer.tweenFromClip("Walk"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user