Search in sources :

Example 1 with GLTFNode

use of org.terasology.engine.rendering.gltf.model.GLTFNode in project Terasology by MovingBlocks.

the class GLTFCommonFormat method loadBones.

protected List<Bone> loadBones(GLTF gltf, GLTFSkin skin, List<byte[]> loadedBuffers) {
    List<Bone> bones = new ArrayList<>();
    TIntIntMap boneToJoint = new TIntIntHashMap();
    List<Matrix4f> inverseMats = loadInverseMats(skin.getInverseBindMatrices(), skin.getJoints().size(), gltf, loadedBuffers);
    for (int i = 0; i < skin.getJoints().size(); i++) {
        int nodeIndex = skin.getJoints().get(i);
        GLTFNode node = gltf.getNodes().get(nodeIndex);
        Vector3f position = new Vector3f();
        Quaternionf rotation = new Quaternionf();
        Vector3f scale = new Vector3f(1, 1, 1);
        if (node.getTranslation() != null) {
            position.set(node.getTranslation());
        }
        if (node.getRotation() != null) {
            rotation.set(node.getRotation());
        }
        if (node.getScale() != null) {
            scale.set(node.getScale());
        }
        String boneName = node.getName();
        if (Strings.isNullOrEmpty(boneName)) {
            boneName = "bone_" + i;
        }
        Bone bone = new Bone(i, boneName, new Matrix4f().translationRotateScale(position, rotation, scale));
        bone.setInverseBindMatrix(inverseMats.get(i));
        bones.add(bone);
        boneToJoint.put(nodeIndex, i);
    }
    for (int i = 0; i < skin.getJoints().size(); i++) {
        int nodeIndex = skin.getJoints().get(i);
        GLTFNode node = gltf.getNodes().get(nodeIndex);
        Bone bone = bones.get(i);
        TIntIterator iterator = node.getChildren().iterator();
        while (iterator.hasNext()) {
            bone.addChild(bones.get(boneToJoint.get(iterator.next())));
        }
    }
    return bones;
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TFloatArrayList(gnu.trove.list.array.TFloatArrayList) Quaternionf(org.joml.Quaternionf) TIntIntMap(gnu.trove.map.TIntIntMap) Matrix4f(org.joml.Matrix4f) Vector3f(org.joml.Vector3f) GLTFNode(org.terasology.engine.rendering.gltf.model.GLTFNode) Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone) TIntIntHashMap(gnu.trove.map.hash.TIntIntHashMap)

Example 2 with GLTFNode

use of org.terasology.engine.rendering.gltf.model.GLTFNode in project Terasology by MovingBlocks.

the class GLTFMeshFormat method getMatrix.

private Matrix4f getMatrix(GLTF gltf, int nodeIndex) {
    Matrix4f transform = new Matrix4f();
    if (nodeIndex != -1) {
        GLTFNode node = gltf.getNodes().get(nodeIndex);
        if (node.getMatrix() == null) {
            Vector3f position = new Vector3f();
            Quaternionf rotation = new Quaternionf();
            Vector3f scale = new Vector3f(1, 1, 1);
            if (node.getTranslation() != null) {
                position.set(node.getTranslation());
            }
            if (node.getRotation() != null) {
                rotation.set(node.getRotation());
            }
            if (node.getScale() != null) {
                scale.set(node.getScale());
            }
            transform.translationRotateScale(position, rotation, scale);
        } else {
            transform.set(node.getMatrix());
        }
        int parentNodeIndex = getParentNode(gltf, nodeIndex);
        Matrix4f parentTransform = getMatrix(gltf, parentNodeIndex);
        // Must be multiplied in the right order
        parentTransform.mul(transform);
        transform.set(parentTransform);
    }
    return transform;
}
Also used : Matrix4f(org.joml.Matrix4f) Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) GLTFNode(org.terasology.engine.rendering.gltf.model.GLTFNode)

Aggregations

Matrix4f (org.joml.Matrix4f)2 Quaternionf (org.joml.Quaternionf)2 Vector3f (org.joml.Vector3f)2 GLTFNode (org.terasology.engine.rendering.gltf.model.GLTFNode)2 TIntIterator (gnu.trove.iterator.TIntIterator)1 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 TIntIntMap (gnu.trove.map.TIntIntMap)1 TIntIntHashMap (gnu.trove.map.hash.TIntIntHashMap)1 ArrayList (java.util.ArrayList)1 Bone (org.terasology.engine.rendering.assets.skeletalmesh.Bone)1