perlin noise
This commit is contained in:
Generated
+87
-8
@@ -620,6 +620,17 @@ dependencies = [
|
||||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.1.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"libc",
|
||||
"wasi 0.9.0+wasi-snapshot-preview1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.2"
|
||||
@@ -628,7 +639,7 @@ checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"libc",
|
||||
"wasi",
|
||||
"wasi 0.10.2+wasi-snapshot-preview1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1243,6 +1254,17 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "noise"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82051dd6745d5184c6efb7bc8be14892a7f6d4f3ad6dbf754d1c7d7d5fe24b43"
|
||||
dependencies = [
|
||||
"image",
|
||||
"rand 0.7.3",
|
||||
"rand_xorshift",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "6.1.2"
|
||||
@@ -1476,6 +1498,19 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
|
||||
dependencies = [
|
||||
"getrandom 0.1.16",
|
||||
"libc",
|
||||
"rand_chacha 0.2.2",
|
||||
"rand_core 0.5.1",
|
||||
"rand_hc 0.2.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.3"
|
||||
@@ -1483,9 +1518,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
"rand_hc",
|
||||
"rand_chacha 0.3.0",
|
||||
"rand_core 0.6.2",
|
||||
"rand_hc 0.3.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core 0.5.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1495,7 +1540,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
"rand_core 0.6.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
|
||||
dependencies = [
|
||||
"getrandom 0.1.16",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1504,7 +1558,16 @@ version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"getrandom 0.2.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_hc"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
|
||||
dependencies = [
|
||||
"rand_core 0.5.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1513,7 +1576,16 @@ version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
|
||||
dependencies = [
|
||||
"rand_core",
|
||||
"rand_core 0.6.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_xorshift"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77d416b86801d23dde1aa643023b775c3a462efc0ed96443add11546cdf1dca8"
|
||||
dependencies = [
|
||||
"rand_core 0.5.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1819,6 +1891,12 @@ dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.9.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.2+wasi-snapshot-preview1"
|
||||
@@ -2051,7 +2129,8 @@ dependencies = [
|
||||
"futures",
|
||||
"glob",
|
||||
"image",
|
||||
"rand",
|
||||
"noise",
|
||||
"rand 0.8.3",
|
||||
"shaderc",
|
||||
"wgpu",
|
||||
"winit",
|
||||
|
||||
@@ -15,6 +15,7 @@ byteorder = "1.4"
|
||||
image = "0.23.14"
|
||||
cgmath = "0.18"
|
||||
rand = "0.8"
|
||||
noise = "0.7"
|
||||
|
||||
[build-dependencies]
|
||||
anyhow = "1.0"
|
||||
|
||||
+5
-8
@@ -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
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
apply = true;
|
||||
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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
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;
|
||||
}
|
||||
|
||||
// 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
@@ -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.
Reference in New Issue
Block a user