world gen: basic implementation of smooth terrain using opensimplexnoise

treemaps-chunkstates-spacefilling
EmaMaker 2022-08-23 21:06:59 +02:00
parent f9bee3d758
commit 9548841cc8
4 changed files with 30 additions and 5 deletions

View File

@ -1,8 +1,10 @@
package com.emamaker.voxeltest.intervaltrees.world;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import com.emamaker.voxeltest.intervaltrees.Voxel;
import com.emamaker.voxeltest.intervaltrees.utils.OpenSimplexNoise;
import com.emamaker.voxeltest.intervaltrees.world.chunk.ChunkGenerator;
import com.emamaker.voxeltest.intervaltrees.world.chunk.ChunkMesher;
import com.emamaker.voxeltest.intervaltrees.utils.Config;
@ -19,6 +21,7 @@ public class WorldManager {
ChunkMesher chunkMesher = new ChunkMesher();
ChunkGenerator chunkGenerator = new ChunkGenerator();
OpenSimplexNoise noiseGenerator;
Node worldNode = new Node();
@ -29,6 +32,7 @@ public class WorldManager {
}
public void initWorld() {
noiseGenerator = new OpenSimplexNoise(new Random().nextLong());
}
int chunkX, chunkY, chunkZ;
@ -65,7 +69,7 @@ public class WorldManager {
meshed = false;
for(Chunk c : chunks.values()){
if(!generated && !c.bgetState(Chunk.CHUNK_STATE_GENERATED)) {
chunkGenerator.arrayGenerateCorner(c);
chunkGenerator.generateNoise(c, noiseGenerator);
c.setState(Chunk.CHUNK_STATE_GENERATED, true);
generated = true;
}
@ -84,7 +88,7 @@ public class WorldManager {
}
public void render(){
for(Chunk c : chunks.values()){
if(!c.bgetState(Chunk.CHUNK_STATE_LOADED) && c.bgetState(Chunk.CHUNK_STATE_GENERATED) && c.bgetState(Chunk.CHUNK_STATE_MESHED)){
if(!c.bgetState(Chunk.CHUNK_STATE_EMPTY) && !c.bgetState(Chunk.CHUNK_STATE_LOADED) && c.bgetState(Chunk.CHUNK_STATE_GENERATED) && c.bgetState(Chunk.CHUNK_STATE_MESHED)){
game.getRootNode().attachChild(c.chunkNode);
c.setState(Chunk.CHUNK_STATE_LOADED, true);
}

View File

@ -17,6 +17,7 @@ public class Chunk {
public static byte CHUNK_STATE_GENERATED = 1;
public static byte CHUNK_STATE_MESHED = 2;
public static byte CHUNK_STATE_LOADED = 4;
public static byte CHUNK_STATE_EMPTY = 64;
public Chunk() {
this(0,0,0);

View File

@ -1,13 +1,14 @@
package com.emamaker.voxeltest.intervaltrees.world.chunk;
import com.emamaker.voxeltest.intervaltrees.utils.Config;
import com.emamaker.voxeltest.intervaltrees.utils.OpenSimplexNoise;
import com.emamaker.voxeltest.intervaltrees.utils.SpaceFillingCurve;
import com.emamaker.voxeltest.intervaltrees.world.blocks.Blocks;
public class ChunkGenerator {
public void generateCube(Chunk c) {
c.setBlocks(Blocks.STONE, 0,
public void generateCube(Chunk c, Blocks block) {
c.setBlocks(block, 0,
Config.CHUNK_3DCOORD_MAX_INDEX);
}
@ -38,4 +39,23 @@ public class ChunkGenerator {
c.treeFrom1DArray(array);
}
public void generateNoise(Chunk c, OpenSimplexNoise noiseGenerator) {
if(c.posy <= 5) generateCube(c, Blocks.STONE);
else if(c.posy == 6){
Blocks[] array = new Blocks[Config.CHUNK_3DCOORD_MAX_INDEX + 1];
int noise, hilbert;
for (int i = 0; i < Config.CHUNK_SIZE; i++) {
for (int k = 0; k < Config.CHUNK_SIZE; k++) {
noise = Math.min((int)((1+noiseGenerator.eval((c.posx*Config.CHUNK_SIZE+i)*0.025, (c.posz*Config.CHUNK_SIZE+k)*0.025))*10), Config.CHUNK_SIZE -1);
System.out.println(noise);
for (int j = 0; j < noise; j++) array[SpaceFillingCurve.HILBERT_XYZ_ENCODE[i][j][k]] = Blocks.DIRT;
array[SpaceFillingCurve.HILBERT_XYZ_ENCODE[i][noise][k]] = Blocks.GRASS;
}
}
c.treeFrom1DArray(array);
}else c.setState(Chunk.CHUNK_STATE_EMPTY, true);}
}

View File

@ -235,7 +235,7 @@ public class ChunkMesher {
mat.setBoolean("VertexColor", true);
// mat.getAdditionalRenderState().setWireframe(true);
//mat.getAdditionalRenderState().setWireframe(true);
// mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Front);
geo.setMaterial(mat);