Search in sources :

Example 1 with MeshAnimationFrame

use of org.terasology.rendering.assets.animation.MeshAnimationFrame in project Terasology by MovingBlocks.

the class SkeletonRenderer method updateSkeletalMeshOfEntity.

private void updateSkeletalMeshOfEntity(EntityRef entity, float delta) {
    SkeletalMeshComponent skeletalMeshComp = entity.getComponent(SkeletalMeshComponent.class);
    if (skeletalMeshComp.animation == null && skeletalMeshComp.animationPool != null) {
        skeletalMeshComp.animation = randomAnimationData(skeletalMeshComp, random);
    }
    if (skeletalMeshComp.animation == null) {
        return;
    }
    if (skeletalMeshComp.animation.getFrameCount() < 1) {
        return;
    }
    skeletalMeshComp.animationTime += delta * skeletalMeshComp.animationRate;
    float animationDuration = getDurationOfAnimation(skeletalMeshComp);
    while (skeletalMeshComp.animationTime >= animationDuration) {
        MeshAnimation newAnimation;
        if (!skeletalMeshComp.loop) {
            newAnimation = null;
        } else {
            newAnimation = randomAnimationData(skeletalMeshComp, random);
        }
        if (newAnimation == null) {
            MeshAnimation finishedAnimation = skeletalMeshComp.animation;
            skeletalMeshComp.animationTime = animationDuration;
            MeshAnimationFrame frame = skeletalMeshComp.animation.getFrame(skeletalMeshComp.animation.getFrameCount() - 1);
            updateSkeleton(skeletalMeshComp, frame, frame, 1.0f);
            // Set animation to null so that AnimEndEvent fires only once
            skeletalMeshComp.animation = null;
            entity.saveComponent(skeletalMeshComp);
            entity.send(new AnimEndEvent(finishedAnimation));
            return;
        }
        skeletalMeshComp.animationTime -= animationDuration;
        if (skeletalMeshComp.animationTime < 0) {
            // In case the float calculation wasn't exact:
            skeletalMeshComp.animationTime = 0;
        }
        skeletalMeshComp.animation = newAnimation;
        animationDuration = getDurationOfAnimation(skeletalMeshComp);
    }
    float framePos = skeletalMeshComp.animationTime / skeletalMeshComp.animation.getTimePerFrame();
    int frameAId = (int) framePos;
    int frameBId = frameAId + 1;
    if (frameBId >= skeletalMeshComp.animation.getFrameCount()) {
        // In case the float calcuation wasn't exact:
        frameBId = skeletalMeshComp.animation.getFrameCount() - 1;
    }
    MeshAnimationFrame frameA = skeletalMeshComp.animation.getFrame(frameAId);
    MeshAnimationFrame frameB = skeletalMeshComp.animation.getFrame(frameBId);
    updateSkeleton(skeletalMeshComp, frameA, frameB, framePos - frameAId);
    entity.saveComponent(skeletalMeshComp);
}
Also used : MeshAnimationFrame(org.terasology.rendering.assets.animation.MeshAnimationFrame) MeshAnimation(org.terasology.rendering.assets.animation.MeshAnimation)

Example 2 with MeshAnimationFrame

use of org.terasology.rendering.assets.animation.MeshAnimationFrame in project Terasology by MovingBlocks.

the class MD5AnimationLoader method createAnimation.

private MeshAnimationData createAnimation(MD5 md5) {
    List<String> boneNames = Lists.newArrayListWithCapacity(md5.numJoints);
    TIntList boneParents = new TIntArrayList(md5.numJoints);
    for (int i = 0; i < md5.numJoints; ++i) {
        boneNames.add(md5.joints[i].name);
        boneParents.add(md5.joints[i].parent);
    }
    float timePerFrame = 1.0f / md5.frameRate;
    List<MeshAnimationFrame> frames = Lists.newArrayList();
    for (int frameIndex = 0; frameIndex < md5.numFrames; ++frameIndex) {
        MD5Frame frame = md5.frames[frameIndex];
        List<Vector3f> positions = Lists.newArrayListWithExpectedSize(md5.numJoints);
        List<Vector3f> rawRotations = Lists.newArrayListWithExpectedSize(md5.numJoints);
        for (int i = 0; i < md5.numJoints; ++i) {
            positions.add(new Vector3f(md5.baseFramePosition[i]));
            rawRotations.add(new Vector3f(md5.baseFrameOrientation[i]));
        }
        for (int jointIndex = 0; jointIndex < md5.numJoints; ++jointIndex) {
            int compIndex = 0;
            if ((md5.joints[jointIndex].flags & POSITION_X_FLAG) != 0) {
                positions.get(jointIndex).x = frame.components[md5.joints[jointIndex].startIndex + compIndex];
                compIndex++;
            }
            if ((md5.joints[jointIndex].flags & POSITION_Y_FLAG) != 0) {
                positions.get(jointIndex).y = frame.components[md5.joints[jointIndex].startIndex + compIndex];
                compIndex++;
            }
            if ((md5.joints[jointIndex].flags & POSITION_Z_FLAG) != 0) {
                positions.get(jointIndex).z = frame.components[md5.joints[jointIndex].startIndex + compIndex];
                compIndex++;
            }
            if ((md5.joints[jointIndex].flags & ORIENTATION_X_FLAG) != 0) {
                rawRotations.get(jointIndex).x = frame.components[md5.joints[jointIndex].startIndex + compIndex];
                compIndex++;
            }
            if ((md5.joints[jointIndex].flags & ORIENTATION_Y_FLAG) != 0) {
                rawRotations.get(jointIndex).y = frame.components[md5.joints[jointIndex].startIndex + compIndex];
                compIndex++;
            }
            if ((md5.joints[jointIndex].flags & ORIENTATION_Z_FLAG) != 0) {
                rawRotations.get(jointIndex).z = frame.components[md5.joints[jointIndex].startIndex + compIndex];
            }
        }
        List<Quat4f> rotations = rawRotations.stream().map(rot -> MD5ParserCommon.completeQuat4f(rot.x, rot.y, rot.z)).collect(Collectors.toCollection(ArrayList::new));
        // Rotate just the root bone to correct for coordinate system differences
        rotations.set(0, MD5ParserCommon.correctQuat4f(rotations.get(0)));
        positions.set(0, MD5ParserCommon.correctOffset(positions.get(0)));
        frames.add(new MeshAnimationFrame(positions, rotations));
    }
    AABB aabb = AABB.createEncompassing(Arrays.asList(md5.bounds));
    return new MeshAnimationData(boneNames, boneParents, frames, timePerFrame, aabb);
}
Also used : Charsets(com.google.common.base.Charsets) TIntList(gnu.trove.list.TIntList) MeshAnimationFrame(org.terasology.rendering.assets.animation.MeshAnimationFrame) Arrays(java.util.Arrays) TIntArrayList(gnu.trove.list.array.TIntArrayList) Vector3f(org.terasology.math.geom.Vector3f) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) MeshAnimationData(org.terasology.rendering.assets.animation.MeshAnimationData) List(java.util.List) Lists(com.google.common.collect.Lists) Matcher(java.util.regex.Matcher) Quat4f(org.terasology.math.geom.Quat4f) AbstractAssetFileFormat(org.terasology.assets.format.AbstractAssetFileFormat) AssetDataFile(org.terasology.assets.format.AssetDataFile) AABB(org.terasology.math.AABB) RegisterAssetFileFormat(org.terasology.assets.module.annotations.RegisterAssetFileFormat) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) ResourceUrn(org.terasology.assets.ResourceUrn) InputStream(java.io.InputStream) MeshAnimationFrame(org.terasology.rendering.assets.animation.MeshAnimationFrame) TIntArrayList(gnu.trove.list.array.TIntArrayList) Vector3f(org.terasology.math.geom.Vector3f) MeshAnimationData(org.terasology.rendering.assets.animation.MeshAnimationData) TIntList(gnu.trove.list.TIntList) Quat4f(org.terasology.math.geom.Quat4f) AABB(org.terasology.math.AABB)

Aggregations

MeshAnimationFrame (org.terasology.rendering.assets.animation.MeshAnimationFrame)2 Charsets (com.google.common.base.Charsets)1 Lists (com.google.common.collect.Lists)1 TIntList (gnu.trove.list.TIntList)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 ResourceUrn (org.terasology.assets.ResourceUrn)1 AbstractAssetFileFormat (org.terasology.assets.format.AbstractAssetFileFormat)1 AssetDataFile (org.terasology.assets.format.AssetDataFile)1 RegisterAssetFileFormat (org.terasology.assets.module.annotations.RegisterAssetFileFormat)1 AABB (org.terasology.math.AABB)1