Search in sources :

Example 16 with TFloatList

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

the class GLTFAnimationFormat method loadAnimation.

private MeshAnimationData loadAnimation(GLTF gltf, GLTFAnimation animation, List<byte[]> loadedBuffers, TIntIntMap boneIndexMapping, List<String> boneNames, TIntList boneParents, List<Bone> bones) throws IOException {
    List<ChannelReader> channelReaders = new ArrayList<>();
    for (GLTFChannel channel : animation.getChannels()) {
        GLTFAnimationSampler sampler = animation.getSamplers().get(channel.getSampler());
        TFloatList times = getFloats(gltf, loadedBuffers, sampler.getInput());
        int bone = boneIndexMapping.get(channel.getTarget().getNode());
        switch(channel.getTarget().getPath()) {
            case TRANSLATION:
                {
                    List<Vector3f> data = getVector3fs(gltf, loadedBuffers, sampler.getOutput());
                    channelReaders.add(new BufferChannelReader<>(times, data, sampler.getInterpolation()::interpolate, x -> x.getPosition(bone)));
                    break;
                }
            case ROTATION:
                {
                    List<Quaternionf> data = getQuat4fs(gltf, loadedBuffers, sampler.getOutput());
                    channelReaders.add(new BufferChannelReader<>(times, data, sampler.getInterpolation()::interpolate, x -> x.getRotation(bone)));
                    break;
                }
            case SCALE:
                {
                    List<Vector3f> data = getVector3fs(gltf, loadedBuffers, sampler.getOutput());
                    channelReaders.add(new BufferChannelReader<>(times, data, sampler.getInterpolation()::interpolate, x -> x.getBoneScale(bone)));
                    break;
                }
            default:
                break;
        }
    }
    int frameCount = (int) (channelReaders.stream().map(ChannelReader::endTime).reduce(Float::max).orElse(0f) / TIME_PER_FRAME) + 1;
    List<MeshAnimationFrame> frames = new ArrayList<>(frameCount);
    for (int i = 0; i < frameCount; i++) {
        float time = i * TIME_PER_FRAME;
        List<Vector3f> boneLocations = new ArrayList<>();
        List<Quaternionf> boneRotations = new ArrayList<>();
        List<Vector3f> boneScales = new ArrayList<>();
        for (Bone bone : bones) {
            boneLocations.add(new Vector3f(bone.getLocalPosition()));
            boneRotations.add(new Quaternionf(bone.getLocalRotation()));
            boneScales.add(new Vector3f(bone.getLocalScale()));
        }
        MeshAnimationFrame frame = new MeshAnimationFrame(boneLocations, boneRotations, boneScales);
        channelReaders.forEach(x -> x.updateFrame(time, frame));
        frames.add(frame);
    }
    return new MeshAnimationData(boneNames, boneParents, frames, TIME_PER_FRAME, new AABBf(0, 0, 0));
}
Also used : GLTFAnimationSampler(org.terasology.engine.rendering.gltf.model.GLTFAnimationSampler) MeshAnimationFrame(org.terasology.engine.rendering.assets.animation.MeshAnimationFrame) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TFloatArrayList(gnu.trove.list.array.TFloatArrayList) TFloatList(gnu.trove.list.TFloatList) GLTFChannel(org.terasology.engine.rendering.gltf.model.GLTFChannel) Quaternionf(org.joml.Quaternionf) AABBf(org.terasology.joml.geom.AABBf) Vector3f(org.joml.Vector3f) MeshAnimationData(org.terasology.engine.rendering.assets.animation.MeshAnimationData) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TFloatList(gnu.trove.list.TFloatList) TIntList(gnu.trove.list.TIntList) TFloatArrayList(gnu.trove.list.array.TFloatArrayList) List(java.util.List) Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone)

Example 17 with TFloatList

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

the class GLTFAnimationFormat method getQuat4fs.

private List<Quaternionf> getQuat4fs(GLTF gltf, List<byte[]> loadedBuffers, int accessorIndex) throws IOException {
    TFloatList floats = getFloats(gltf, loadedBuffers, accessorIndex);
    List<Quaternionf> quats = Lists.newArrayListWithCapacity(floats.size() / 4);
    for (int i = 0; i < floats.size(); i += 4) {
        quats.add(new Quaternionf(floats.get(i), floats.get(i + 1), floats.get(i + 2), floats.get(i + 3)));
    }
    return quats;
}
Also used : TFloatList(gnu.trove.list.TFloatList) Quaternionf(org.joml.Quaternionf)

Example 18 with TFloatList

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

the class GLTFCommonFormat method loadVector4fList.

protected List<Vector4f> loadVector4fList(MeshAttributeSemantic semantic, GLTFPrimitive gltfPrimitive, GLTF gltf, List<byte[]> loadedBuffers) throws IOException {
    TFloatList floats = readFloatBuffer(semantic, gltfPrimitive, gltf, loadedBuffers);
    List<Vector4f> vectors = Lists.newArrayListWithCapacity(floats.size() / 4);
    for (int i = 0; i < floats.size(); i += 4) {
        vectors.add(new Vector4f(floats.get(i), floats.get(i + 1), floats.get(i + 2), floats.get(i + 3)));
    }
    return vectors;
}
Also used : Vector4f(org.joml.Vector4f) TFloatList(gnu.trove.list.TFloatList)

Example 19 with TFloatList

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

the class GLTFCommonFormat method loadVector2fList.

protected List<Vector2f> loadVector2fList(MeshAttributeSemantic semantic, GLTFPrimitive gltfPrimitive, GLTF gltf, List<byte[]> loadedBuffers) throws IOException {
    TFloatList floats = readFloatBuffer(semantic, gltfPrimitive, gltf, loadedBuffers);
    if (floats == null) {
        return Collections.emptyList();
    }
    List<Vector2f> vectors = Lists.newArrayListWithCapacity(floats.size() / 2);
    for (int i = 0; i < floats.size(); i += 2) {
        vectors.add(new Vector2f(floats.get(i), floats.get(i + 1)));
    }
    return vectors;
}
Also used : Vector2f(org.joml.Vector2f) TFloatList(gnu.trove.list.TFloatList)

Example 20 with TFloatList

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

the class GLTFCommonFormat method loadVector3fList.

protected List<Vector3f> loadVector3fList(MeshAttributeSemantic semantic, GLTFPrimitive gltfPrimitive, GLTF gltf, List<byte[]> loadedBuffers) throws IOException {
    TFloatList floats = readFloatBuffer(semantic, gltfPrimitive, gltf, loadedBuffers);
    List<Vector3f> vectors = Lists.newArrayListWithCapacity(floats.size() / 3);
    for (int i = 0; i < floats.size(); i += 3) {
        vectors.add(new Vector3f(floats.get(i), floats.get(i + 1), floats.get(i + 2)));
    }
    return vectors;
}
Also used : Vector3f(org.joml.Vector3f) TFloatList(gnu.trove.list.TFloatList)

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