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);
}
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;
}
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;
}
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;
}
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 :)";
}
Aggregations