perlin noise

This commit is contained in:
Piotrek
2021-04-26 15:23:40 +02:00
parent beea89699d
commit dae867be9f
6 changed files with 174 additions and 48 deletions
+5 -8
View File
@@ -3,14 +3,16 @@ use winit::{
window::Window
};
use cgmath::Point3;
use noise::Perlin;
use std::sync::Arc;
use noise::NoiseFn;
mod camera_controller; use camera_controller::CameraController;
mod world_gen; use world_gen::WorldGen;
use crate::renderer::Renderer;
//use crate::renderer::BrickData;
pub struct App {
pub window: Window,
pub renderer: Renderer,
@@ -25,13 +27,8 @@ impl App {
let mut world_gen = WorldGen::new();
let start = std::time::Instant::now();
for x in 0..48 {
for y in 0..16 {
for z in 0..48 {
world_gen.generate(&mut renderer, Point3{x, y, z});
}
}
}
let noise = Perlin::new();
world_gen.generate(&mut renderer, &noise);
println!("Generated in {}ms", start.elapsed().as_millis());
Self { window, renderer, camera_controller, world_gen }
+80 -31
View File
@@ -1,7 +1,10 @@
use cgmath::{Point3, InnerSpace};
use noise::{Perlin, NoiseFn};
use rand::Rng;
use crate::renderer::Renderer;
use crate::renderer::buffers::{BLOCK_SIZE, NODE_TEX_SIZE};
pub struct WorldGen {
@@ -13,44 +16,90 @@ impl WorldGen {
Self{}
}
pub fn generate(&mut self, renderer: &mut Renderer, pos: Point3<usize>) {
/*
* Generate world
*/
pub fn generate(&mut self, renderer: &mut Renderer, noise: &Perlin) {
const SCALE: f64 = 150.0;
let mut rng = rand::thread_rng();
let mut apply = false;
let block_id = renderer.block_get_id(pos, false);
// Generate grass on ground
if pos.y == 0 {
for x in 0..32 {
for z in 0..32 {
// Dirt
renderer.block_set_voxel(block_id, x, 0, z, 1);
// Grass
if rng.gen::<f32>() < 0.3 {
let h = (rng.gen::<f32>() * 5.0) as usize;
for y in 1..h {
renderer.block_set_voxel(block_id, x, y, z, 2);
for bx in 0..32 {
for bz in 0..32 {
let mut block_ids = [0_usize; 16];
for by in 0..2 {
//TODO consume only nonempty blocks
block_ids[by] = renderer.block_get_id(Point3{x:bx, y:by, z:bz}, true);
}
for x in 0..BLOCK_SIZE {
for z in 0..BLOCK_SIZE {
// world x,z
let wx = bx * BLOCK_SIZE + x;
let wz = bz * BLOCK_SIZE + z;
// world h
let value1 = (noise.get([wx as f64 /SCALE*2.0, wz as f64 /SCALE*2.0]) + 1.0) / 1.5 - 0.6;
let value2 = (noise.get([wx as f64 /SCALE, wz as f64 /SCALE]) + 1.0) / 2.0;
let value = if value1 > value2 { value1 } else {value2};
let mut wh = (value * 40.0) as usize + 1;
wh += (rng.gen::<f32>() * 2.0) as usize;
// fill column
for wy in 0..wh {
let y = wy % BLOCK_SIZE;
let by = wy / BLOCK_SIZE;
let val = if wy == wh-1 { 2 } else { 1 };
renderer.block_set_voxel(block_ids[by], x, y, z, val);
}
}
}
}
apply = true;
}
// Generate wall
if pos.x == 8 && pos.y < 3 && pos.z < 8 && pos.z > 4 {
for z in 0..32 {
for y in 0..32 {
renderer.block_set_voxel(block_id, 16, y, z, 3);
}
}
apply = true;
}
// Apply
if apply {
if block_id != renderer.block_get_id(pos, true) {
panic!("Block already consumed?");
}
}
// let mut rng = rand::thread_rng();
// let mut apply = false;
// let voxel_pos = pos * BLOCK_SIZE;
// // Genereate grass
// if pos.y < 1 {
// for x in 0..32 {
// for z in 0..32 {
// let wx = voxel_pos.x + x;
// let wz = voxel_pos.z + z;
// let value = (noise.get([wx as f64 * 0.0123, wz as f64 * 0.0123]) + 1.0) / 2.0;
// let h = (value * 30.0) as usize + 1;
// for y in 0..h {
// renderer.block_set_voxel(block_id, x, y, z, 2);
// }
// }
// }
// apply = true;
// }
// // Generate wall
// if pos.x == 8 && pos.y < 3 && pos.z < 8 && pos.z > 4 {
// for z in 0..32 {
// for y in 0..32 {
// renderer.block_set_voxel(block_id, 16, y, z, 3);
// }
// }
// apply = true;
// }
// // Apply
// if apply {
// if block_id != renderer.block_get_id(pos, true) {
// panic!("Block already consumed?");
// }
// }
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
use winit::{window::Window, dpi::PhysicalSize};
use cgmath::{Point3};
mod buffers;
pub mod buffers;
use buffers::{Nodes, Blocks, Uniform, Raster, Materials};
mod passes;
Binary file not shown.