Skip to content

Commit 3a784d9

Browse files
committed
add tests
1 parent 7d9880d commit 3a784d9

File tree

2 files changed

+335
-0
lines changed

2 files changed

+335
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package jme3test.animation;
2+
3+
import java.io.IOException;
4+
5+
import com.jme3.anim.AnimClip;
6+
import com.jme3.anim.AnimComposer;
7+
import com.jme3.anim.AnimFactory;
8+
import com.jme3.anim.util.AnimMigrationUtils;
9+
import com.jme3.app.SimpleApplication;
10+
import com.jme3.cinematic.PlayState;
11+
import com.jme3.cinematic.events.AnimEvent;
12+
import com.jme3.export.InputCapsule;
13+
import com.jme3.export.JmeExporter;
14+
import com.jme3.export.JmeImporter;
15+
import com.jme3.export.OutputCapsule;
16+
import com.jme3.export.Savable;
17+
import com.jme3.export.binary.BinaryExporter;
18+
import com.jme3.input.KeyInput;
19+
import com.jme3.input.controls.ActionListener;
20+
import com.jme3.input.controls.KeyTrigger;
21+
import com.jme3.light.DirectionalLight;
22+
import com.jme3.material.Material;
23+
import com.jme3.math.ColorRGBA;
24+
import com.jme3.math.Vector3f;
25+
import com.jme3.renderer.Caps;
26+
import com.jme3.renderer.queue.RenderQueue;
27+
import com.jme3.scene.Node;
28+
import com.jme3.scene.Spatial;
29+
import com.jme3.shadow.DirectionalLightShadowRenderer;
30+
31+
32+
public class TestAnimEventSavable extends SimpleApplication {
33+
34+
public static void main(String[] args) {
35+
TestAnimEventSavable app = new TestAnimEventSavable();
36+
app.setPauseOnLostFocus(false);
37+
app.start();
38+
}
39+
40+
private Spatial teapot;
41+
private AnimEvent evt;
42+
43+
public static class AnimatedScene implements Savable{
44+
public Node scene;
45+
public AnimEvent anim;
46+
47+
@Override
48+
public void write(JmeExporter ex) throws IOException {
49+
OutputCapsule oc = ex.getCapsule(this);
50+
oc.write(scene, "scene", null);
51+
oc.write(anim, "anim", null);
52+
}
53+
54+
@Override
55+
public void read(JmeImporter im) throws IOException {
56+
InputCapsule ic = im.getCapsule(this);
57+
scene = (Node) ic.readSavable("scene", null);
58+
anim = (AnimEvent) ic.readSavable("anim", null);
59+
}
60+
61+
}
62+
63+
@Override
64+
public void simpleInitApp() {
65+
66+
viewPort.setBackgroundColor(ColorRGBA.DarkGray);
67+
68+
69+
setupLightsAndFilters();
70+
setupModel();
71+
72+
Node jaime = (Node) assetManager.loadModel("Models/Jaime/Jaime.j3o");
73+
AnimMigrationUtils.migrate(jaime);
74+
jaime.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
75+
evt = new AnimEvent(jaime.getControl(AnimComposer.class), "JumpStart", AnimComposer.DEFAULT_LAYER);
76+
77+
AnimatedScene original = new AnimatedScene();
78+
original.scene = jaime;
79+
original.anim = evt;
80+
81+
AnimatedScene copy = BinaryExporter.saveAndLoad(assetManager, original);
82+
rootNode.attachChild(copy.scene);
83+
evt = copy.anim;
84+
85+
assert copy.anim.getComposer()!=original.anim.getComposer();
86+
assert copy.scene.getControl(AnimComposer.class)==copy.anim.getComposer();
87+
88+
89+
initInputs();
90+
}
91+
92+
93+
private void setupLightsAndFilters() {
94+
DirectionalLight light = new DirectionalLight();
95+
light.setDirection(new Vector3f(0, -1, -1).normalizeLocal());
96+
light.setColor(ColorRGBA.White.mult(1.5f));
97+
rootNode.addLight(light);
98+
99+
if (renderer.getCaps().contains(Caps.GLSL100)) {
100+
DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, 512, 1);
101+
dlsr.setLight(light);
102+
dlsr.setShadowIntensity(0.4f);
103+
viewPort.addProcessor(dlsr);
104+
}
105+
}
106+
107+
private void setupModel() {
108+
teapot = assetManager.loadModel("Models/Teapot/Teapot.obj");
109+
teapot.setLocalTranslation(5, 0, 5);
110+
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
111+
mat.setColor("Color", ColorRGBA.Cyan);
112+
teapot.setMaterial(mat);
113+
teapot.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
114+
rootNode.attachChild(teapot);
115+
116+
// creating spatial animation for the teapot
117+
AnimFactory factory = new AnimFactory(20f, "teapotAnim", 30f);
118+
factory.addTimeTranslation(0, new Vector3f(5, 0, 5));
119+
factory.addTimeTranslation(4, new Vector3f(5, 0, -5));
120+
AnimClip animClip = factory.buildAnimation(teapot);
121+
122+
AnimComposer animComposer = new AnimComposer();
123+
animComposer.addAnimClip(animClip);
124+
teapot.addControl(animComposer);
125+
}
126+
127+
128+
private void initInputs() {
129+
inputManager.addMapping("togglePlay", new KeyTrigger(KeyInput.KEY_SPACE));
130+
ActionListener acl = new ActionListener() {
131+
132+
@Override
133+
public void onAction(String name, boolean keyPressed, float tpf) {
134+
if (name.equals("togglePlay") && keyPressed) {
135+
if (evt.getPlayState() == PlayState.Playing) {
136+
evt.pause();
137+
} else {
138+
System.out.println("Play");
139+
evt.play();
140+
}
141+
}
142+
143+
}
144+
};
145+
inputManager.addListener(acl, "togglePlay");
146+
}
147+
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package jme3test.animation;
2+
3+
import com.jme3.anim.AnimClip;
4+
import com.jme3.anim.AnimComposer;
5+
import com.jme3.anim.AnimFactory;
6+
import com.jme3.anim.util.AnimMigrationUtils;
7+
import com.jme3.animation.LoopMode;
8+
import com.jme3.app.SimpleApplication;
9+
import com.jme3.cinematic.Cinematic;
10+
import com.jme3.cinematic.MotionPath;
11+
import com.jme3.cinematic.PlayState;
12+
import com.jme3.cinematic.events.AnimEvent;
13+
import com.jme3.cinematic.events.CinematicEvent;
14+
import com.jme3.cinematic.events.CinematicEventListener;
15+
import com.jme3.cinematic.events.MotionEvent;
16+
import com.jme3.cinematic.events.SoundEvent;
17+
import com.jme3.export.binary.BinaryExporter;
18+
import com.jme3.input.KeyInput;
19+
import com.jme3.input.controls.ActionListener;
20+
import com.jme3.input.controls.KeyTrigger;
21+
import com.jme3.light.DirectionalLight;
22+
import com.jme3.material.Material;
23+
import com.jme3.math.ColorRGBA;
24+
import com.jme3.math.Vector3f;
25+
import com.jme3.renderer.Caps;
26+
import com.jme3.renderer.queue.RenderQueue;
27+
import com.jme3.scene.CameraNode;
28+
import com.jme3.scene.Node;
29+
import com.jme3.scene.Spatial;
30+
import com.jme3.shadow.DirectionalLightShadowRenderer;
31+
32+
/**
33+
*
34+
* @author capdevon
35+
*/
36+
public class TestCinematicSavable extends SimpleApplication {
37+
38+
public static void main(String[] args) {
39+
TestCinematicSavable app = new TestCinematicSavable();
40+
app.setPauseOnLostFocus(false);
41+
app.start();
42+
}
43+
44+
private Cinematic cinematic;
45+
private Spatial teapot;
46+
private MotionEvent cameraMotionEvent;
47+
48+
@Override
49+
public void simpleInitApp() {
50+
51+
viewPort.setBackgroundColor(ColorRGBA.DarkGray);
52+
53+
cinematic = new Cinematic(rootNode, 10);
54+
// cinematic.initialize(stateManager, this);
55+
// stateManager.attach(cinematic);
56+
57+
setupLightsAndFilters();
58+
setupModel();
59+
createCameraMotion();
60+
61+
Node jaime = (Node) assetManager.loadModel("Models/Jaime/Jaime.j3o");
62+
AnimMigrationUtils.migrate(jaime);
63+
jaime.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
64+
rootNode.attachChild(jaime);
65+
66+
cinematic.activateCamera(0, "aroundCam");
67+
// cinematic.activateCamera(0, "topView");
68+
cinematic.addCinematicEvent(0f, new AnimEvent(teapot.getControl(AnimComposer.class), "teapotAnim",
69+
AnimComposer.DEFAULT_LAYER));
70+
cinematic.addCinematicEvent(0f,
71+
new AnimEvent(jaime.getControl(AnimComposer.class), "JumpStart", AnimComposer.DEFAULT_LAYER));
72+
cinematic.addCinematicEvent(0f, cameraMotionEvent);
73+
cinematic.addCinematicEvent(0f, new SoundEvent("Sound/Environment/Nature.ogg", LoopMode.Loop));
74+
cinematic.addCinematicEvent(3f, new SoundEvent("Sound/Effects/kick.wav"));
75+
cinematic.addCinematicEvent(5.1f, new SoundEvent("Sound/Effects/Beep.ogg", 1));
76+
77+
cinematic.addListener(new CinematicEventListener() {
78+
79+
@Override
80+
public void onPlay(CinematicEvent cinematic) {
81+
flyCam.setEnabled(false);
82+
System.out.println("play");
83+
}
84+
85+
@Override
86+
public void onPause(CinematicEvent cinematic) {
87+
System.out.println("pause");
88+
}
89+
90+
@Override
91+
public void onStop(CinematicEvent cinematic) {
92+
flyCam.setEnabled(true);
93+
System.out.println("stop");
94+
}
95+
});
96+
97+
Cinematic copy = BinaryExporter.saveAndLoad(assetManager, cinematic);
98+
stateManager.detach(cinematic);
99+
100+
cinematic = copy;
101+
cinematic.setScene(rootNode);
102+
stateManager.attach(cinematic);
103+
104+
configureCamera();
105+
106+
initInputs();
107+
}
108+
109+
private void configureCamera() {
110+
flyCam.setMoveSpeed(25f);
111+
flyCam.setDragToRotate(true);
112+
cam.setLocation(Vector3f.UNIT_XYZ.mult(12));
113+
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
114+
}
115+
116+
private void setupLightsAndFilters() {
117+
DirectionalLight light = new DirectionalLight();
118+
light.setDirection(new Vector3f(0, -1, -1).normalizeLocal());
119+
light.setColor(ColorRGBA.White.mult(1.5f));
120+
rootNode.addLight(light);
121+
122+
if (renderer.getCaps().contains(Caps.GLSL100)) {
123+
DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, 512, 1);
124+
dlsr.setLight(light);
125+
dlsr.setShadowIntensity(0.4f);
126+
viewPort.addProcessor(dlsr);
127+
}
128+
}
129+
130+
private void setupModel() {
131+
teapot = assetManager.loadModel("Models/Teapot/Teapot.obj");
132+
teapot.setLocalTranslation(5, 0, 5);
133+
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
134+
mat.setColor("Color", ColorRGBA.Cyan);
135+
teapot.setMaterial(mat);
136+
teapot.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
137+
rootNode.attachChild(teapot);
138+
139+
// creating spatial animation for the teapot
140+
AnimFactory factory = new AnimFactory(20f, "teapotAnim", 30f);
141+
factory.addTimeTranslation(0, new Vector3f(5, 0, 5));
142+
factory.addTimeTranslation(4, new Vector3f(5, 0, -5));
143+
AnimClip animClip = factory.buildAnimation(teapot);
144+
145+
AnimComposer animComposer = new AnimComposer();
146+
animComposer.addAnimClip(animClip);
147+
teapot.addControl(animComposer);
148+
}
149+
150+
private void createCameraMotion() {
151+
CameraNode camNode = cinematic.bindCamera("topView", cam);
152+
camNode.setLocalTranslation(new Vector3f(0, 50, 0));
153+
camNode.lookAt(teapot.getLocalTranslation(), Vector3f.UNIT_Y);
154+
155+
CameraNode camNode2 = cinematic.bindCamera("aroundCam", cam);
156+
MotionPath path = new MotionPath();
157+
path.setCycle(true);
158+
path.addWayPoint(new Vector3f(20, 3, 0));
159+
path.addWayPoint(new Vector3f(0, 3, 20));
160+
path.addWayPoint(new Vector3f(-20, 3, 0));
161+
path.addWayPoint(new Vector3f(0, 3, -20));
162+
path.setCurveTension(0.83f);
163+
cameraMotionEvent = new MotionEvent(camNode2, path);
164+
cameraMotionEvent.setLoopMode(LoopMode.Loop);
165+
cameraMotionEvent.setLookAt(teapot.getWorldTranslation(), Vector3f.UNIT_Y);
166+
cameraMotionEvent.setDirectionType(MotionEvent.Direction.LookAt);
167+
}
168+
169+
private void initInputs() {
170+
inputManager.addMapping("togglePlay", new KeyTrigger(KeyInput.KEY_SPACE));
171+
ActionListener acl = new ActionListener() {
172+
173+
@Override
174+
public void onAction(String name, boolean keyPressed, float tpf) {
175+
if (name.equals("togglePlay") && keyPressed) {
176+
if (cinematic.getPlayState() == PlayState.Playing) {
177+
cinematic.pause();
178+
} else {
179+
System.out.println("Play");
180+
cinematic.play();
181+
}
182+
}
183+
184+
}
185+
};
186+
inputManager.addListener(acl, "togglePlay");
187+
}
188+
}

0 commit comments

Comments
 (0)