Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class AnimatedModel

  1. Object
  2. AnimatedModel

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();

Constructors

public AnimatedModel(GraphicsDevice device, GltfScene scene)Creates an instance of scene, uploading its textures through device the first time any instance needs them.

Methods

public GltfScene getScene()The scene this instance poses.
public Model[] getParts()The models that draw this instance, one per primitive.
public void addTo(GameView view)Adds every part to view.
public void removeFrom(GameView view)Removes every part from view.
public boolean hideNode(String name)Hides the primitives placed by the node named name and by every node below it.
public boolean setNodeVisible(String name, boolean show)Shows or hides the primitives placed by the node named name and by every node below it.
public void setTint(int argb)Multiplies every part’s color by argb, for a team tint or a hit flash.
public boolean play(String name, boolean loop)Starts the clip named name from its beginning, replacing whatever was playing.
public boolean crossFade(String name, float seconds, boolean loop)Blends from the current pose into the clip named name over seconds.
public String getClipName()The name of the clip playing, or null.
public float getClipTime()The position in the current clip, in seconds.
public void setClipTime(float seconds)Jumps to seconds in the current clip.
public boolean isFinished()True when a non-looping clip has reached its end.
public float getSpeed()The playback rate; 1 is the authored speed.
public void setSpeed(float speed)Sets the playback rate; 1 is the authored speed.
public void update(float deltaSeconds)Advances the animation by deltaSeconds and re-poses the vertices.
public void pose()Computes the current pose and writes the deformed vertices of every visible part.
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.
public AnimatedModel setPosition(float x, float y, float z)Places the instance.
public AnimatedModel setRotation(float degX, float degY, float degZ)Rotates the instance, in degrees about each axis.
public AnimatedModel setScale(float scale)Scales the instance uniformly.
public float getX()The x position.
public float getY()The y position.
public float getZ()The z position.
public float getRotationY()The rotation about the y axis in degrees.
public float getRotationX()The rotation about the x axis in degrees.
public float getRotationZ()The rotation about the z axis in degrees.
public float getScale()The uniform scale.
public boolean isVisible()True unless hidden with setVisible(false).
public void setVisible(boolean visible)Shows or hides every part.

Inherited methods

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 GraphicsDevice
the device that will draw the instance
scene GltfScene
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.