AnimComposer: updated to save globalSpeed on export and fixed issue with Layer cloning (#1080)

* AnimComposer: save globalSpeed on export

* Updated TestModelExportingCloning to test AnimComposer.globalSpeed

* Fixed issue with AnimComposer.Layer cloning
accellbaker
Ali-RS 6 years ago committed by GitHub
parent 50aed6f59e
commit c4d2de1656
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      jme3-core/src/main/java/com/jme3/anim/AnimComposer.java
  2. 3
      jme3-examples/src/main/java/jme3test/model/anim/TestModelExportingCloning.java

@ -29,7 +29,7 @@ public class AnimComposer extends AbstractControl {
private Map<String, Layer> layers = new LinkedHashMap<>(); private Map<String, Layer> layers = new LinkedHashMap<>();
public AnimComposer() { public AnimComposer() {
layers.put(DEFAULT_LAYER, new Layer()); layers.put(DEFAULT_LAYER, new Layer(this));
} }
/** /**
@ -171,7 +171,7 @@ public class AnimComposer extends AbstractControl {
} }
public void makeLayer(String name, AnimationMask mask) { public void makeLayer(String name, AnimationMask mask) {
Layer l = new Layer(); Layer l = new Layer(this);
l.mask = mask; l.mask = mask;
layers.put(name, l); layers.put(name, l);
} }
@ -300,6 +300,7 @@ public class AnimComposer extends AbstractControl {
super.read(im); super.read(im);
InputCapsule ic = im.getCapsule(this); InputCapsule ic = im.getCapsule(this);
animClipMap = (Map<String, AnimClip>) ic.readStringSavableMap("animClipMap", new HashMap<String, AnimClip>()); animClipMap = (Map<String, AnimClip>) ic.readStringSavableMap("animClipMap", new HashMap<String, AnimClip>());
globalSpeed = ic.readFloat("globalSpeed", 1f);
} }
@Override @Override
@ -307,16 +308,22 @@ public class AnimComposer extends AbstractControl {
super.write(ex); super.write(ex);
OutputCapsule oc = ex.getCapsule(this); OutputCapsule oc = ex.getCapsule(this);
oc.writeStringSavableMap(animClipMap, "animClipMap", new HashMap<String, AnimClip>()); oc.writeStringSavableMap(animClipMap, "animClipMap", new HashMap<String, AnimClip>());
oc.write(globalSpeed, "globalSpeed", 1f);
} }
private class Layer implements JmeCloneable { private static class Layer implements JmeCloneable {
private AnimComposer ac;
private Action currentAction; private Action currentAction;
private AnimationMask mask; private AnimationMask mask;
private float weight; private float weight;
private double time; private double time;
public Layer(AnimComposer ac) {
this.ac = ac;
}
public void advance(float tpf) { public void advance(float tpf) {
time += tpf * currentAction.getSpeed() * globalSpeed; time += tpf * currentAction.getSpeed() * ac.globalSpeed;
// make sure negative time is in [0, length] range // make sure negative time is in [0, length] range
if (time < 0) { if (time < 0) {
double length = currentAction.getLength(); double length = currentAction.getLength();
@ -337,6 +344,7 @@ public class AnimComposer extends AbstractControl {
@Override @Override
public void cloneFields(Cloner cloner, Object original) { public void cloneFields(Cloner cloner, Object original) {
ac = cloner.clone(ac);
currentAction = null; currentAction = null;
} }
} }

@ -61,18 +61,21 @@ public class TestModelExportingCloning extends SimpleApplication {
Spatial originalModel = assetManager.loadModel("Models/Oto/Oto.mesh.xml"); Spatial originalModel = assetManager.loadModel("Models/Oto/Oto.mesh.xml");
composer = originalModel.getControl(AnimComposer.class); composer = originalModel.getControl(AnimComposer.class);
composer.setCurrentAction("Walk"); composer.setCurrentAction("Walk");
composer.setGlobalSpeed(1.5f);
rootNode.attachChild(originalModel); rootNode.attachChild(originalModel);
Spatial clonedModel = originalModel.clone(); Spatial clonedModel = originalModel.clone();
clonedModel.move(10, 0, 0); clonedModel.move(10, 0, 0);
composer = clonedModel.getControl(AnimComposer.class); composer = clonedModel.getControl(AnimComposer.class);
composer.setCurrentAction("push"); composer.setCurrentAction("push");
System.out.println("clonedModel: globalSpeed=" + composer.getGlobalSpeed());
rootNode.attachChild(clonedModel); rootNode.attachChild(clonedModel);
Spatial exportedModel = BinaryExporter.saveAndLoad(assetManager, originalModel); Spatial exportedModel = BinaryExporter.saveAndLoad(assetManager, originalModel);
exportedModel.move(20, 0, 0); exportedModel.move(20, 0, 0);
composer = exportedModel.getControl(AnimComposer.class); composer = exportedModel.getControl(AnimComposer.class);
composer.setCurrentAction("pull"); composer.setCurrentAction("pull");
System.out.println("exportedModel: globalSpeed=" + composer.getGlobalSpeed());
rootNode.attachChild(exportedModel); rootNode.attachChild(exportedModel);
} }
} }

Loading…
Cancel
Save