World 0,0 is now at the center of the node texture
This commit is contained in:
+4
-1
@@ -67,7 +67,10 @@ impl Game {
|
|||||||
// Get positions
|
// Get positions
|
||||||
let pos = camera.get_position();
|
let pos = camera.get_position();
|
||||||
let pos = Vector3{x: pos.x as i32, y: pos.y as i32, z: pos.z as i32};
|
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
|
// Block position
|
||||||
if self.prev_pos == None || self.prev_pos != Some(pos) {
|
if self.prev_pos == None || self.prev_pos != Some(pos) {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use std::time::Instant;
|
||||||
use cgmath::Vector3;
|
use cgmath::Vector3;
|
||||||
use noise::{Perlin, NoiseFn};
|
use noise::{Perlin, NoiseFn};
|
||||||
use rand::{Rng, prelude::ThreadRng};
|
use rand::{Rng, prelude::ThreadRng};
|
||||||
@@ -16,21 +17,22 @@ impl WorldGen {
|
|||||||
/*
|
/*
|
||||||
* Generate height map for given block position
|
* 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) {
|
fn gen_height_map(x: i32, z: i32, rand: &mut ThreadRng, noise: &Perlin) -> ([[usize;block::SIZE]; block::SIZE], usize) {
|
||||||
let x = x * block::SIZE;
|
let size = block::SIZE as i32;
|
||||||
let z = z * block::SIZE;
|
let x = x * size;
|
||||||
|
let z = z * size;
|
||||||
|
|
||||||
// Generate map
|
// Generate map
|
||||||
let mut max: usize = 0;
|
let mut max: usize = 0;
|
||||||
let mut map = [[0_usize; block::SIZE]; block::SIZE];
|
let mut map = [[0_usize; block::SIZE]; block::SIZE];
|
||||||
for lx in 0..block::SIZE {
|
for lx in 0..size {
|
||||||
for lz in 0..block::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 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;
|
let mut h = (value * 30.0) as usize;
|
||||||
h += (rand.gen::<f32>() * 2.0) as usize;
|
h += (rand.gen::<f32>() * 2.0) as usize;
|
||||||
if h == 0 { h = 1; }
|
if h == 0 { h = 1; }
|
||||||
if h > max { max = h; }
|
if h > max { max = h; }
|
||||||
map[lx][lz] = h;
|
map[lx as usize][lz as usize] = h;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(map, max)
|
(map, max)
|
||||||
@@ -44,8 +46,8 @@ impl WorldGen {
|
|||||||
let noise = Perlin::new();
|
let noise = Perlin::new();
|
||||||
|
|
||||||
// Generate height map
|
// 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
|
// Fill
|
||||||
for x in 0..block::SIZE {
|
for x in 0..block::SIZE {
|
||||||
for z in 0..block::SIZE {
|
for z in 0..block::SIZE {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ mod generator;
|
|||||||
mod data;
|
mod data;
|
||||||
pub mod chunk;
|
pub mod chunk;
|
||||||
pub mod block;
|
pub mod block;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|||||||
@@ -103,6 +103,10 @@ impl Content {
|
|||||||
|
|
||||||
pub fn write(&mut self, queue: &wgpu::Queue, block_pos: &Vector3<i32>, block: &WorldBlock) {
|
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
|
// 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 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;
|
let mut value = self.node_buffer[index] as usize;
|
||||||
@@ -167,7 +171,7 @@ impl Content {
|
|||||||
|
|
||||||
self.node_buffer = result;
|
self.node_buffer = result;
|
||||||
self.node_dirty = true;
|
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) {
|
fn write_nodes(&mut self, queue: &wgpu::Queue, z: usize) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
mod uniform;
|
mod uniform;
|
||||||
pub use uniform::Uniform;
|
pub use uniform::Uniform;
|
||||||
pub use uniform::UniformValues;
|
pub use uniform::UniformValues;
|
||||||
mod content;
|
pub mod content;
|
||||||
pub use content::Content;
|
pub use content::Content;
|
||||||
mod materials;
|
mod materials;
|
||||||
pub use materials::Materials;
|
pub use materials::Materials;
|
||||||
|
|||||||
+3
-1
@@ -117,7 +117,9 @@ impl Renderer {
|
|||||||
pub fn render(&mut self) -> Result<(), wgpu::SwapChainError> {
|
pub fn render(&mut self) -> Result<(), wgpu::SwapChainError> {
|
||||||
|
|
||||||
// Update uniform buffer
|
// 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.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]));
|
self.queue.write_buffer(&self.buffers.uniforms.buffer, 0, bytemuck::cast_slice(&[self.buffers.uniforms.values]));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user