Hier ist mein bisheriger Code:
Code: Alles auswählen
package terrain;
import models.CubeModel;
import org.joml.Vector3f;
import org.joml.Vector3i;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ChunkMesh {
private List<Vertex> vertices;
private List<Float> positionList;
private List<Float> uvList;
private List<Float> normalList;
public float[] positions;
public float[] uvs;
public float[] normals;
public Chunk chunk;
public ChunkMesh(Chunk chunk) {
this.chunk = chunk;
vertices = new ArrayList<>();
positionList = new ArrayList<>();
uvList = new ArrayList<>();
normalList = new ArrayList<>();
buildMesh();
populateList();
}
public void update(Chunk chunk) {
this.chunk = chunk;
buildMesh();
populateList();
}
private void buildMesh() {
HashMap<Vector3i, Voxel> voxelMap = new HashMap<>();
for (Voxel voxel : chunk.blocks) {
voxelMap.put(voxel.position, voxel);
}
Vector3i tempPos = new Vector3i();
for (Voxel voxelI : chunk.blocks) {
Vector3i position = voxelI.position;
int x = position.x;
int y = position.y;
int z = position.z;
boolean px = voxelMap.containsKey(tempPos.set(x + 1, y, z));
boolean nx = voxelMap.containsKey(tempPos.set(x - 1, y, z));
boolean py = voxelMap.containsKey(tempPos.set(x, y + 1, z));
boolean ny = voxelMap.containsKey(tempPos.set(x, y - 1, z));
boolean pz = voxelMap.containsKey(tempPos.set(x, y, z + 1));
boolean nz = voxelMap.containsKey(tempPos.set(x, y, z - 1));
if (!px) {
addVoxelFace(CubeModel.facePX, voxelI);
}
if (!nx) {
addVoxelFace(CubeModel.faceNX, voxelI);
}
if (!py) {
addVoxelFace(CubeModel.facePY, voxelI);
}
if (!ny) {
addVoxelFace(CubeModel.faceNY, voxelI);
}
if (!pz) {
addVoxelFace(CubeModel.facePZ, voxelI);
}
if (!nz) {
addVoxelFace(CubeModel.faceNZ, voxelI);
}
}
}
private void addVoxelFace(Vector3f[] face, Voxel voxel) {
for (int k = 0; k < 6; k++) {
vertices.add(new Vertex(new Vector3f(face[k].x + voxel.position.x, face[k].y + voxel.position.y, face[k].z + voxel.position.z), CubeModel.NORMALS[k], CubeModel.UVS[k]));
}
}
private void populateList() {
for (Vertex vertex : vertices) {
positionList.add(vertex.positions.x);
positionList.add(vertex.positions.y);
positionList.add(vertex.positions.z);
normalList.add(vertex.normals.x);
normalList.add(vertex.normals.y);
normalList.add(vertex.normals.z);
uvList.add(vertex.uvs.x);
uvList.add(vertex.uvs.y);
}
positions = new float[positionList.size()];
uvs = new float[uvList.size()];
normals = new float[normalList.size()];
for (int i = 0; i < positionList.size(); i++) {
positions[i] = positionList.get(i);
}
for (int i = 0; i < uvList.size(); i++) {
uvs[i] = uvList.get(i);
}
for (int i = 0; i < normalList.size(); i++) {
normals[i] = normalList.get(i);
}
positionList.clear();
uvList.clear();
normalList.clear();
}
}