terrain generation with noise
this method does not require a conversion from an array, instead can write directly to the interval maptreemaps-chunkstates-spacefilling
parent
b9e55f717b
commit
a3e9786fcb
|
@ -69,7 +69,7 @@ public class WorldManager {
|
|||
meshed = false;
|
||||
for(Chunk c : chunks.values()){
|
||||
if(!generated && !c.bgetState(Chunk.CHUNK_STATE_GENERATED)) {
|
||||
chunkGenerator.generateNoise(c, noiseGenerator);
|
||||
chunkGenerator.generateNoise(c);
|
||||
c.setState(Chunk.CHUNK_STATE_GENERATED, true);
|
||||
generated = true;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@ import com.emamaker.voxeltest.intervaltrees.utils.OpenSimplexNoise;
|
|||
import com.emamaker.voxeltest.intervaltrees.utils.SpaceFillingCurve;
|
||||
import com.emamaker.voxeltest.intervaltrees.world.blocks.Blocks;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class ChunkGenerator {
|
||||
|
||||
public void generateCube(Chunk c, Blocks block) {
|
||||
|
@ -39,23 +41,87 @@ 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 GRASS_OFFSET = 80;
|
||||
int NOISE_GRASS_MULT = 20;
|
||||
int NOISE_DIRT_MULT = 3;
|
||||
int NOISE_DIRT_MIN = 2;
|
||||
float NOISE_DIRT_X_MULT = 0.001f;
|
||||
float NOISE_DIRT_Z_MULT = 0.001f;
|
||||
float NOISE_GRASS_X_MULT = 0.01f;
|
||||
float NOISE_GRASS_Z_MULT = 0.01f;
|
||||
|
||||
int noise, hilbert;
|
||||
OpenSimplexNoise noiseGen1 = new OpenSimplexNoise(new Random().nextInt());
|
||||
OpenSimplexNoise noiseGen2 = new OpenSimplexNoise(new Random().nextInt());
|
||||
public void generateNoise(Chunk c) {
|
||||
short coords[];
|
||||
int x, y, z;
|
||||
int grassNoise, dirtNoise, stoneLevel;
|
||||
int start = 0;
|
||||
Blocks prevBlock = null, block = Blocks.AIR;
|
||||
|
||||
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.setState(Chunk.CHUNK_STATE_EMPTY, true);
|
||||
for(int i = 0; i < Config.CHUNK_3DCOORD_MAX_INDEX + 1; i++){
|
||||
coords = SpaceFillingCurve.HILBERT_XYZ_DECODE[i];
|
||||
x = c.posx * Config.CHUNK_SIZE + coords[0];
|
||||
y = c.posy * Config.CHUNK_SIZE + coords[1];
|
||||
z = c.posz * Config.CHUNK_SIZE + coords[2];
|
||||
|
||||
grassNoise = GRASS_OFFSET + (int) ((0.5d + noiseGen1.eval(x * NOISE_GRASS_X_MULT, z * NOISE_GRASS_Z_MULT)) * NOISE_GRASS_MULT);
|
||||
dirtNoise = NOISE_DIRT_MIN + (int) ((0.5f + noiseGen2.eval(x * NOISE_DIRT_X_MULT, z * NOISE_DIRT_Z_MULT)) * NOISE_DIRT_MULT);
|
||||
stoneLevel = grassNoise - dirtNoise;
|
||||
|
||||
if (y < stoneLevel) block = Blocks.STONE;
|
||||
else if (y >= stoneLevel && y < grassNoise) block = Blocks.DIRT;
|
||||
else if (y == grassNoise) block = Blocks.GRASS;
|
||||
else block = Blocks.AIR;
|
||||
|
||||
if(prevBlock == null) prevBlock = block;
|
||||
|
||||
if(prevBlock != block){
|
||||
if(prevBlock != Blocks.AIR) c.setState(Chunk.CHUNK_STATE_EMPTY, false);
|
||||
c.setBlocks(prevBlock, start, i);
|
||||
start = i;
|
||||
}
|
||||
prevBlock = block;
|
||||
}
|
||||
|
||||
c.treeFrom1DArray(array);
|
||||
}else c.setState(Chunk.CHUNK_STATE_EMPTY, true);}
|
||||
if(prevBlock!= Blocks.AIR) c.setState(Chunk.CHUNK_STATE_EMPTY, false);
|
||||
c.setBlocks(prevBlock, start, Config.CHUNK_3DCOORD_MAX_INDEX + 1);
|
||||
//c.blocks.print();
|
||||
}
|
||||
|
||||
/*public void generateNoise(Chunk c) {
|
||||
short coords[];
|
||||
int x, y, z;
|
||||
int grassNoise, dirtNoise, stoneLevel;
|
||||
int start = 0;
|
||||
Blocks prevBlock = null, block = Blocks.AIR;
|
||||
int hilbert;
|
||||
Blocks[] array = new Blocks[Config.CHUNK_3DCOORD_MAX_INDEX + 1];
|
||||
c.setState(Chunk.CHUNK_STATE_EMPTY, true);
|
||||
//for(int i = 0; i < Config.CHUNK_3DCOORD_MAX_INDEX + 1; i++){
|
||||
for(int i = 0; i < Config.CHUNK_SIZE; i++ )
|
||||
for(int j = 0; j < Config.CHUNK_SIZE; j++ )
|
||||
for(int k = 0; k < Config.CHUNK_SIZE; k++ ){
|
||||
//coords = SpaceFillingCurve.HILBERT_XYZ_DECODE[i];
|
||||
hilbert = SpaceFillingCurve.HILBERT_XYZ_ENCODE[i][j][k];
|
||||
x = c.posx*Config.CHUNK_SIZE + i;
|
||||
y = c.posy*Config.CHUNK_SIZE + j;
|
||||
z = c.posz*Config.CHUNK_SIZE + k;
|
||||
|
||||
grassNoise = GRASS_OFFSET + (int) ( (0.5d + noiseGen1.eval(x*NOISE_GRASS_X_MULT, z *NOISE_GRASS_Z_MULT) )*NOISE_GRASS_MULT);
|
||||
dirtNoise = NOISE_DIRT_MIN + (int)((0.5f + noiseGen2.eval(x*NOISE_DIRT_X_MULT, z*NOISE_DIRT_Z_MULT))*NOISE_DIRT_MULT);
|
||||
stoneLevel = grassNoise - dirtNoise;
|
||||
|
||||
if(y < stoneLevel) block = Blocks.STONE;
|
||||
else if(y >= stoneLevel && y < grassNoise) block = Blocks.DIRT;
|
||||
else if(y == grassNoise) block = Blocks.GRASS;
|
||||
else block = Blocks.AIR;
|
||||
|
||||
if(block != Blocks.AIR) c.setState(Chunk.CHUNK_STATE_EMPTY, false);
|
||||
array[hilbert] = block;
|
||||
//if(prevBlock == null) prevBlock = block;
|
||||
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue