Class 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();
-
Constructor Summary
ConstructorsConstructorDescriptionAnimatedModel(GraphicsDevice device, GltfScene scene) Creates an instance ofscene, uploading its textures throughdevicethe first time any instance needs them. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds every part toview.booleanBlends from the current pose into the clip namednameoverseconds.The name of the clip playing, or null.floatThe position in the current clip, in seconds.booleangetNodeMatrix(String name, float[] dst16) Copies the current world matrix of the node namedname(in the instance's own space, beforesetPositionand friends) intodst16.Model[]getParts()The models that draw this instance, one per primitive.floatThe rotation about the x axis in degrees.floatThe rotation about the y axis in degrees.floatThe rotation about the z axis in degrees.floatgetScale()The uniform scale.getScene()The scene this instance poses.floatgetSpeed()The playback rate; 1 is the authored speed.floatgetX()The x position.floatgetY()The y position.floatgetZ()The z position.booleanHides the primitives placed by the node namednameand by every node below it.booleanTrue when a non-looping clip has reached its end.booleanTrue unless hidden withsetVisible(false).booleanStarts the clip namednamefrom its beginning, replacing whatever was playing.voidpose()Computes the current pose and writes the deformed vertices of every visible part.voidremoveFrom(GameView view) Removes every part fromview.voidsetClipTime(float seconds) Jumps tosecondsin the current clip.booleansetNodeVisible(String name, boolean show) Shows or hides the primitives placed by the node namednameand 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.voidsetSpeed(float speed) Sets the playback rate; 1 is the authored speed.voidsetTint(int argb) Multiplies every part's color byargb, for a team tint or a hit flash.voidsetVisible(boolean visible) Shows or hides every part.voidupdate(float deltaSeconds) Advances the animation bydeltaSecondsand re-poses the vertices.
-
Constructor Details
-
AnimatedModel
Creates an instance of
scene, uploading its textures throughdevicethe first time any instance needs them. Call it fromGameView#onSetup(GraphicsDevice)or later.Parameters
-
device: the device that will draw the instance -
scene: a scene fromGltfLoader#loadScene(byte[])
-
-
-
Method Details
-
getScene
The scene this instance poses. -
getParts
The models that draw this instance, one per primitive. -
addTo
Adds every part toview. -
removeFrom
Removes every part fromview. -
hideNode
Hides the primitives placed by the node named
nameand 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
Shows or hides the primitives placed by the node named
nameand 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 byargb, for a team tint or a hit flash. Pass0xffffffffto restore the file's colors. -
play
Starts the clip named
namefrom its beginning, replacing whatever was playing.Returns
false (and nothing changes) when the scene has no such clip
-
crossFade
Blends from the current pose into the clip named
nameoverseconds. 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
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 tosecondsin 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 bydeltaSecondsand re-poses the vertices. Call it once per frame, typically fromGameView#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
Copies the current world matrix of the node named
name(in the instance's own space, beforesetPositionand friends) intodst16. 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
Places the instance. -
setRotation
Rotates the instance, in degrees about each axis. -
setScale
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 withsetVisible(false). -
setVisible
public void setVisible(boolean visible) Shows or hides every part. A hidden instance skips its skinning work.
-