Skip to content

Commit c4d2de1

Browse files
authored
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
1 parent 50aed6f commit c4d2de1

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

jme3-core/src/main/java/com/jme3/anim/AnimComposer.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class AnimComposer extends AbstractControl {
2929
private Map<String, Layer> layers = new LinkedHashMap<>();
3030

3131
public AnimComposer() {
32-
layers.put(DEFAULT_LAYER, new Layer());
32+
layers.put(DEFAULT_LAYER, new Layer(this));
3333
}
3434

3535
/**
@@ -171,7 +171,7 @@ public Action removeAction(String name) {
171171
}
172172

173173
public void makeLayer(String name, AnimationMask mask) {
174-
Layer l = new Layer();
174+
Layer l = new Layer(this);
175175
l.mask = mask;
176176
layers.put(name, l);
177177
}
@@ -300,23 +300,30 @@ public void read(JmeImporter im) throws IOException {
300300
super.read(im);
301301
InputCapsule ic = im.getCapsule(this);
302302
animClipMap = (Map<String, AnimClip>) ic.readStringSavableMap("animClipMap", new HashMap<String, AnimClip>());
303+
globalSpeed = ic.readFloat("globalSpeed", 1f);
303304
}
304305

305306
@Override
306307
public void write(JmeExporter ex) throws IOException {
307308
super.write(ex);
308309
OutputCapsule oc = ex.getCapsule(this);
309310
oc.writeStringSavableMap(animClipMap, "animClipMap", new HashMap<String, AnimClip>());
311+
oc.write(globalSpeed, "globalSpeed", 1f);
310312
}
311313

312-
private class Layer implements JmeCloneable {
314+
private static class Layer implements JmeCloneable {
315+
private AnimComposer ac;
313316
private Action currentAction;
314317
private AnimationMask mask;
315318
private float weight;
316319
private double time;
317320

321+
public Layer(AnimComposer ac) {
322+
this.ac = ac;
323+
}
324+
318325
public void advance(float tpf) {
319-
time += tpf * currentAction.getSpeed() * globalSpeed;
326+
time += tpf * currentAction.getSpeed() * ac.globalSpeed;
320327
// make sure negative time is in [0, length] range
321328
if (time < 0) {
322329
double length = currentAction.getLength();
@@ -337,6 +344,7 @@ public Object jmeClone() {
337344

338345
@Override
339346
public void cloneFields(Cloner cloner, Object original) {
347+
ac = cloner.clone(ac);
340348
currentAction = null;
341349
}
342350
}

jme3-examples/src/main/java/jme3test/model/anim/TestModelExportingCloning.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,21 @@ public void simpleInitApp() {
6161
Spatial originalModel = assetManager.loadModel("Models/Oto/Oto.mesh.xml");
6262
composer = originalModel.getControl(AnimComposer.class);
6363
composer.setCurrentAction("Walk");
64+
composer.setGlobalSpeed(1.5f);
6465
rootNode.attachChild(originalModel);
6566

6667
Spatial clonedModel = originalModel.clone();
6768
clonedModel.move(10, 0, 0);
6869
composer = clonedModel.getControl(AnimComposer.class);
6970
composer.setCurrentAction("push");
71+
System.out.println("clonedModel: globalSpeed=" + composer.getGlobalSpeed());
7072
rootNode.attachChild(clonedModel);
7173

7274
Spatial exportedModel = BinaryExporter.saveAndLoad(assetManager, originalModel);
7375
exportedModel.move(20, 0, 0);
7476
composer = exportedModel.getControl(AnimComposer.class);
7577
composer.setCurrentAction("pull");
78+
System.out.println("exportedModel: globalSpeed=" + composer.getGlobalSpeed());
7679
rootNode.attachChild(exportedModel);
7780
}
7881
}

0 commit comments

Comments
 (0)