Search in sources :

Example 1 with GLTF

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

the class GLTFMeshFormat method load.

@Override
public MeshData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
    try (Reader in = new InputStreamReader(inputs.get(0).openStream())) {
        GLTF gltf = gson.fromJson(in, GLTF.class);
        checkVersionSupported(urn, gltf);
        checkMeshPresent(urn, gltf);
        GLTFMesh gltfMesh = gltf.getMeshes().get(0);
        checkPrimitivePresent(urn, gltfMesh);
        GLTFPrimitive gltfPrimitive = gltfMesh.getPrimitives().get(0);
        List<byte[]> loadedBuffers = loadBinaryBuffers(urn, gltf);
        StandardMeshData meshData = new StandardMeshData();
        for (MeshAttributeSemantic semantic : MeshAttributeSemantic.values()) {
            GLTFAccessor gltfAccessor = getAccessor(semantic, gltfPrimitive, gltf);
            if (gltfAccessor != null && gltfAccessor.getBufferView() != null) {
                GLTFBufferView bufferView = gltf.getBufferViews().get(gltfAccessor.getBufferView());
                switch(semantic) {
                    case Position:
                        GLTFAttributeMapping.readVec3FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.position);
                        break;
                    case Normal:
                        GLTFAttributeMapping.readVec3FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.normal);
                        break;
                    case Texcoord_0:
                        GLTFAttributeMapping.readVec2FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.uv0);
                        break;
                    case Texcoord_1:
                        GLTFAttributeMapping.readVec2FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.uv1);
                        break;
                    case Color_0:
                        GLTFAttributeMapping.readColor4FBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, meshData.color0);
                        break;
                }
            }
        }
        GLTFAccessor indicesAccessor = getIndicesAccessor(gltfPrimitive, gltf, urn);
        if (indicesAccessor.getBufferView() == null) {
            throw new IOException("Missing buffer view for indices accessor in " + urn);
        }
        GLTFBufferView indicesBuffer = gltf.getBufferViews().get(indicesAccessor.getBufferView());
        checkIndicesBuffer(indicesBuffer);
        TIntArrayList indices = new TIntArrayList();
        readBuffer(loadedBuffers.get(indicesBuffer.getBuffer()), indicesAccessor, indicesBuffer, indices);
        for (int x = 0; x < indices.size(); x++) {
            meshData.indices.put(indices.get(x));
        }
        return meshData;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) StandardMeshData(org.terasology.engine.rendering.assets.mesh.StandardMeshData) GLTFMesh(org.terasology.engine.rendering.gltf.model.GLTFMesh) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) TIntArrayList(gnu.trove.list.array.TIntArrayList) GLTFBufferView(org.terasology.engine.rendering.gltf.model.GLTFBufferView) GLTF(org.terasology.engine.rendering.gltf.model.GLTF) GLTFPrimitive(org.terasology.engine.rendering.gltf.model.GLTFPrimitive) GLTFAccessor(org.terasology.engine.rendering.gltf.model.GLTFAccessor)

Example 2 with GLTF

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

the class GLTFAnimationFormat method load.

@Override
public MeshAnimationBundleData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
    try (Reader in = new InputStreamReader(inputs.get(0).openStream())) {
        GLTF gltf = gson.fromJson(in, GLTF.class);
        checkVersionSupported(urn, gltf);
        List<byte[]> loadedBuffers = loadBinaryBuffers(urn, gltf);
        if (gltf.getSkins().isEmpty()) {
            throw new IOException("Skeletal mesh '" + urn + "' missing skin");
        }
        GLTFSkin skin = gltf.getSkins().get(0);
        List<String> boneNames = Lists.newArrayList();
        TIntList boneParents = new TIntArrayList();
        TIntIntMap nodeToJoint = new TIntIntHashMap();
        for (int i = 0; i < skin.getJoints().size(); i++) {
            nodeToJoint.put(skin.getJoints().get(i), i);
        }
        List<Bone> bones = loadBones(gltf, skin, loadedBuffers);
        bones.forEach(x -> boneNames.add(x.getName()));
        bones.forEach(x -> {
            if (x.getParentIndex() != -1) {
                boneParents.add(x.getParentIndex());
            } else {
                boneParents.add(MeshAnimationData.NO_PARENT);
            }
        });
        Map<ResourceUrn, MeshAnimationData> animations = new HashMap<>();
        for (int index = 0; index < gltf.getAnimations().size(); ++index) {
            GLTFAnimation gltfAnimation = gltf.getAnimations().get(index);
            String name = gltfAnimation.getName();
            if (Strings.isNullOrEmpty(name)) {
                name = "anim_" + index;
            }
            animations.put(new ResourceUrn(urn, name), loadAnimation(gltf, gltfAnimation, loadedBuffers, nodeToJoint, boneNames, boneParents, bones));
        }
        return new MeshAnimationBundleData(animations);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) TIntIntHashMap(gnu.trove.map.hash.TIntIntHashMap) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) MeshAnimationBundleData(org.terasology.engine.rendering.assets.animation.MeshAnimationBundleData) IOException(java.io.IOException) TIntArrayList(gnu.trove.list.array.TIntArrayList) TIntIntMap(gnu.trove.map.TIntIntMap) GLTFSkin(org.terasology.engine.rendering.gltf.model.GLTFSkin) GLTF(org.terasology.engine.rendering.gltf.model.GLTF) MeshAnimationData(org.terasology.engine.rendering.assets.animation.MeshAnimationData) Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone) TIntList(gnu.trove.list.TIntList) TIntIntHashMap(gnu.trove.map.hash.TIntIntHashMap) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) GLTFAnimation(org.terasology.engine.rendering.gltf.model.GLTFAnimation)

Example 3 with GLTF

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

the class GLTFSkeletalMeshFormat method load.

@Override
public SkeletalMeshData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
    try (Reader in = new InputStreamReader(inputs.get(0).openStream())) {
        GLTF gltf = gson.fromJson(in, GLTF.class);
        checkVersionSupported(urn, gltf);
        checkMeshPresent(urn, gltf);
        GLTFSkin skin = gltf.getSkins().get(0);
        GLTFMesh gltfMesh = gltf.getMeshes().get(0);
        checkPrimitivePresent(urn, gltfMesh);
        GLTFPrimitive gltfPrimitive = gltfMesh.getPrimitives().get(0);
        List<byte[]> loadedBuffers = loadBinaryBuffers(urn, gltf);
        SkeletalMeshDataBuilder builder = new SkeletalMeshDataBuilder();
        List<Bone> bones = loadBones(gltf, skin, loadedBuffers);
        for (Bone bone : bones) {
            builder.addBone(bone);
        }
        List<Vector3f> positions = loadVector3fList(MeshAttributeSemantic.Position, gltfPrimitive, gltf, loadedBuffers);
        List<Vector3f> normals = loadVector3fList(MeshAttributeSemantic.Normal, gltfPrimitive, gltf, loadedBuffers);
        TIntList joints = readIntBuffer(MeshAttributeSemantic.Joints_0, gltfPrimitive, gltf, loadedBuffers);
        TFloatList weights = readFloatBuffer(MeshAttributeSemantic.Weights_0, gltfPrimitive, gltf, loadedBuffers);
        List<BoneWeight> boneWeights = new ArrayList<>();
        for (int index = 0; index < positions.size(); index++) {
            TIntList weightJoints = new TIntArrayList();
            TFloatList weightBiases = new TFloatArrayList();
            for (int i = 0; i < 4; i++) {
                if (weights.get(4 * index + i) > 0) {
                    weightBiases.add(weights.get(4 * index + i));
                    weightJoints.add(joints.get(4 * index + i));
                }
            }
            boneWeights.add(new BoneWeight(weightBiases, weightJoints));
        }
        builder.addVertices(positions);
        builder.addNormals(normals);
        builder.addWeights(boneWeights);
        builder.setUvs(loadVector2fList(MeshAttributeSemantic.Texcoord_0, gltfPrimitive, gltf, loadedBuffers));
        GLTFAccessor indicesAccessor = getIndicesAccessor(gltfPrimitive, gltf, urn);
        if (indicesAccessor.getBufferView() == null) {
            throw new IOException("Missing buffer view for indices accessor in " + urn);
        }
        GLTFBufferView indicesBuffer = gltf.getBufferViews().get(indicesAccessor.getBufferView());
        checkIndicesBuffer(indicesBuffer);
        TIntList indicies = new TIntArrayList();
        readBuffer(loadedBuffers.get(indicesBuffer.getBuffer()), indicesAccessor, indicesBuffer, indicies);
        builder.setIndices(indicies);
        if (gltf.getSkins().isEmpty()) {
            throw new IOException("Skeletal mesh '" + urn + "' missing skin");
        }
        return builder.build();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) GLTFMesh(org.terasology.engine.rendering.gltf.model.GLTFMesh) TFloatList(gnu.trove.list.TFloatList) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TFloatArrayList(gnu.trove.list.array.TFloatArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) TIntArrayList(gnu.trove.list.array.TIntArrayList) TFloatArrayList(gnu.trove.list.array.TFloatArrayList) GLTFSkin(org.terasology.engine.rendering.gltf.model.GLTFSkin) GLTFBufferView(org.terasology.engine.rendering.gltf.model.GLTFBufferView) BoneWeight(org.terasology.engine.rendering.assets.skeletalmesh.BoneWeight) GLTF(org.terasology.engine.rendering.gltf.model.GLTF) SkeletalMeshDataBuilder(org.terasology.engine.rendering.assets.skeletalmesh.SkeletalMeshDataBuilder) Vector3f(org.joml.Vector3f) GLTFPrimitive(org.terasology.engine.rendering.gltf.model.GLTFPrimitive) Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone) TIntList(gnu.trove.list.TIntList) GLTFAccessor(org.terasology.engine.rendering.gltf.model.GLTFAccessor)

Aggregations

TIntArrayList (gnu.trove.list.array.TIntArrayList)3 IOException (java.io.IOException)3 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 GLTF (org.terasology.engine.rendering.gltf.model.GLTF)3 TIntList (gnu.trove.list.TIntList)2 Bone (org.terasology.engine.rendering.assets.skeletalmesh.Bone)2 GLTFAccessor (org.terasology.engine.rendering.gltf.model.GLTFAccessor)2 GLTFBufferView (org.terasology.engine.rendering.gltf.model.GLTFBufferView)2 GLTFMesh (org.terasology.engine.rendering.gltf.model.GLTFMesh)2 GLTFPrimitive (org.terasology.engine.rendering.gltf.model.GLTFPrimitive)2 GLTFSkin (org.terasology.engine.rendering.gltf.model.GLTFSkin)2 TFloatList (gnu.trove.list.TFloatList)1 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)1 TIntIntMap (gnu.trove.map.TIntIntMap)1 TIntIntHashMap (gnu.trove.map.hash.TIntIntHashMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Vector3f (org.joml.Vector3f)1 MeshAnimationBundleData (org.terasology.engine.rendering.assets.animation.MeshAnimationBundleData)1