Search in sources :

Example 86 with Vector3f

use of org.joml.Vector3f in project Terasology by MovingBlocks.

the class SkeletalMeshDataBuilder method build.

public SkeletalMeshData build() {
    int rootBones = 0;
    for (Bone bone : bones) {
        if (bone.getParent() == null) {
            rootBones++;
        }
    }
    Vector3f minOfAABB = new Vector3f(vertices.get(0));
    Vector3f maxOfAABB = new Vector3f(vertices.get(0));
    for (Vector3f vert : vertices) {
        minOfAABB.min(vert);
        maxOfAABB.max(vert);
    }
    if (rootBones == 0) {
        throw new IllegalStateException("Cannot create a skeleton with no root bones");
    } else if (rootBones > 1) {
        throw new IllegalStateException("Cannot create a skeleton with multiple root bones");
    }
    AABBf staticAabb = new AABBf(minOfAABB, maxOfAABB);
    return new SkeletalMeshData(bones, vertices, normals, weights, uvs, indices, staticAabb);
}
Also used : AABBf(org.terasology.joml.geom.AABBf) Vector3f(org.joml.Vector3f)

Example 87 with Vector3f

use of org.joml.Vector3f in project Terasology by MovingBlocks.

the class SkeletalMeshData method getVertexNormals.

/**
 * Provides the normals of all vertices of the mesh, transformed based on the transformation matrices of all bones
 *
 * @param boneTransforms A transformation matrix for each bone in the skeletal mesh
 * @return The normals of each vertex
 */
public List<Vector3f> getVertexNormals(List<Matrix4f> boneTransforms) {
    List<Vector3f> results = Lists.newArrayListWithCapacity(getVertexCount());
    for (int i = 0; i < normals.size(); i++) {
        Vector3f norm = new Vector3f(normals.get(i));
        Matrix4f skinMat = new Matrix4f().m00(0).m11(0).m22(0).m33(0);
        BoneWeight weight = weights.get(i);
        for (int w = 0; w < weight.jointCount(); w++) {
            Matrix4f jointMat = new Matrix4f(boneTransforms.get(weight.getJoint(w)));
            jointMat.scale(weight.getBias(w));
            skinMat.add(jointMat);
        }
        norm.mulTransposePosition(skinMat);
        results.add(norm);
    }
    return results;
}
Also used : Matrix4f(org.joml.Matrix4f) Vector3f(org.joml.Vector3f)

Example 88 with Vector3f

use of org.joml.Vector3f in project Terasology by MovingBlocks.

the class SkeletalMeshData method getVertexPositions.

/**
 * Provides the positions of all vertices of the mesh, transformed based on the transformation matrices of all
 * bones
 *
 * @param boneTransforms A transformation matrix for each bone in the skeletal mesh
 * @return The positions of each vertex
 */
public List<Vector3f> getVertexPositions(List<Matrix4f> boneTransforms) {
    List<Vector3f> results = Lists.newArrayListWithCapacity(getVertexCount());
    for (int i = 0; i < vertices.size(); i++) {
        Vector3f pos = new Vector3f(vertices.get(i));
        Matrix4f skinMat = new Matrix4f().m00(0).m11(0).m22(0).m33(0);
        BoneWeight weight = weights.get(i);
        for (int w = 0; w < weight.jointCount(); w++) {
            Matrix4f jointMat = new Matrix4f(boneTransforms.get(weight.getJoint(w)));
            jointMat.scale(weight.getBias(w));
            skinMat.add(jointMat);
        }
        pos.mulTransposePosition(skinMat);
        results.add(pos);
    }
    return results;
}
Also used : Matrix4f(org.joml.Matrix4f) Vector3f(org.joml.Vector3f)

Example 89 with Vector3f

use of org.joml.Vector3f in project Terasology by MovingBlocks.

the class SphereBuilder method build.

// refrence: https://github.com/caosdoar/spheres/blob/master/src/spheres.cpp
public StandardMeshData build() {
    StandardMeshData meshData = new StandardMeshData();
    Matrix4f mat = new Matrix4f().setRotationXYZ(0, 0, (float) (Math.PI / 2.0f));
    Vector4f pos = new Vector4f();
    Vector3f normal = new Vector3f();
    Vector2f uv0 = new Vector2f();
    Vector3f loc = new Vector3f();
    float s = 0.0f;
    float t = 1.0f;
    float ds = 1.0f / verticalCuts;
    float dt = 1.0f / horizontalCuts;
    for (int j = 0; j <= horizontalCuts; ++j) {
        double polar = (Math.PI * j) / horizontalCuts;
        double sp = Math.sin(polar);
        double cp = Math.cos(polar);
        s = 0.0f;
        for (int i = 0; i <= verticalCuts; ++i) {
            double azimuth = (2.0 * Math.PI * i) / verticalCuts;
            double sa = Math.sin(azimuth);
            double ca = Math.cos(azimuth);
            if (this.normals) {
                normal.set((float) (sp * ca), (float) cp, (float) (sp * sa));
                meshData.normal.put(normal);
            }
            if (this.textured) {
                uv0.set(s, t);
                meshData.uv0.put(uv0);
            }
            s += ds;
            pos.set((float) ((sp * ca) * radius), (float) (cp * radius), (float) ((sp * sa) * radius), 1.0f);
            mat.transform(pos);
            loc.set(pos.x, pos.y, pos.z);
            meshData.position.put(loc);
        }
        t -= dt;
    }
    for (int j = 0; j < horizontalCuts; ++j) {
        int aStart = (j * (verticalCuts + 1));
        int bStart = (j + 1) * (verticalCuts + 1);
        for (int i = 0; i < verticalCuts; ++i) {
            int a = aStart + i;
            int a1 = aStart + ((i + 1) % (verticalCuts + 1));
            int b = bStart + i;
            int b1 = bStart + ((i + 1) % (verticalCuts + 1));
            meshData.indices.putAll(a, b1, b);
            meshData.indices.putAll(a1, b1, a);
        }
    }
    return meshData;
}
Also used : Matrix4f(org.joml.Matrix4f) Vector4f(org.joml.Vector4f) Vector2f(org.joml.Vector2f) Vector3f(org.joml.Vector3f)

Example 90 with Vector3f

use of org.joml.Vector3f in project Terasology by MovingBlocks.

the class CoreCommands method bowlingPrep.

@Command(shortDescription = "Sets up a typical bowling pin arrangement in front of the player. ", helpText = "Spawns the specific block in a regular bowling pin pattern, Throw something at it!", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String bowlingPrep(@Sender EntityRef sender, @CommandParam("blockName") String blockName) {
    ClientComponent clientComponent = sender.getComponent(ClientComponent.class);
    LocationComponent characterLocation = clientComponent.character.getComponent(LocationComponent.class);
    Vector3f spawnPos = characterLocation.getWorldPosition(new Vector3f());
    Vector3f offset = characterLocation.getWorldDirection(new Vector3f());
    offset.mul(5);
    spawnPos.add(offset);
    BlockFamily block = blockManager.getBlockFamily(blockName);
    if (block == null) {
        return "Sorry, your block is not found";
    }
    BlockItemFactory blockItemFactory = new BlockItemFactory(entityManager);
    Vector3f startPos = new Vector3f(spawnPos);
    // delta x is the distance between the pins in the rows.
    float deltax = 0.5f;
    // delta z is the distance between the rows.
    float deltaz = 1.0f;
    // the height of the drop (to be modified to keep the bowlingPin upright)
    float vectorY = 0.0f;
    // rownumber loop is for selecting row
    for (int rownumber = 0; rownumber < 4; rownumber++) {
        // Spawn starting position for Rownumber
        startPos.add(deltax * (4 - rownumber), vectorY, deltaz);
        // pinPosx loop is for vectorx position of bowling pin  in  a particular row
        for (int pinPosx = 0; pinPosx <= rownumber; pinPosx++) {
            EntityRef blockItem = blockItemFactory.newInstance(block);
            blockItem.send(new DropItemEvent(startPos));
            if (pinPosx < rownumber) {
                // drift of position in vector x coordinate, for the last pin stop drifting
                startPos.add(2 * deltax, 0, 0);
            }
        }
        // returns to start position
        startPos.add(-deltax * (rownumber + 4), 0, 0);
    }
    return "prepared 10 " + blockName + " in a bowling pin pattern :)";
}
Also used : DropItemEvent(org.terasology.engine.logic.inventory.events.DropItemEvent) Vector3f(org.joml.Vector3f) BlockItemFactory(org.terasology.engine.world.block.items.BlockItemFactory) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) ClientComponent(org.terasology.engine.network.ClientComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command) ConsoleCommand(org.terasology.engine.logic.console.commandSystem.ConsoleCommand)

Aggregations

Vector3f (org.joml.Vector3f)261 LocationComponent (org.terasology.engine.logic.location.LocationComponent)50 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)49 Matrix4f (org.joml.Matrix4f)34 Quaternionf (org.joml.Quaternionf)29 Test (org.junit.jupiter.api.Test)20 Vector3i (org.joml.Vector3i)19 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)18 FloatBuffer (java.nio.FloatBuffer)17 ClientComponent (org.terasology.engine.network.ClientComponent)17 ByteBuffer (java.nio.ByteBuffer)16 Vector3fc (org.joml.Vector3fc)15 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)15 Vector2f (org.joml.Vector2f)13 Vector4f (org.joml.Vector4f)13 ArrayList (java.util.ArrayList)10 HitResult (org.terasology.engine.physics.HitResult)10 IOException (java.io.IOException)8 Test (org.junit.Test)8 VertexResourceBuilder (org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder)8