World 0,0 is now at the center of the node texture

This commit is contained in:
Piotrek
2021-05-14 14:25:12 +02:00
parent 05b62d4b90
commit e6329d568c
6 changed files with 24 additions and 12 deletions
+4 -1
View File
@@ -67,7 +67,10 @@ impl Game {
// Get positions
let pos = camera.get_position();
let pos = Vector3{x: pos.x as i32, y: pos.y as i32, z: pos.z as i32};
let chunk_pos = pos / world::chunk::SIZE as i32;
let mut chunk_pos = pos / world::chunk::SIZE as i32;
if pos.x < 0 { chunk_pos.x -= 1; }
if pos.y < 0 { chunk_pos.y -= 1; }
if pos.z < 0 { chunk_pos.z -= 1; }
// Block position
if self.prev_pos == None || self.prev_pos != Some(pos) {
+9 -7
View File
@@ -1,3 +1,4 @@
use std::time::Instant;
use cgmath::Vector3;
use noise::{Perlin, NoiseFn};
use rand::{Rng, prelude::ThreadRng};
@@ -16,21 +17,22 @@ impl WorldGen {
/*
* Generate height map for given block position
*/
fn gen_height_map(x: usize, z: usize, rand: &mut ThreadRng, noise: &Perlin) -> ([[usize;block::SIZE]; block::SIZE], usize) {
let x = x * block::SIZE;
let z = z * block::SIZE;
fn gen_height_map(x: i32, z: i32, rand: &mut ThreadRng, noise: &Perlin) -> ([[usize;block::SIZE]; block::SIZE], usize) {
let size = block::SIZE as i32;
let x = x * size;
let z = z * size;
// Generate map
let mut max: usize = 0;
let mut map = [[0_usize; block::SIZE]; block::SIZE];
for lx in 0..block::SIZE {
for lz in 0..block::SIZE {
for lx in 0..size {
for lz in 0..size {
let value = (noise.get([(x + lx) as f64 / SCALE, (z + lz) as f64 /SCALE]) + 1.0) / 2.0;
let mut h = (value * 30.0) as usize;
h += (rand.gen::<f32>() * 2.0) as usize;
if h == 0 { h = 1; }
if h > max { max = h; }
map[lx][lz] = h;
map[lx as usize][lz as usize] = h;
}
}
(map, max)
@@ -44,7 +46,7 @@ impl WorldGen {
let noise = Perlin::new();
// Generate height map
let (map, _max) = WorldGen::gen_height_map(block_pos.x as usize, block_pos.z as usize, &mut rng, &noise);
let (map, _max) = WorldGen::gen_height_map(block_pos.x, block_pos.z, &mut rng, &noise);
// Fill
for x in 0..block::SIZE {
+1
View File
@@ -2,6 +2,7 @@ mod generator;
mod data;
pub mod chunk;
pub mod block;
use std::sync::Arc;
use std::time::Instant;
use std::path::PathBuf;
use std::io::Write;
+5 -1
View File
@@ -103,6 +103,10 @@ impl Content {
pub fn write(&mut self, queue: &wgpu::Queue, block_pos: &Vector3<i32>, block: &WorldBlock) {
// 0,0 at node buffer center
let half = (NODE_TEX_SIZE / 2);
let block_pos = block_pos + &Vector3{x: half as i32, y: half as i32, z: half as i32};
// Get node
let index = (block_pos.x + NODE_TEX_SIZE as i32 * (block_pos.y + NODE_TEX_SIZE as i32 * block_pos.z)) as usize;
let mut value = self.node_buffer[index] as usize;
@@ -167,7 +171,7 @@ impl Content {
self.node_buffer = result;
self.node_dirty = true;
println!("Shifted in {} ms", timer.elapsed().as_millis());
println!("Shifted node buffer by {:?} in {} ms", offset, timer.elapsed().as_millis());
}
fn write_nodes(&mut self, queue: &wgpu::Queue, z: usize) {
+1 -1
View File
@@ -1,7 +1,7 @@
mod uniform;
pub use uniform::Uniform;
pub use uniform::UniformValues;
mod content;
pub mod content;
pub use content::Content;
mod materials;
pub use materials::Materials;
+3 -1
View File
@@ -117,7 +117,9 @@ impl Renderer {
pub fn render(&mut self) -> Result<(), wgpu::SwapChainError> {
// Update uniform buffer
let offset = Vector3{x: self.world_offset.x as f32, y: self.world_offset.y as f32, z: self.world_offset.z as f32 };
let half = (buffers::content::NODE_TEX_SIZE / 2) as i32;
let offset = Vector3{x: (self.world_offset.x + half) as f32, y: (self.world_offset.y + half) as f32, z: (self.world_offset.z + half) as f32 };
self.buffers.uniforms.values.update(&self.camera, offset, self.start.elapsed().as_secs_f32());
self.queue.write_buffer(&self.buffers.uniforms.buffer, 0, bytemuck::cast_slice(&[self.buffers.uniforms.values]));