Search in sources :

Example 51 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef 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 52 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef 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 53 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef 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 54 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class BoundingBoxRenderer method renderOverlay.

@Override
public void renderOverlay() {
    if (config.getRendering().getDebug().isRenderEntityBoundingBoxes()) {
        GL33.glDepthFunc(GL33.GL_ALWAYS);
        Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
        Vector3f worldPos = new Vector3f();
        Vector3f worldPositionCameraSpace = new Vector3f();
        worldPos.sub(cameraPosition, worldPositionCameraSpace);
        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);
        int index = 0;
        meshData.reallocate(0, 0);
        meshData.indices.rewind();
        meshData.position.rewind();
        meshData.color0.rewind();
        Vector3f worldPosition = new Vector3f();
        Quaternionf worldRot = new Quaternionf();
        Matrix4f transform = new Matrix4f();
        AABBf bounds = new AABBf(0, 0, 0, 0, 0, 0);
        for (EntityRef entity : entityManager.getEntitiesWith(LocationComponent.class)) {
            LocationComponent location = entity.getComponent(LocationComponent.class);
            location.getWorldPosition(worldPosition);
            location.getWorldRotation(worldRot);
            BoxShapeComponent boxShapeComponent = entity.getComponent(BoxShapeComponent.class);
            if (boxShapeComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(boxShapeComponent.extents).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
            CapsuleShapeComponent capsuleComponent = entity.getComponent(CapsuleShapeComponent.class);
            if (capsuleComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(capsuleComponent.radius, capsuleComponent.height / 2.0f, capsuleComponent.radius).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
            CylinderShapeComponent cylinderShapeComponent = entity.getComponent(CylinderShapeComponent.class);
            if (cylinderShapeComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(cylinderShapeComponent.radius, cylinderShapeComponent.height / 2.0f, cylinderShapeComponent.radius).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
            SphereShapeComponent sphereShapeComponent = entity.getComponent(SphereShapeComponent.class);
            if (sphereShapeComponent != null) {
                bounds.set(0, 0, 0, 0, 0, 0);
                bounds.expand(new Vector3f(sphereShapeComponent.radius).div(2.0f));
                transform.translationRotateScale(worldPosition, worldRot, location.getWorldScale());
                bounds.transform(transform);
                index = addRenderBound(meshData, bounds, index);
            }
        }
        material.enable();
        mesh.reload(meshData);
        mesh.render();
        GL33.glDepthFunc(GL33.GL_LEQUAL);
    }
}
Also used : Matrix4f(org.joml.Matrix4f) AABBf(org.terasology.joml.geom.AABBf) SphereShapeComponent(org.terasology.engine.physics.components.shapes.SphereShapeComponent) Vector3f(org.joml.Vector3f) BoxShapeComponent(org.terasology.engine.physics.components.shapes.BoxShapeComponent) Quaternionf(org.joml.Quaternionf) CylinderShapeComponent(org.terasology.engine.physics.components.shapes.CylinderShapeComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) CapsuleShapeComponent(org.terasology.engine.physics.components.shapes.CapsuleShapeComponent)

Example 55 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class LightFadeSystem method update.

@Override
public void update(float delta) {
    for (EntityRef fadingLight : entityManager.getEntitiesWith(LightFadeComponent.class, LightComponent.class)) {
        LightFadeComponent fade = fadingLight.getComponent(LightFadeComponent.class);
        LightComponent light = fadingLight.getComponent(LightComponent.class);
        // Fade
        float ambientChange = delta * fade.ambientFadeRate;
        float diffuseChange = delta * fade.diffuseFadeRate;
        boolean incomplete = false;
        if (Math.abs(fade.targetAmbientIntensity - light.lightAmbientIntensity) < ambientChange) {
            light.lightAmbientIntensity = fade.targetAmbientIntensity;
        } else if (light.lightAmbientIntensity < fade.targetAmbientIntensity) {
            light.lightAmbientIntensity += ambientChange;
            incomplete = true;
        } else {
            light.lightAmbientIntensity -= ambientChange;
            incomplete = true;
        }
        if (Math.abs(fade.targetDiffuseIntensity - light.lightDiffuseIntensity) < diffuseChange) {
            light.lightDiffuseIntensity = fade.targetDiffuseIntensity;
        } else if (light.lightDiffuseIntensity < fade.targetDiffuseIntensity) {
            light.lightDiffuseIntensity += diffuseChange;
            incomplete = true;
        } else {
            light.lightDiffuseIntensity -= diffuseChange;
            incomplete = true;
        }
        // If fade complete, remove fade and maybe light
        if (incomplete) {
            fadingLight.saveComponent(light);
        } else {
            if (fade.removeLightAfterFadeComplete) {
                fadingLight.removeComponent(LightComponent.class);
            } else {
                fadingLight.saveComponent(light);
            }
            fadingLight.removeComponent(LightFadeComponent.class);
        }
    }
}
Also used : EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Aggregations

EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)298 Test (org.junit.jupiter.api.Test)88 ClientComponent (org.terasology.engine.network.ClientComponent)55 Vector3f (org.joml.Vector3f)51 LocationComponent (org.terasology.engine.logic.location.LocationComponent)44 Vector3i (org.joml.Vector3i)36 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)34 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)29 StringComponent (org.terasology.unittest.stubs.StringComponent)26 NetworkComponent (org.terasology.engine.network.NetworkComponent)23 EntityData (org.terasology.protobuf.EntityData)23 Quaternionf (org.joml.Quaternionf)19 DisplayNameComponent (org.terasology.engine.logic.common.DisplayNameComponent)19 Component (org.terasology.gestalt.entitysystem.component.Component)19 CharacterComponent (org.terasology.engine.logic.characters.CharacterComponent)15 Map (java.util.Map)14 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)13 BlockComponent (org.terasology.engine.world.block.BlockComponent)13 Block (org.terasology.engine.world.block.Block)11 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)10