only generate and mesh one chunk per loop cycle

modeling/meshing needs optimization
treemaps-queues-iteration
EmaMaker 2022-08-20 17:33:58 +02:00
parent fe8268a10a
commit 6090a2d04a
2 changed files with 25 additions and 14 deletions

View File

@ -1,7 +1,10 @@
package com.emamaker.voxeltest.intervaltrees.utils;
public class Utils {
public static boolean withinDistance(int startx, int starty, int startz, int x, int y, int z, int dist) {
return (x-startx)*(x-startx) + (y - starty)*(y-starty) + (z-startz)*(z-startz) <= dist*dist;
}
// https://stackoverflow.com/questions/20266201/3d-array-1d-flat-indexing
//flatten 3d coords to 1d array cords
public static int coord3DTo1D (int x, int y, int z, int maxX, int maxY, int maxZ){

View File

@ -7,6 +7,7 @@ import java.util.Queue;
import com.emamaker.voxeltest.intervaltrees.Voxel;
import com.emamaker.voxeltest.intervaltrees.renderer.ChunkRenderer;
import com.emamaker.voxeltest.intervaltrees.utils.Config;
import com.emamaker.voxeltest.intervaltrees.utils.Utils;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
@ -79,35 +80,44 @@ public class WorldManager {
// for (int j = Math.max(0,chunkY - Config.RENDER_DISTANCE); j < chunkY +
// Config.RENDER_DISTANCE; j++) {
for (int k = Math.max(0, chunkZ - Config.RENDER_DISTANCE); k < chunkZ + Config.RENDER_DISTANCE; k++) {
v = new Vector3f(i,j,k); // this part has to be revised, i can see spikes in memory usage
if(!Utils.withinDistance(chunkX, chunkY, chunkZ, i,j,k,Config.RENDER_DISTANCE)) continue;
v = new Vector3f(i, j, k); // this part has to be revised, i can see spikes in memory usage
if (chunks.get(v) == null) {
chunks.put(v, new Chunk(v));
chunksToGenerate.add(v);
chunksToModel.add(v);
// chunks.get(v).generatePlane();
// chunkRenderer.stupidArrayMeshing(this, chunks.get(v));
// game.getRootNode().attachChild(chunks.get(v).chunkNode);
}
chunksToRender.add(v);
v=null;
}else
chunksToRender.add(v);
v = null;
}
}
}
while (!chunksToGenerate.isEmpty())
chunks.get(chunksToGenerate.poll()).arrayGenerateCorner();
if (!chunksToGenerate.isEmpty()){
v = chunksToGenerate.poll();
if(v != null) {
chunks.get(v).arrayGenerateCorner();
chunksToModel.add(v);
}
}
while (!chunksToModel.isEmpty()){
if (!chunksToModel.isEmpty()){
v = chunksToModel.poll();
chunkRenderer.greedyMeshing(this, chunks.get(v));
if(v != null) {
chunkRenderer.greedyMeshing(this, chunks.get(v));
chunksToRender.add(v);
}
}
while (!lastRenderedChunks.isEmpty()) {
v = lastRenderedChunks.poll();
c = chunks.get(v);
if (!chunkInCameraLimits(c)) {
if (!Utils.withinDistance(chunkX, chunkY, chunkZ, (int)(c.pos.x), (int)(c.pos.y), (int)(c.pos.z), Config.RENDER_DISTANCE)) {
c.chunkNode.removeFromParent();
chunks.remove(v);
}
@ -129,9 +139,7 @@ public class WorldManager {
int vx = (int) c.pos.x;
int vy = (int) c.pos.y;
int vz = (int) c.pos.z;
return vx >= chunkX - Config.RENDER_DISTANCE && vx < chunkX + Config.RENDER_DISTANCE
&& vy >= chunkY - Config.RENDER_DISTANCE && vy < chunkY + Config.RENDER_DISTANCE
&& vz >= chunkZ - Config.RENDER_DISTANCE && vz < chunkZ + Config.RENDER_DISTANCE;
return vx >= Math.min(0, chunkX - Config.RENDER_DISTANCE ) && vx < Math.min(0, chunkX + Config.RENDER_DISTANCE) && vy >= chunkY - Config.RENDER_DISTANCE && vy < Math.min(0, chunkY + Config.RENDER_DISTANCE) && vz >= chunkZ - Config.RENDER_DISTANCE && vz < Math.min(0, chunkZ + Config.RENDER_DISTANCE);
}
}