Search in sources :

Example 1 with Quaternion

use of com.badlogic.gdx.math.Quaternion 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 Quaternion

use of com.badlogic.gdx.math.Quaternion in project libgdx by libgdx.

the class ModelInstance method copyAnimations.

private void copyAnimations(final Iterable<Animation> source, boolean shareKeyframes) {
    for (final Animation anim : source) {
        Animation animation = new Animation();
        animation.id = anim.id;
        animation.duration = anim.duration;
        for (final NodeAnimation nanim : anim.nodeAnimations) {
            final Node node = getNode(nanim.node.id);
            if (node == null)
                continue;
            NodeAnimation nodeAnim = new NodeAnimation();
            nodeAnim.node = node;
            if (shareKeyframes) {
                nodeAnim.translation = nanim.translation;
                nodeAnim.rotation = nanim.rotation;
                nodeAnim.scaling = nanim.scaling;
            } else {
                if (nanim.translation != null) {
                    nodeAnim.translation = new Array<NodeKeyframe<Vector3>>();
                    for (final NodeKeyframe<Vector3> kf : nanim.translation) nodeAnim.translation.add(new NodeKeyframe<Vector3>(kf.keytime, kf.value));
                }
                if (nanim.rotation != null) {
                    nodeAnim.rotation = new Array<NodeKeyframe<Quaternion>>();
                    for (final NodeKeyframe<Quaternion> kf : nanim.rotation) nodeAnim.rotation.add(new NodeKeyframe<Quaternion>(kf.keytime, kf.value));
                }
                if (nanim.scaling != null) {
                    nodeAnim.scaling = new Array<NodeKeyframe<Vector3>>();
                    for (final NodeKeyframe<Vector3> kf : nanim.scaling) nodeAnim.scaling.add(new NodeKeyframe<Vector3>(kf.keytime, kf.value));
                }
            }
            if (nodeAnim.translation != null || nodeAnim.rotation != null || nodeAnim.scaling != null)
                animation.nodeAnimations.add(nodeAnim);
        }
        if (animation.nodeAnimations.size > 0)
            animations.add(animation);
    }
}
Also used : NodeKeyframe(com.badlogic.gdx.graphics.g3d.model.NodeKeyframe) Quaternion(com.badlogic.gdx.math.Quaternion) Node(com.badlogic.gdx.graphics.g3d.model.Node) Animation(com.badlogic.gdx.graphics.g3d.model.Animation) NodeAnimation(com.badlogic.gdx.graphics.g3d.model.NodeAnimation) Vector3(com.badlogic.gdx.math.Vector3) NodeAnimation(com.badlogic.gdx.graphics.g3d.model.NodeAnimation)

Example 3 with Quaternion

use of com.badlogic.gdx.math.Quaternion 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)

Example 4 with Quaternion

use of com.badlogic.gdx.math.Quaternion in project nhglib by VoidZombie.

the class PhysicsSystem method process.

@Override
protected void process(int entityId) {
    NodeComponent nodeComponent = nodeMapper.get(entityId);
    RigidBodyComponent bodyComponent = rigidBodyMapper.get(entityId);
    if (!bodyComponent.isAdded()) {
        Matrix4 initialTransform = new Matrix4();
        Vector3 trn = nodeComponent.getTranslation();
        Vector3 scl = new Vector3(1, 1, 1);
        Quaternion rtn = nodeComponent.getRotationQuaternion();
        initialTransform.set(trn, rtn, scl);
        bodyComponent.addToWorld(dynamicsWorld, initialTransform);
    } else {
        nodeComponent.setTranslation(bodyComponent.getTranslation());
        nodeComponent.setRotation(bodyComponent.getRotation());
        nodeComponent.applyTransforms();
    }
}
Also used : RigidBodyComponent(io.github.voidzombie.nhglib.runtime.ecs.components.physics.RigidBodyComponent) Quaternion(com.badlogic.gdx.math.Quaternion) NodeComponent(io.github.voidzombie.nhglib.runtime.ecs.components.scenes.NodeComponent) Vector3(com.badlogic.gdx.math.Vector3) Matrix4(com.badlogic.gdx.math.Matrix4)

Example 5 with Quaternion

use of com.badlogic.gdx.math.Quaternion in project nhglib by VoidZombie.

the class NhgG3dModelLoader 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 : Quaternion(com.badlogic.gdx.math.Quaternion) Vector3(com.badlogic.gdx.math.Vector3)

Aggregations

Quaternion (com.badlogic.gdx.math.Quaternion)12 Vector3 (com.badlogic.gdx.math.Vector3)9 NodeKeyframe (com.badlogic.gdx.graphics.g3d.model.NodeKeyframe)3 ModelNode (com.badlogic.gdx.graphics.g3d.model.data.ModelNode)3 Matrix4 (com.badlogic.gdx.math.Matrix4)3 Animation (com.badlogic.gdx.graphics.g3d.model.Animation)2 Node (com.badlogic.gdx.graphics.g3d.model.Node)2 NodeAnimation (com.badlogic.gdx.graphics.g3d.model.NodeAnimation)2 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 ModelNodePart (com.badlogic.gdx.graphics.g3d.model.data.ModelNodePart)2 Array (com.badlogic.gdx.utils.Array)2 JsonValue (com.badlogic.gdx.utils.JsonValue)2 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)1 ModelData (com.badlogic.gdx.graphics.g3d.model.data.ModelData)1 ModelMaterial (com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial)1 ModelMesh (com.badlogic.gdx.graphics.g3d.model.data.ModelMesh)1 ModelMeshPart (com.badlogic.gdx.graphics.g3d.model.data.ModelMeshPart)1 FloatArray (com.badlogic.gdx.utils.FloatArray)1