basic terrain

This commit is contained in:
griffi-gh 2023-01-18 03:42:58 +01:00
parent 67c53d78bb
commit ed5b8de09a
2 changed files with 16 additions and 14 deletions

View file

@ -11,7 +11,7 @@ env_logger = "0.10"
strum = { version = "0.24", features = ["derive"] } strum = { version = "0.24", features = ["derive"] }
glam = { version = "0.22", features = ["debug-glam-assert", "mint", "fast-math"] } glam = { version = "0.22", features = ["debug-glam-assert", "mint", "fast-math"] }
hashbrown = "0.13" hashbrown = "0.13"
simdnoise = "3.1" noise = "0.8"
rayon = "1.6" rayon = "1.6"
[features] [features]

View file

@ -1,32 +1,34 @@
use glam::IVec2; use glam::{Vec2, DVec2, IVec2};
use simdnoise::NoiseBuilder; use noise::{NoiseFn, Perlin, Simplex, Fbm, Seedable};
use crate::game::{ use crate::game::{
world::chunk::{ChunkData, CHUNK_SIZE, CHUNK_HEIGHT}, world::chunk::{ChunkData, CHUNK_SIZE, CHUNK_HEIGHT},
blocks::Block blocks::Block
}; };
const TERRAIN_HEIGHT_MIN: f32 = 64.; const HEIGHTMAP_SCALE: f64 = 0.004;
const TERRAIN_HEIGHT_MAX: f32 = 80.; const TERRAIN_HEIGHT_MIN: f64 = 64.;
const TERRAIN_HEIGHT_MAX: f64 = 80.;
pub fn generate_chunk(position: IVec2, seed: u32) -> ChunkData { pub fn generate_chunk(position: IVec2, seed: u32) -> ChunkData {
let world_xz = position.as_vec2() * CHUNK_SIZE as f32; let world_xz = position.as_vec2() * CHUNK_SIZE as f32;
let mut chunk = Box::new([[[Block::Air; CHUNK_SIZE]; CHUNK_HEIGHT]; CHUNK_SIZE]); let mut chunk = Box::new([[[Block::Air; CHUNK_SIZE]; CHUNK_HEIGHT]; CHUNK_SIZE]);
//generate noises //generate noises
let height_noise = NoiseBuilder::fbm_2d_offset(world_xz.x, CHUNK_SIZE, world_xz.y, CHUNK_SIZE) let mut terrain_base_fbm: Fbm<Perlin> = Fbm::new(seed);
.with_freq(0.025) terrain_base_fbm.octaves = 6;
.with_octaves(4)
.with_seed(seed as i32)
.generate_scaled(TERRAIN_HEIGHT_MIN, TERRAIN_HEIGHT_MAX);
//put everything together //put everything together
for x in 0..CHUNK_SIZE { for x in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE { for z in 0..CHUNK_SIZE {
let heightmap = height_noise[x + z * CHUNK_SIZE] as usize; let point = world_xz.as_dvec2() + DVec2::from_array([x as f64, z as f64]);
for y in 0..heightmap {
let heightmap = (terrain_base_fbm.get((point * HEIGHTMAP_SCALE).to_array()) + 1.) / 2.;
//generate basic terrain
let terain_height = (TERRAIN_HEIGHT_MIN + (heightmap * TERRAIN_HEIGHT_MAX)).floor() as usize;
for y in 0..terain_height {
chunk[x][y][z] = Block::Dirt; chunk[x][y][z] = Block::Dirt;
} }
chunk[x][heightmap][z] = Block::Grass; chunk[x][terain_height][z] = Block::Grass;
} }
} }