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