Search in sources :

Example 1 with GLTFBufferView

use of org.terasology.engine.rendering.gltf.model.GLTFBufferView 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 GLTFBufferView

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

the class GLTFAnimationFormat method getFloats.

private TFloatList getFloats(GLTF gltf, List<byte[]> loadedBuffers, int accessorIndex) throws IOException {
    GLTFAccessor accessor = gltf.getAccessors().get(accessorIndex);
    GLTFBufferView bufferView = gltf.getBufferViews().get(accessor.getBufferView());
    TFloatList floats = new TFloatArrayList();
    readBuffer(loadedBuffers.get(bufferView.getBuffer()), accessor, bufferView, floats);
    return floats;
}
Also used : GLTFBufferView(org.terasology.engine.rendering.gltf.model.GLTFBufferView) TFloatList(gnu.trove.list.TFloatList) GLTFAccessor(org.terasology.engine.rendering.gltf.model.GLTFAccessor) TFloatArrayList(gnu.trove.list.array.TFloatArrayList)

Example 3 with GLTFBufferView

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

the class GLTFCommonFormat method readFloatBuffer.

protected TFloatList readFloatBuffer(MeshAttributeSemantic semantic, GLTFPrimitive gltfPrimitive, GLTF gltf, List<byte[]> loadedBuffers) throws IOException {
    GLTFAccessor gltfAccessor = getAccessor(semantic, gltfPrimitive, gltf);
    if (gltfAccessor != null && gltfAccessor.getBufferView() != null) {
        GLTFBufferView bufferView = gltf.getBufferViews().get(gltfAccessor.getBufferView());
        TFloatList floats = new TFloatArrayList();
        readBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, floats);
        return floats;
    }
    return new TFloatArrayList();
}
Also used : GLTFBufferView(org.terasology.engine.rendering.gltf.model.GLTFBufferView) TFloatList(gnu.trove.list.TFloatList) GLTFAccessor(org.terasology.engine.rendering.gltf.model.GLTFAccessor) TFloatArrayList(gnu.trove.list.array.TFloatArrayList)

Example 4 with GLTFBufferView

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

the class GLTFCommonFormat method loadMat4fList.

protected List<Matrix4f> loadMat4fList(int inverseBindMatrices, GLTF gltf, List<byte[]> loadedBuffers) {
    GLTFAccessor accessor = gltf.getAccessors().get(inverseBindMatrices);
    GLTFBufferView bufferView = gltf.getBufferViews().get(accessor.getBufferView());
    byte[] buffer = loadedBuffers.get(bufferView.getBuffer());
    TFloatList values = new TFloatArrayList();
    readBuffer(buffer, accessor, bufferView, values);
    List<Matrix4f> matricies = Lists.newArrayList();
    for (int i = 0; i < values.size(); i += 16) {
        Matrix4f mat = new Matrix4f(values.get(i), values.get(i + 1), values.get(i + 2), values.get(i + 3), values.get(i + 4), values.get(i + 5), values.get(i + 6), values.get(i + 7), values.get(i + 8), values.get(i + 9), values.get(i + 10), values.get(i + 11), values.get(i + 12), values.get(i + 13), values.get(i + 14), values.get(i + 15));
        matricies.add(mat);
    }
    return matricies;
}
Also used : GLTFBufferView(org.terasology.engine.rendering.gltf.model.GLTFBufferView) Matrix4f(org.joml.Matrix4f) TFloatList(gnu.trove.list.TFloatList) GLTFAccessor(org.terasology.engine.rendering.gltf.model.GLTFAccessor) TFloatArrayList(gnu.trove.list.array.TFloatArrayList)

Example 5 with GLTFBufferView

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

the class GLTFCommonFormat method readIntBuffer.

protected TIntList readIntBuffer(MeshAttributeSemantic semantic, GLTFPrimitive gltfPrimitive, GLTF gltf, List<byte[]> loadedBuffers) throws IOException {
    GLTFAccessor gltfAccessor = getAccessor(semantic, gltfPrimitive, gltf);
    if (gltfAccessor != null && gltfAccessor.getBufferView() != null) {
        GLTFBufferView bufferView = gltf.getBufferViews().get(gltfAccessor.getBufferView());
        TIntList ints = new TIntArrayList();
        readBuffer(loadedBuffers.get(bufferView.getBuffer()), gltfAccessor, bufferView, ints);
        return ints;
    }
    throw new IOException("Cannot load gltf without " + semantic);
}
Also used : GLTFBufferView(org.terasology.engine.rendering.gltf.model.GLTFBufferView) IOException(java.io.IOException) TIntList(gnu.trove.list.TIntList) GLTFAccessor(org.terasology.engine.rendering.gltf.model.GLTFAccessor) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Aggregations

GLTFAccessor (org.terasology.engine.rendering.gltf.model.GLTFAccessor)6 GLTFBufferView (org.terasology.engine.rendering.gltf.model.GLTFBufferView)6 TFloatList (gnu.trove.list.TFloatList)4 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)4 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 IOException (java.io.IOException)3 TIntList (gnu.trove.list.TIntList)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 GLTF (org.terasology.engine.rendering.gltf.model.GLTF)2 GLTFMesh (org.terasology.engine.rendering.gltf.model.GLTFMesh)2 GLTFPrimitive (org.terasology.engine.rendering.gltf.model.GLTFPrimitive)2 ArrayList (java.util.ArrayList)1 Matrix4f (org.joml.Matrix4f)1 Vector3f (org.joml.Vector3f)1 StandardMeshData (org.terasology.engine.rendering.assets.mesh.StandardMeshData)1 Bone (org.terasology.engine.rendering.assets.skeletalmesh.Bone)1 BoneWeight (org.terasology.engine.rendering.assets.skeletalmesh.BoneWeight)1 SkeletalMeshDataBuilder (org.terasology.engine.rendering.assets.skeletalmesh.SkeletalMeshDataBuilder)1 GLTFSkin (org.terasology.engine.rendering.gltf.model.GLTFSkin)1