Search in sources :

Example 1 with Bone

use of org.terasology.engine.rendering.assets.skeletalmesh.Bone in project Terasology by MovingBlocks.

the class SkeletonRenderer method renderOpaque.

@Override
public void renderOpaque() {
    Vector3fc cameraPosition = worldRenderer.getActiveCamera().getPosition();
    Quaternionf worldRot = new Quaternionf();
    Vector3f worldPos = new Vector3f();
    Quaternionf inverseWorldRot = new Quaternionf();
    FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
    FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
    for (EntityRef entity : entityManager.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) {
        SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
        if (skeletalMesh.mesh == null || skeletalMesh.material == null || skeletalMesh.boneEntities == null || !skeletalMesh.material.isRenderable()) {
            continue;
        }
        AABBf aabb;
        MeshAnimation animation = skeletalMesh.animation;
        if (animation != null) {
            aabb = animation.getAabb();
        } else {
            aabb = skeletalMesh.mesh.getStaticAabb();
        }
        LocationComponent location = entity.getComponent(LocationComponent.class);
        location.getWorldRotation(worldRot);
        worldRot.invert(inverseWorldRot);
        location.getWorldPosition(worldPos);
        float worldScale = location.getWorldScale();
        aabb = aabb.transform(new Matrix4f().translationRotateScale(worldPos, worldRot, worldScale), new AABBf());
        // Scale bounding box for skeletalMesh.
        Vector3f scale = skeletalMesh.scale;
        Vector3f aabbCenter = aabb.center(new Vector3f());
        Vector3f scaledExtents = aabb.extent(new Vector3f()).mul(scale.x(), scale.y(), scale.z());
        aabb = new AABBf(aabbCenter, aabbCenter).expand(scaledExtents);
        if (!worldRenderer.getActiveCamera().hasInSight(aabb)) {
            continue;
        }
        skeletalMesh.material.enable();
        skeletalMesh.material.setFloat("sunlight", 1.0f, true);
        skeletalMesh.material.setFloat("blockLight", 1.0f, true);
        skeletalMesh.material.setFloat3("colorOffset", skeletalMesh.color.rf(), skeletalMesh.color.gf(), skeletalMesh.color.bf(), true);
        skeletalMesh.material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
        skeletalMesh.material.bindTextures();
        Vector3f worldPositionCameraSpace = new Vector3f();
        worldPos.sub(cameraPosition, worldPositionCameraSpace);
        worldPositionCameraSpace.y += skeletalMesh.heightOffset;
        Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, worldRot, worldScale);
        Matrix4f modelViewMatrix = worldRenderer.getActiveCamera().getViewMatrix().mul(matrixCameraSpace, new Matrix4f());
        modelViewMatrix.get(tempMatrixBuffer44);
        skeletalMesh.material.setMatrix4("modelViewMatrix", tempMatrixBuffer44, true);
        modelViewMatrix.normal(new Matrix3f()).get(tempMatrixBuffer33);
        skeletalMesh.material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
        skeletalMesh.material.setFloat("sunlight", worldRenderer.getMainLightIntensityAt(worldPos), true);
        skeletalMesh.material.setFloat("blockLight", worldRenderer.getBlockLightIntensityAt(worldPos), true);
        Matrix4f[] boneTransforms = new Matrix4f[skeletalMesh.mesh.getBones().size()];
        for (Bone bone : skeletalMesh.mesh.getBones()) {
            EntityRef boneEntity = skeletalMesh.boneEntities.get(bone.getName());
            if (boneEntity == null) {
                boneEntity = EntityRef.NULL;
            }
            LocationComponent boneLocation = boneEntity.getComponent(LocationComponent.class);
            if (boneLocation != null) {
                Matrix4f boneTransform = new Matrix4f();
                boneLocation.getRelativeTransform(boneTransform, entity);
                boneTransform.mul(bone.getInverseBindMatrix());
                boneTransforms[bone.getIndex()] = boneTransform.transpose();
            } else {
                logger.warn("Unable to resolve bone \"{}\"", bone.getName());
                boneTransforms[bone.getIndex()] = new Matrix4f();
            }
        }
        ((OpenGLSkeletalMesh) skeletalMesh.mesh).setScaleTranslate(skeletalMesh.scale, skeletalMesh.translate);
        ((OpenGLSkeletalMesh) skeletalMesh.mesh).render(Arrays.asList(boneTransforms));
    }
}
Also used : Quaternionf(org.joml.Quaternionf) FloatBuffer(java.nio.FloatBuffer) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Vector3fc(org.joml.Vector3fc) Matrix4f(org.joml.Matrix4f) AABBf(org.terasology.joml.geom.AABBf) Matrix3f(org.joml.Matrix3f) OpenGLSkeletalMesh(org.terasology.engine.rendering.opengl.OpenGLSkeletalMesh) Vector3f(org.joml.Vector3f) MeshAnimation(org.terasology.engine.rendering.assets.animation.MeshAnimation) Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 2 with Bone

use of org.terasology.engine.rendering.assets.skeletalmesh.Bone in project Terasology by MovingBlocks.

the class SkeletonRenderer method renderOverlay.

@Override
public void renderOverlay() {
    if (config.getRendering().getDebug().isRenderSkeletons()) {
        meshData.reallocate(0, 0);
        meshData.indices.rewind();
        meshData.position.rewind();
        meshData.color0.rewind();
        Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
        Matrix4f relMat = new Matrix4f();
        Matrix4f relFinal = new Matrix4f();
        Matrix4f entityTransform = new Matrix4f();
        Matrix4f result = new Matrix4f();
        Vector3f currentPos = new Vector3f();
        int index = 0;
        for (EntityRef entity : entityManager.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) {
            SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
            LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
            if (skeletalMesh.boneEntities == null) {
                continue;
            }
            Vector3f location = locationComponent.getWorldPosition(new Vector3f());
            Quaternionf rotation = locationComponent.getWorldRotation(new Quaternionf());
            // transformation of the entity
            entityTransform.translationRotateScale(location, rotation, 1.0f);
            // position is referenced around (0,0,0) (worldposition - cameraposition)
            Vector3f worldPositionCameraSpace = cameraPosition.negate(new Vector3f());
            // same heightOffset is applied to worldPositionCameraSpace from #renderOpaque()
            // TODO: resolve repeated logic for transformation applied to bones
            worldPositionCameraSpace.y += skeletalMesh.heightOffset;
            Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, new Quaternionf(), 1.0f);
            Matrix4f modelViewMatrix = new Matrix4f(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
            material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
            material.setMatrix4("modelViewMatrix", modelViewMatrix, true);
            for (Bone bone : skeletalMesh.mesh.getBones()) {
                Bone parentBone = bone.getParent();
                EntityRef boneEntity = skeletalMesh.boneEntities.get(bone.getName());
                if (parentBone == null) {
                    continue;
                }
                // TODO: the position of the bone is de-coupled from the actual translation/scale
                EntityRef boneParentEntity = skeletalMesh.boneEntities.get(parentBone.getName());
                LocationComponent locCompA = boneEntity.getComponent(LocationComponent.class);
                LocationComponent locCompB = boneParentEntity.getComponent(LocationComponent.class);
                // need to calculate the relative transformation from the entity to the start of the bone
                locCompA.getRelativeTransform(relMat.identity(), entity);
                // entityTransform * (scale, translation) * relativeMat * [x,y,z,1]
                result.set(entityTransform).mul(relFinal.identity().scale(skeletalMesh.scale).translate(skeletalMesh.translate).mul(relMat)).transformPosition(// get the position of the start of the bone
                currentPos.zero());
                // the start of the bone
                meshData.position.put(currentPos);
                // need to calculate the relative transformation from the entity to the connecting bone
                locCompB.getRelativeTransform(relMat.identity(), entity);
                // entityTransform * (scale, translation) * relativeMat * [x,y,z,1]
                result.set(entityTransform).mul(relFinal.identity().scale(skeletalMesh.scale).translate(skeletalMesh.translate).mul(relMat)).transformPosition(// get the position to the connecting bone
                currentPos.zero());
                // the end of the bone
                meshData.position.put(currentPos);
                meshData.color0.put(Color.white);
                meshData.color0.put(Color.white);
                meshData.indices.putAll(new int[] { index, index + 1 });
                index += 2;
            }
        }
        GL33.glDepthFunc(GL33.GL_ALWAYS);
        material.enable();
        mesh.reload(meshData);
        mesh.render();
        GL33.glDepthFunc(GL33.GL_LEQUAL);
    }
}
Also used : Matrix4f(org.joml.Matrix4f) Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 3 with Bone

use of org.terasology.engine.rendering.assets.skeletalmesh.Bone in project Terasology by MovingBlocks.

the class SkeletonRenderer method updateSkeleton.

private void updateSkeleton(SkeletalMeshComponent skeletalMeshComp, MeshAnimationFrame frameA, MeshAnimationFrame frameB, float interpolationVal) {
    for (int i = 0; i < skeletalMeshComp.animation.getBoneCount(); ++i) {
        String boneName = skeletalMeshComp.animation.getBoneName(i);
        Bone bone = skeletalMeshComp.mesh.getBone(boneName);
        EntityRef boneEntity = skeletalMeshComp.boneEntities.get(boneName);
        if (boneEntity == null || bone == null) {
            continue;
        }
        LocationComponent boneLoc = boneEntity.getComponent(LocationComponent.class);
        if (boneLoc != null) {
            Vector3f newPos = frameA.getPosition(i).lerp(frameB.getPosition(i), interpolationVal, new Vector3f());
            boneLoc.setLocalPosition(newPos);
            Quaternionf newRot = frameA.getRotation(i).slerp(frameB.getRotation(i), interpolationVal, new Quaternionf());
            newRot.normalize();
            boneLoc.setLocalRotation(newRot);
            boneLoc.setLocalScale(frameA.getBoneScale(i).lerp(frameB.getBoneScale(i), interpolationVal, new Vector3f()).x);
            boneEntity.saveComponent(boneLoc);
        }
    }
}
Also used : Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 4 with Bone

use of org.terasology.engine.rendering.assets.skeletalmesh.Bone in project Terasology by MovingBlocks.

the class MeshAnimationImpl method isValidAnimationFor.

@Override
public boolean isValidAnimationFor(SkeletalMesh mesh) {
    for (int i = 0; i < data.getBoneNames().size(); ++i) {
        Bone bone = mesh.getBone(data.getBoneNames().get(i));
        boolean hasParent = data.getBoneParent().get(i) != MeshAnimationData.NO_PARENT;
        if (hasParent && (bone.getParent() == null || !bone.getParent().getName().equals(data.getBoneNames().get(data.getBoneParent().get(i))))) {
            return false;
        } else if (!hasParent && bone.getParent() != null) {
            return false;
        }
    }
    return true;
}
Also used : Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone)

Example 5 with Bone

use of org.terasology.engine.rendering.assets.skeletalmesh.Bone 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)

Aggregations

Bone (org.terasology.engine.rendering.assets.skeletalmesh.Bone)9 Vector3f (org.joml.Vector3f)6 Quaternionf (org.joml.Quaternionf)5 TIntArrayList (gnu.trove.list.array.TIntArrayList)4 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)4 LocationComponent (org.terasology.engine.logic.location.LocationComponent)4 TIntList (gnu.trove.list.TIntList)3 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)3 ArrayList (java.util.ArrayList)3 Matrix4f (org.joml.Matrix4f)3 TFloatList (gnu.trove.list.TFloatList)2 TIntIntMap (gnu.trove.map.TIntIntMap)2 TIntIntHashMap (gnu.trove.map.hash.TIntIntHashMap)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 MeshAnimationData (org.terasology.engine.rendering.assets.animation.MeshAnimationData)2 GLTF (org.terasology.engine.rendering.gltf.model.GLTF)2 GLTFSkin (org.terasology.engine.rendering.gltf.model.GLTFSkin)2 AABBf (org.terasology.joml.geom.AABBf)2