Search in sources :

Example 1 with ModelAnimation

use of com.badlogic.gdx.graphics.g3d.model.data.ModelAnimation in project libgdx by libgdx.

the class Model method loadAnimations.

protected void loadAnimations(Iterable<ModelAnimation> modelAnimations) {
    for (final ModelAnimation anim : modelAnimations) {
        Animation animation = new Animation();
        animation.id = anim.id;
        for (ModelNodeAnimation nanim : anim.nodeAnimations) {
            final Node node = getNode(nanim.nodeId);
            if (node == null)
                continue;
            NodeAnimation nodeAnim = new NodeAnimation();
            nodeAnim.node = node;
            if (nanim.translation != null) {
                nodeAnim.translation = new Array<NodeKeyframe<Vector3>>();
                nodeAnim.translation.ensureCapacity(nanim.translation.size);
                for (ModelNodeKeyframe<Vector3> kf : nanim.translation) {
                    if (kf.keytime > animation.duration)
                        animation.duration = kf.keytime;
                    nodeAnim.translation.add(new NodeKeyframe<Vector3>(kf.keytime, new Vector3(kf.value == null ? node.translation : kf.value)));
                }
            }
            if (nanim.rotation != null) {
                nodeAnim.rotation = new Array<NodeKeyframe<Quaternion>>();
                nodeAnim.rotation.ensureCapacity(nanim.rotation.size);
                for (ModelNodeKeyframe<Quaternion> kf : nanim.rotation) {
                    if (kf.keytime > animation.duration)
                        animation.duration = kf.keytime;
                    nodeAnim.rotation.add(new NodeKeyframe<Quaternion>(kf.keytime, new Quaternion(kf.value == null ? node.rotation : kf.value)));
                }
            }
            if (nanim.scaling != null) {
                nodeAnim.scaling = new Array<NodeKeyframe<Vector3>>();
                nodeAnim.scaling.ensureCapacity(nanim.scaling.size);
                for (ModelNodeKeyframe<Vector3> kf : nanim.scaling) {
                    if (kf.keytime > animation.duration)
                        animation.duration = kf.keytime;
                    nodeAnim.scaling.add(new NodeKeyframe<Vector3>(kf.keytime, new Vector3(kf.value == null ? node.scale : kf.value)));
                }
            }
            if ((nodeAnim.translation != null && nodeAnim.translation.size > 0) || (nodeAnim.rotation != null && nodeAnim.rotation.size > 0) || (nodeAnim.scaling != null && nodeAnim.scaling.size > 0))
                animation.nodeAnimations.add(nodeAnim);
        }
        if (animation.nodeAnimations.size > 0)
            animations.add(animation);
    }
}
Also used : NodeKeyframe(com.badlogic.gdx.graphics.g3d.model.NodeKeyframe) ModelNodeKeyframe(com.badlogic.gdx.graphics.g3d.model.data.ModelNodeKeyframe) Quaternion(com.badlogic.gdx.math.Quaternion) ModelAnimation(com.badlogic.gdx.graphics.g3d.model.data.ModelAnimation) ModelNode(com.badlogic.gdx.graphics.g3d.model.data.ModelNode) Node(com.badlogic.gdx.graphics.g3d.model.Node) Vector3(com.badlogic.gdx.math.Vector3) ModelAnimation(com.badlogic.gdx.graphics.g3d.model.data.ModelAnimation) NodeAnimation(com.badlogic.gdx.graphics.g3d.model.NodeAnimation) Animation(com.badlogic.gdx.graphics.g3d.model.Animation) ModelNodeAnimation(com.badlogic.gdx.graphics.g3d.model.data.ModelNodeAnimation) ModelNodeAnimation(com.badlogic.gdx.graphics.g3d.model.data.ModelNodeAnimation) NodeAnimation(com.badlogic.gdx.graphics.g3d.model.NodeAnimation) ModelNodeAnimation(com.badlogic.gdx.graphics.g3d.model.data.ModelNodeAnimation)

Example 2 with ModelAnimation

use of com.badlogic.gdx.graphics.g3d.model.data.ModelAnimation in project libgdx by libgdx.

the class G3dModelLoader method parseAnimations.

private void parseAnimations(ModelData model, JsonValue json) {
    JsonValue animations = json.get("animations");
    if (animations == null)
        return;
    model.animations.ensureCapacity(animations.size);
    for (JsonValue anim = animations.child; anim != null; anim = anim.next) {
        JsonValue nodes = anim.get("bones");
        if (nodes == null)
            continue;
        ModelAnimation animation = new ModelAnimation();
        model.animations.add(animation);
        animation.nodeAnimations.ensureCapacity(nodes.size);
        animation.id = anim.getString("id");
        for (JsonValue node = nodes.child; node != null; node = node.next) {
            ModelNodeAnimation nodeAnim = new ModelNodeAnimation();
            animation.nodeAnimations.add(nodeAnim);
            nodeAnim.nodeId = node.getString("boneId");
            // For backwards compatibility (version 0.1):
            JsonValue keyframes = node.get("keyframes");
            if (keyframes != null && keyframes.isArray()) {
                for (JsonValue keyframe = keyframes.child; keyframe != null; keyframe = keyframe.next) {
                    final float keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                    JsonValue translation = keyframe.get("translation");
                    if (translation != null && translation.size == 3) {
                        if (nodeAnim.translation == null)
                            nodeAnim.translation = new Array<ModelNodeKeyframe<Vector3>>();
                        ModelNodeKeyframe<Vector3> tkf = new ModelNodeKeyframe<Vector3>();
                        tkf.keytime = keytime;
                        tkf.value = new Vector3(translation.getFloat(0), translation.getFloat(1), translation.getFloat(2));
                        nodeAnim.translation.add(tkf);
                    }
                    JsonValue rotation = keyframe.get("rotation");
                    if (rotation != null && rotation.size == 4) {
                        if (nodeAnim.rotation == null)
                            nodeAnim.rotation = new Array<ModelNodeKeyframe<Quaternion>>();
                        ModelNodeKeyframe<Quaternion> rkf = new ModelNodeKeyframe<Quaternion>();
                        rkf.keytime = keytime;
                        rkf.value = new Quaternion(rotation.getFloat(0), rotation.getFloat(1), rotation.getFloat(2), rotation.getFloat(3));
                        nodeAnim.rotation.add(rkf);
                    }
                    JsonValue scale = keyframe.get("scale");
                    if (scale != null && scale.size == 3) {
                        if (nodeAnim.scaling == null)
                            nodeAnim.scaling = new Array<ModelNodeKeyframe<Vector3>>();
                        ModelNodeKeyframe<Vector3> skf = new ModelNodeKeyframe();
                        skf.keytime = keytime;
                        skf.value = new Vector3(scale.getFloat(0), scale.getFloat(1), scale.getFloat(2));
                        nodeAnim.scaling.add(skf);
                    }
                }
            } else {
                // Version 0.2:
                JsonValue translationKF = node.get("translation");
                if (translationKF != null && translationKF.isArray()) {
                    nodeAnim.translation = new Array<ModelNodeKeyframe<Vector3>>();
                    nodeAnim.translation.ensureCapacity(translationKF.size);
                    for (JsonValue keyframe = translationKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Vector3> kf = new ModelNodeKeyframe<Vector3>();
                        nodeAnim.translation.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue translation = keyframe.get("value");
                        if (translation != null && translation.size >= 3)
                            kf.value = new Vector3(translation.getFloat(0), translation.getFloat(1), translation.getFloat(2));
                    }
                }
                JsonValue rotationKF = node.get("rotation");
                if (rotationKF != null && rotationKF.isArray()) {
                    nodeAnim.rotation = new Array<ModelNodeKeyframe<Quaternion>>();
                    nodeAnim.rotation.ensureCapacity(rotationKF.size);
                    for (JsonValue keyframe = rotationKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Quaternion> kf = new ModelNodeKeyframe<Quaternion>();
                        nodeAnim.rotation.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue rotation = keyframe.get("value");
                        if (rotation != null && rotation.size >= 4)
                            kf.value = new Quaternion(rotation.getFloat(0), rotation.getFloat(1), rotation.getFloat(2), rotation.getFloat(3));
                    }
                }
                JsonValue scalingKF = node.get("scaling");
                if (scalingKF != null && scalingKF.isArray()) {
                    nodeAnim.scaling = new Array<ModelNodeKeyframe<Vector3>>();
                    nodeAnim.scaling.ensureCapacity(scalingKF.size);
                    for (JsonValue keyframe = scalingKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Vector3> kf = new ModelNodeKeyframe<Vector3>();
                        nodeAnim.scaling.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue scaling = keyframe.get("value");
                        if (scaling != null && scaling.size >= 3)
                            kf.value = new Vector3(scaling.getFloat(0), scaling.getFloat(1), scaling.getFloat(2));
                    }
                }
            }
        }
    }
}
Also used : Array(com.badlogic.gdx.utils.Array) ModelNodeKeyframe(com.badlogic.gdx.graphics.g3d.model.data.ModelNodeKeyframe) Quaternion(com.badlogic.gdx.math.Quaternion) ModelAnimation(com.badlogic.gdx.graphics.g3d.model.data.ModelAnimation) JsonValue(com.badlogic.gdx.utils.JsonValue) Vector3(com.badlogic.gdx.math.Vector3) ModelNodeAnimation(com.badlogic.gdx.graphics.g3d.model.data.ModelNodeAnimation)

Aggregations

ModelAnimation (com.badlogic.gdx.graphics.g3d.model.data.ModelAnimation)2 ModelNodeAnimation (com.badlogic.gdx.graphics.g3d.model.data.ModelNodeAnimation)2 ModelNodeKeyframe (com.badlogic.gdx.graphics.g3d.model.data.ModelNodeKeyframe)2 Quaternion (com.badlogic.gdx.math.Quaternion)2 Vector3 (com.badlogic.gdx.math.Vector3)2 Animation (com.badlogic.gdx.graphics.g3d.model.Animation)1 Node (com.badlogic.gdx.graphics.g3d.model.Node)1 NodeAnimation (com.badlogic.gdx.graphics.g3d.model.NodeAnimation)1 NodeKeyframe (com.badlogic.gdx.graphics.g3d.model.NodeKeyframe)1 ModelNode (com.badlogic.gdx.graphics.g3d.model.data.ModelNode)1 Array (com.badlogic.gdx.utils.Array)1 JsonValue (com.badlogic.gdx.utils.JsonValue)1