Просмотр исходного кода

resolve issue #1452 (convert jme3-examples to new system) (#1515)

* AnimEvent: set initial duration in constructor

* TestCinematic: complete the conversion to new animation system

* bugfix: assertion failures in AnimEvent

* convert TestJaime to the new animation system
Stephen Gold 4 лет назад
Родитель
Сommit
508c5cdbf8

+ 12 - 3
jme3-core/src/main/java/com/jme3/cinematic/events/AnimEvent.java

@@ -74,7 +74,8 @@ public class AnimEvent extends AbstractCinematicEvent {
     private String layerName;
 
     /**
-     * Instantiate an event.
+     * Instantiate a non-looping event to play the named action on the named
+     * layer of the specified AnimComposer.
      *
      * @param composer the Control that will play the animation (not null)
      * @param actionName the name of the animation action to be played
@@ -86,6 +87,14 @@ public class AnimEvent extends AbstractCinematicEvent {
         this.composer = composer;
         this.actionName = actionName;
         this.layerName = layerName;
+        /*
+         * Override initialDuration, which defaults to 10 seconds.
+         */
+        Action eventAction = composer.action(actionName);
+        if (eventAction == null) {
+            throw new IllegalStateException("No action named: " + actionName);
+        }
+        initialDuration = (float) eventAction.getLength();
     }
 
     /**
@@ -132,7 +141,7 @@ public class AnimEvent extends AbstractCinematicEvent {
         }
 
         if (currentAction != eventAction) {
-            composer.setCurrentAction(actionName);
+            composer.setCurrentAction(actionName, layerName);
             assert composer.getCurrentAction(layerName) == eventAction;
         }
 
@@ -209,7 +218,7 @@ public class AnimEvent extends AbstractCinematicEvent {
         Action currentAction = composer.getCurrentAction(layerName);
         Action eventAction = composer.action(actionName);
         if (currentAction != eventAction) {
-            composer.setCurrentAction(actionName);
+            composer.setCurrentAction(actionName, layerName);
             assert composer.getCurrentAction(layerName) == eventAction;
         }
 

+ 26 - 11
jme3-examples/src/main/java/jme3test/animation/TestCinematic.java

@@ -31,9 +31,9 @@
  */
 package jme3test.animation;
 
+import com.jme3.anim.AnimClip;
 import com.jme3.anim.AnimComposer;
-import com.jme3.animation.AnimControl;
-import com.jme3.animation.AnimationFactory;
+import com.jme3.anim.AnimFactory;
 import com.jme3.animation.LoopMode;
 import com.jme3.app.SimpleApplication;
 import com.jme3.cinematic.*;
@@ -100,28 +100,43 @@ public class TestCinematic extends SimpleApplication {
         createCameraMotion();
 
         //creating spatial animation for the teapot
-        AnimationFactory factory = new AnimationFactory(20, "teapotAnim");
+        AnimFactory factory = new AnimFactory(20f, "teapotAnim", 30f);
         factory.addTimeTranslation(0, new Vector3f(10, 0, 10));
         factory.addTimeTranslation(20, new Vector3f(10, 0, -10));
         factory.addTimeScale(10, new Vector3f(4, 4, 4));
         factory.addTimeScale(20, new Vector3f(1, 1, 1));
-        factory.addTimeRotationAngles(20, 0, 4 * FastMath.TWO_PI, 0);
-        AnimControl control = new AnimControl();
-        control.addAnim(factory.buildAnimation());
-        teapot.addControl(control);
+        for (int iStep = 1; iStep <= 12; ++iStep) {
+            float animationTime = iStep * 20f / 12; // in seconds
+            float yRotationAngle = iStep * FastMath.TWO_PI / 3; // in radians
+            factory.addTimeRotation(animationTime, 0f, yRotationAngle, 0f);
+        }
+        AnimClip spatialAnimation = factory.buildAnimation(teapot);
+        AnimComposer teapotComposer = new AnimComposer();
+        teapotComposer.addAnimClip(spatialAnimation);
+        teapot.addControl(teapotComposer);
 
         //fade in
         cinematic.addCinematicEvent(0, new FadeEvent(true));
         // cinematic.activateCamera(0, "aroundCam");
-        cinematic.addCinematicEvent(0, new AnimationEvent(teapot, "teapotAnim", LoopMode.DontLoop));
+
+        // 20-second spatial animation begins at t=0 seconds
+        AnimEvent teapotAnimEvent = new AnimEvent(teapotComposer, "teapotAnim",
+                AnimComposer.DEFAULT_LAYER);
+        cinematic.addCinematicEvent(0f, teapotAnimEvent);
+
         cinematic.addCinematicEvent(0, cameraMotionEvent);
         cinematic.addCinematicEvent(0, new SoundEvent("Sound/Environment/Nature.ogg", LoopMode.Loop));
         cinematic.addCinematicEvent(3f, new SoundEvent("Sound/Effects/kick.wav"));
         cinematic.addCinematicEvent(3, new SubtitleTrack(nifty, "start", 3, "jMonkey engine really kicks A..."));
         cinematic.addCinematicEvent(5.1f, new SoundEvent("Sound/Effects/Beep.ogg", 1));
-        AnimComposer composer = model.getControl(AnimComposer.class);
-        cinematic.addCinematicEvent(2f,
-                new AnimEvent(composer, "Walk", AnimComposer.DEFAULT_LAYER));
+
+        // 1.24-second bone animation loop begins at t=2 seconds
+        AnimComposer otoComposer = model.getControl(AnimComposer.class);
+        AnimEvent walkEvent = new AnimEvent(otoComposer, "Walk",
+                AnimComposer.DEFAULT_LAYER);
+        walkEvent.setLoopMode(LoopMode.Loop);
+        cinematic.addCinematicEvent(2f, walkEvent);
+
         cinematic.activateCamera(0, "topView");
         //  cinematic.activateCamera(10, "aroundCam");
 

+ 37 - 17
jme3-examples/src/main/java/jme3test/animation/TestJaime.java

@@ -31,8 +31,10 @@
  */
 package jme3test.animation;
 
-import com.jme3.animation.AnimControl;
-import com.jme3.animation.AnimationFactory;
+import com.jme3.anim.AnimClip;
+import com.jme3.anim.AnimComposer;
+import com.jme3.anim.AnimFactory;
+import com.jme3.anim.util.AnimMigrationUtils;
 import com.jme3.animation.LoopMode;
 import com.jme3.app.DebugKeysAppState;
 import com.jme3.app.FlyCamAppState;
@@ -42,7 +44,7 @@ import com.jme3.app.StatsAppState;
 import com.jme3.cinematic.Cinematic;
 import com.jme3.cinematic.MotionPath;
 import com.jme3.cinematic.PlayState;
-import com.jme3.cinematic.events.AnimationEvent;
+import com.jme3.cinematic.events.AnimEvent;
 import com.jme3.cinematic.events.MotionEvent;
 import com.jme3.input.KeyInput;
 import com.jme3.input.controls.ActionListener;
@@ -99,6 +101,7 @@ public class TestJaime  extends SimpleApplication {
     
     public Node LoadModel() {
         Node jaime = (Node)assetManager.loadModel("Models/Jaime/Jaime.j3o");
+        jaime = (Node) AnimMigrationUtils.migrate(jaime);
         jaime.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
         rootNode.attachChild(jaime);
         return jaime;
@@ -149,23 +152,40 @@ public class TestJaime  extends SimpleApplication {
         stateManager.attach(cinematic);
         
         jaime.move(0, 0, -3);
-        AnimationFactory af = new AnimationFactory(0.7f, "JumpForward");
+        AnimFactory af = new AnimFactory(0.7f, "JumpForward", 30f);
         af.addTimeTranslation(0, new Vector3f(0, 0, -3));
         af.addTimeTranslation(0.35f, new Vector3f(0, 1, -1.5f));
         af.addTimeTranslation(0.7f, new Vector3f(0, 0, 0));
-        jaime.getControl(AnimControl.class).addAnim(af.buildAnimation());
-   
-        cinematic.enqueueCinematicEvent(new AnimationEvent(jaime, "Idle",3, LoopMode.DontLoop));
-        float jumpStart = cinematic.enqueueCinematicEvent(new AnimationEvent(jaime, "JumpStart", LoopMode.DontLoop));
-        cinematic.addCinematicEvent(jumpStart+0.2f, new AnimationEvent(jaime, "JumpForward", LoopMode.DontLoop,1));        
-        cinematic.enqueueCinematicEvent( new AnimationEvent(jaime, "JumpEnd", LoopMode.DontLoop));                
-        cinematic.enqueueCinematicEvent( new AnimationEvent(jaime, "Punches", LoopMode.DontLoop));
-        cinematic.enqueueCinematicEvent( new AnimationEvent(jaime, "SideKick", LoopMode.DontLoop));        
-        float camStart = cinematic.enqueueCinematicEvent( new AnimationEvent(jaime, "Taunt", LoopMode.DontLoop));
-        cinematic.enqueueCinematicEvent( new AnimationEvent(jaime, "Idle",1, LoopMode.DontLoop));
-        cinematic.enqueueCinematicEvent( new AnimationEvent(jaime, "Wave", LoopMode.DontLoop));
-        cinematic.enqueueCinematicEvent( new AnimationEvent(jaime, "Idle", LoopMode.DontLoop));        
-        
+        AnimClip forwardClip = af.buildAnimation(jaime);
+        AnimComposer composer = jaime.getControl(AnimComposer.class);
+        composer.addAnimClip(forwardClip);
+
+        composer.makeLayer("SpatialLayer", null);
+        String boneLayer = AnimComposer.DEFAULT_LAYER;
+
+        cinematic.enqueueCinematicEvent(
+                new AnimEvent(composer, "Idle", boneLayer));
+        float jumpStart = cinematic.enqueueCinematicEvent(
+                new AnimEvent(composer, "JumpStart", boneLayer));
+        cinematic.addCinematicEvent(jumpStart + 0.2f,
+                new AnimEvent(composer, "JumpForward", "SpatialLayer"));
+        cinematic.enqueueCinematicEvent(
+                new AnimEvent(composer, "JumpEnd", boneLayer));
+        cinematic.enqueueCinematicEvent(
+                new AnimEvent(composer, "Punches", boneLayer));
+        cinematic.enqueueCinematicEvent(
+                new AnimEvent(composer, "SideKick", boneLayer));
+
+        float camStart = cinematic.enqueueCinematicEvent(
+                new AnimEvent(composer, "Taunt", boneLayer));
+        AnimEvent idleOneSecond = new AnimEvent(composer, "Idle", boneLayer);
+        idleOneSecond.setInitialDuration(1f);
+        cinematic.enqueueCinematicEvent(idleOneSecond);
+        cinematic.enqueueCinematicEvent(
+                new AnimEvent(composer, "Wave", boneLayer));
+        cinematic.enqueueCinematicEvent(
+                new AnimEvent(composer, "Idle", boneLayer));
+
         CameraNode camNode = cinematic.bindCamera("cam", cam);
         camNode.setLocalTranslation(new Vector3f(1.1f, 1.2f, 2.9f));
         camNode.lookAt(new Vector3f(0, 0.5f, 0), Vector3f.UNIT_Y);