Animation in jME3¶
In 3D games, you do not only load static 3D models, you also want to be able to trigger animations in the model from the Java code.
Requirements¶
jMonkeyEngine can play imported animation clips. Many assets are authored in a 3D editor, though applications can also construct animation data programmatically.
What is required for an animated model? (See also: Animation terminology
-
For a skinned character, create an armature or skeleton (rigging).
-
Bind the mesh to the rig and assign vertex weights (skinning).
-
Author named animation clips using keyframes. Animation can also target transforms or morphs rather than a skeleton.
-
One model can contain several animations. You give every animation a name when you save it in the mesh editor.
Unless you download free models, or buy them from a 3D artist, you must create your animated models in an external mesh editor (for example, Blender) yourself.
What is required in your JME3-based Java class?
-
Locate the
AnimComposerassociated with the part of the loaded model you want to animate. A loaded scene can contain multiple controls. -
As many Layer per Composer as you need to play your animations. In simple cases one layer is enough to play animations for the whole model, sometimes you need two or more layers per model to play gestures and motions in parallel.
Code Samples¶
Controlling Animations¶
The AnimComposer¶
An imported animated model can include an AnimComposer with its available clips. Converting glTF to .j3o is not required. The composer may be on a child spatial; inspect the loaded hierarchy and list its clip names if needed.
Playing animations¶
AnimComposer animComposer = animatedModel.getControl(AnimComposer.class);
animComposer.setCurrentAction("Walk");
...
When you tell the AnimComposer to play the animation it will be looped by default. If you want it to only play an animation once, you can do the following:
Action walk = animComposer.action("Walk");
Tween doneTween = Tweens.callMethod(animComposer, "setCurrentAction", "Idle");
Action walkOnce = animComposer.actionSequence("WalkOnce", walk, doneTween);
animComposer.setCurrentAction("WalkOnce");
Playing animation on part of body¶
If you want to play an animation on part of the body, you need to create layers in the AnimComposer. You do this with the help of a SkinningControl, which should also have been created if your imported model had an armature.
SkinningControl sc = animatedModel.getControl(SkinningControl.class);
animComposer.makeLayer("UpperBody", ArmatureMask.createMask(sc.getArmature(), "Spine"));
animComposer.makeLayer("LowerBody", ArmatureMask.createMask(sc.getArmature(), "Hips"));
// Play the animation
animComposer.setCurrentAction("Walk", "UpperBody");
Further reading:¶
Some forum topics that contain more information on the animation system: