Class AnimatedModel

java.lang.Object
com.codename1.gaming.AnimatedModel

public class AnimatedModel extends Object

One posed, animated instance of a GltfScene: a character, a creature, a door that swings. It plays the scene's animation clips, cross fades between them, deforms skinned parts with their skeleton and carries rigid parts (a sword in a hand bone, a helmet on the head) along with the bones they hang from.

Each triangle primitive of the scene becomes one Model with its own vertex buffer, so add the instance to a view with addTo(GameView) and move it with setPosition, setRotation and setScale -- every part follows. Skinning runs on the CPU and re-uploads the posed vertices when the pose changes, which keeps it identical on every port and needs nothing from the shaders.

Many instances can share one scene: the vertex data, the clips and the uploaded textures are shared; only the posed buffers are per instance.

import com.codename1.gaming.*;
import com.codename1.gpu.*;
import com.codename1.io.Log;
import com.codename1.ui.*;
import com.codename1.ui.layouts.BorderLayout;
import java.io.InputStream;

class Knight extends GameView {
    private AnimatedModel unit;

    protected void onSetup(GraphicsDevice device) {
        try {
            InputStream in = Display.getInstance().getResourceAsStream(getClass(), "/knight.glb");
            GltfScene knight = GltfLoader.loadScene(in);
            in.close();
            unit = new AnimatedModel(device, knight);
            unit.hideNode("Shield_Spike");          // the file carries every prop
            unit.play("Walking_A", true);
            unit.addTo(this);
        } catch (Exception err) {
            Log.e(err);
        }
    }

    protected void update(double deltaSeconds) {
        if (unit != null) {
            unit.update((float) deltaSeconds);
        }
    }

    void attack() {
        unit.crossFade("1H_Melee_Attack_Chop", 0.15f, false);
    }
}

Form f = new Form("Knight", new BorderLayout());
f.add(BorderLayout.CENTER, new Knight());
f.show();
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an instance of scene, uploading its textures through device the first time any instance needs them.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds every part to view.
    boolean
    crossFade(String name, float seconds, boolean loop)
    Blends from the current pose into the clip named name over seconds.
    The name of the clip playing, or null.
    float
    The position in the current clip, in seconds.
    boolean
    getNodeMatrix(String name, float[] dst16)
    Copies the current world matrix of the node named name (in the instance's own space, before setPosition and friends) into dst16.
    The models that draw this instance, one per primitive.
    float
    The rotation about the x axis in degrees.
    float
    The rotation about the y axis in degrees.
    float
    The rotation about the z axis in degrees.
    float
    The uniform scale.
    The scene this instance poses.
    float
    The playback rate; 1 is the authored speed.
    float
    The x position.
    float
    The y position.
    float
    The z position.
    boolean
    Hides the primitives placed by the node named name and by every node below it.
    boolean
    True when a non-looping clip has reached its end.
    boolean
    True unless hidden with setVisible(false).
    boolean
    play(String name, boolean loop)
    Starts the clip named name from its beginning, replacing whatever was playing.
    void
    Computes the current pose and writes the deformed vertices of every visible part.
    void
    Removes every part from view.
    void
    setClipTime(float seconds)
    Jumps to seconds in the current clip.
    boolean
    setNodeVisible(String name, boolean show)
    Shows or hides the primitives placed by the node named name and by every node below it.
    setPosition(float x, float y, float z)
    Places the instance.
    setRotation(float degX, float degY, float degZ)
    Rotates the instance, in degrees about each axis.
    setScale(float scale)
    Scales the instance uniformly.
    void
    setSpeed(float speed)
    Sets the playback rate; 1 is the authored speed.
    void
    setTint(int argb)
    Multiplies every part's color by argb, for a team tint or a hit flash.
    void
    setVisible(boolean visible)
    Shows or hides every part.
    void
    update(float deltaSeconds)
    Advances the animation by deltaSeconds and re-poses the vertices.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AnimatedModel

      public AnimatedModel(GraphicsDevice device, GltfScene scene)

      Creates an instance of scene, uploading its textures through device the first time any instance needs them. Call it from GameView#onSetup(GraphicsDevice) or later.

      Parameters
      • device: the device that will draw the instance

      • scene: a scene from GltfLoader#loadScene(byte[])

  • Method Details

    • getScene

      public GltfScene getScene()
      The scene this instance poses.
    • getParts

      public Model[] getParts()
      The models that draw this instance, one per primitive.
    • addTo

      public void addTo(GameView view)
      Adds every part to view.
    • removeFrom

      public void removeFrom(GameView view)
      Removes every part from view.
    • hideNode

      public boolean hideNode(String name)

      Hides the primitives placed by the node named name and by every node below it. Character files often carry every weapon and accessory a variant might hold; hide the ones this instance does not use.

      Returns

      true when a node of that name exists

    • setNodeVisible

      public boolean setNodeVisible(String name, boolean show)

      Shows or hides the primitives placed by the node named name and by every node below it.

      Returns

      true when a node of that name exists

    • setTint

      public void setTint(int argb)
      Multiplies every part's color by argb, for a team tint or a hit flash. Pass 0xffffffff to restore the file's colors.
    • play

      public boolean play(String name, boolean loop)

      Starts the clip named name from its beginning, replacing whatever was playing.

      Returns

      false (and nothing changes) when the scene has no such clip

    • crossFade

      public boolean crossFade(String name, float seconds, boolean loop)

      Blends from the current pose into the clip named name over seconds. Calling it with the clip that is already playing and looping does nothing, so it is safe to call every frame with the state's clip.

      Returns

      false (and nothing changes) when the scene has no such clip

    • getClipName

      public String getClipName()
      The name of the clip playing, or null.
    • getClipTime

      public float getClipTime()
      The position in the current clip, in seconds.
    • setClipTime

      public void setClipTime(float seconds)
      Jumps to seconds in the current clip.
    • isFinished

      public boolean isFinished()
      True when a non-looping clip has reached its end.
    • getSpeed

      public float getSpeed()
      The playback rate; 1 is the authored speed.
    • setSpeed

      public void setSpeed(float speed)
      Sets the playback rate; 1 is the authored speed.
    • update

      public void update(float deltaSeconds)
      Advances the animation by deltaSeconds and re-poses the vertices. Call it once per frame, typically from GameView#update(double).
    • pose

      public void pose()
      Computes the current pose and writes the deformed vertices of every visible part. update(float) calls this; call it directly only after changing the clip time of a paused instance.
    • getNodeMatrix

      public boolean getNodeMatrix(String name, float[] dst16)

      Copies the current world matrix of the node named name (in the instance's own space, before setPosition and friends) into dst16. Use it to attach an effect to a bone, such as a spark at a sword tip.

      Returns

      false when no node has that name

    • setPosition

      public AnimatedModel setPosition(float x, float y, float z)
      Places the instance.
    • setRotation

      public AnimatedModel setRotation(float degX, float degY, float degZ)
      Rotates the instance, in degrees about each axis.
    • setScale

      public AnimatedModel setScale(float scale)
      Scales the instance uniformly.
    • getX

      public float getX()
      The x position.
    • getY

      public float getY()
      The y position.
    • getZ

      public float getZ()
      The z position.
    • getRotationY

      public float getRotationY()
      The rotation about the y axis in degrees.
    • getRotationX

      public float getRotationX()
      The rotation about the x axis in degrees.
    • getRotationZ

      public float getRotationZ()
      The rotation about the z axis in degrees.
    • getScale

      public float getScale()
      The uniform scale.
    • isVisible

      public boolean isVisible()
      True unless hidden with setVisible(false).
    • setVisible

      public void setVisible(boolean visible)
      Shows or hides every part. A hidden instance skips its skinning work.