Search in sources :

Example 21 with TFloatList

use of gnu.trove.list.TFloatList 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)

Example 22 with TFloatList

use of gnu.trove.list.TFloatList in project Terasology by MovingBlocks.

the class Vector3fDeserializer method deserialize.

@Override
public Vector3f deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    TFloatList result = new TFloatArrayList();
    json.getAsJsonArray().forEach(x -> result.add(x.getAsFloat()));
    if (result.size() != 3) {
        throw new JsonParseException("Incorrect number of values for ImmutableVector3f - expected 3");
    }
    return new Vector3f(result.get(0), result.get(1), result.get(2));
}
Also used : Vector3f(org.joml.Vector3f) TFloatList(gnu.trove.list.TFloatList) JsonParseException(com.google.gson.JsonParseException) TFloatArrayList(gnu.trove.list.array.TFloatArrayList)

Example 23 with TFloatList

use of gnu.trove.list.TFloatList in project Terasology by MovingBlocks.

the class TroveUtils method intToFloat.

public static TFloatList intToFloat(TIntList list) {
    TFloatList result = new TFloatArrayList(list.size());
    TIntIterator iterator = list.iterator();
    while (iterator.hasNext()) {
        int i = iterator.next();
        result.add(i);
    }
    return result;
}
Also used : TIntIterator(gnu.trove.iterator.TIntIterator) TFloatList(gnu.trove.list.TFloatList) TFloatArrayList(gnu.trove.list.array.TFloatArrayList)

Aggregations

TFloatList (gnu.trove.list.TFloatList)23 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)12 TIntList (gnu.trove.list.TIntList)6 Vector3f (org.joml.Vector3f)5 GLTFAccessor (org.terasology.engine.rendering.gltf.model.GLTFAccessor)4 GLTFBufferView (org.terasology.engine.rendering.gltf.model.GLTFBufferView)4 JsonParseException (com.google.gson.JsonParseException)3 IOException (java.io.IOException)3 Quaternionf (org.joml.Quaternionf)3 PersistedDataArray (org.terasology.persistence.typeHandling.PersistedDataArray)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 ArrayList (java.util.ArrayList)2 Matrix4f (org.joml.Matrix4f)2 Bone (org.terasology.engine.rendering.assets.skeletalmesh.Bone)2 AABBf (org.terasology.joml.geom.AABBf)2 Vector2f (org.terasology.math.geom.Vector2f)2 Vector3f (org.terasology.math.geom.Vector3f)2 PersistedDataMap (org.terasology.persistence.typeHandling.PersistedDataMap)2 TDoubleIterator (gnu.trove.iterator.TDoubleIterator)1 TIntIterator (gnu.trove.iterator.TIntIterator)1