From 210f8c79d6b89b2baa563c1872a26d2361f87319 Mon Sep 17 00:00:00 2001 From: BreakingLead Date: Sat, 1 Jun 2024 02:19:36 +0800 Subject: [PATCH] chunk loading (1/3) --- blockworld-client/game/chunk.rs | 40 ++++++++++++------- blockworld-client/game/mod.rs | 22 +++++++++- blockworld-client/main.rs | 2 + blockworld-client/render/draw.rs | 28 ++++++------- blockworld-client/render/mod.rs | 3 +- blockworld-client/render/render_chunk.rs | 2 + blockworld-client/render/render_chunk_pool.rs | 13 ------ 7 files changed, 63 insertions(+), 47 deletions(-) delete mode 100644 blockworld-client/render/render_chunk_pool.rs diff --git a/blockworld-client/game/chunk.rs b/blockworld-client/game/chunk.rs index 1ad2069..5c18c93 100644 --- a/blockworld-client/game/chunk.rs +++ b/blockworld-client/game/chunk.rs @@ -9,7 +9,7 @@ pub const CHUNK_HEIGHT: usize = 256; pub const CHUNK_BLOCK_NUM: usize = CHUNK_SIZE * CHUNK_SIZE * CHUNK_HEIGHT; /// from `net/minecraft/util/math/ChunkPos.java` -#[derive(Clone, Copy, Hash, PartialEq, Eq)] +#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)] pub struct ChunkPos { pub x: i32, pub z: i32, @@ -38,6 +38,30 @@ pub struct Chunk { } impl Chunk { + pub fn new(chunk_x: i32, chunk_z: i32) -> Self { + let mut blocks = Box::new([Block::default(); CHUNK_BLOCK_NUM]); + for x in 0..CHUNK_SIZE as i32 { + for y in 0..3 { + for z in 0..CHUNK_SIZE as i32 { + blocks[Chunk::index(x, y, z)] = match y { + _ => Block { id: 1 }, + } + } + } + } + for y in 5..(5 + chunk_x) { + blocks[Chunk::index(7, y, 7)] = Block { id: 2 }; + } + for y in 5..(5 + chunk_z) { + blocks[Chunk::index(8, y * 2, 8)] = Block { id: 2 }; + blocks[Chunk::index(8, y * 2 + 1, 8)] = Block { id: 2 }; + } + Self { + blocks, + pos: ChunkPos::new(chunk_x, chunk_z), + } + } + /// Reference: [https://minecraft.wiki/w/Chunk_format] /// /// Format: YZX @@ -125,23 +149,9 @@ impl Default for Chunk { } } } - for x in 0..CHUNK_SIZE as i32 { - for y in 5..40 { - for z in 0..CHUNK_SIZE as i32 { - if (vec3(x as f32, y as f32, z as f32) - vec3(7.0, 15.0, 7.0)).length() <= 7.0 { - blocks[Chunk::index(x, y, z)] = match y { - _ => Block { id: 2 }, - } - } - } - } - } - Self { blocks, pos: ChunkPos::new(0, 0), } } } - -pub struct ChunkProvider; diff --git a/blockworld-client/game/mod.rs b/blockworld-client/game/mod.rs index 8eddbb3..f651e15 100644 --- a/blockworld-client/game/mod.rs +++ b/blockworld-client/game/mod.rs @@ -1,19 +1,37 @@ +use std::rc::Rc; + +use chunk_provider::ClientChunkProvider; +use world::ClientWorld; + use crate::io::input_helper::InputState; use self::player_state::PlayerState; pub mod block; pub mod chunk; -pub mod chunk_array; +pub mod chunk_provider; pub mod console_instr; pub mod player_state; pub mod register; pub mod save; pub mod settings; +pub mod world; -#[derive(Default, Debug)] pub struct Game { pub player_state: PlayerState, + pub client_world: Rc, + pub chunk_provider: ClientChunkProvider, +} +impl Default for Game { + fn default() -> Self { + let client_world = Rc::new(ClientWorld); + Self { + player_state: Default::default(), + client_world: client_world.clone(), + // ! TEMP + chunk_provider: ClientChunkProvider::new(client_world.clone(), 16), + } + } } impl Game { diff --git a/blockworld-client/main.rs b/blockworld-client/main.rs index 87a28c8..01bb6ee 100644 --- a/blockworld-client/main.rs +++ b/blockworld-client/main.rs @@ -1,3 +1,5 @@ +#![feature(int_roundings)] + use anyhow::*; use clap::Parser; diff --git a/blockworld-client/render/draw.rs b/blockworld-client/render/draw.rs index 0a5d87b..b71961a 100644 --- a/blockworld-client/render/draw.rs +++ b/blockworld-client/render/draw.rs @@ -30,6 +30,7 @@ use crate::{ }; use super::camera::{Camera, MatrixData}; +use super::render_array::RenderArray; use super::texture::Texture; use super::uniform::*; use super::{ @@ -50,7 +51,7 @@ pub struct State<'a> { pub main_pipeline: RegularPipeline, pub wireframe_pipeline: WireframePipeline, - pub render_chunk: RenderChunk, + pub render_array: RenderArray, pub texture: Texture, pub texture_bind_group: wgpu::BindGroup, @@ -84,7 +85,6 @@ pub struct State<'a> { impl<'a> State<'a> { pub async fn new(event_loop: &EventLoop<()>, boot_args: &BootArgs) -> Result> { - // /-------------------../assets/atlas.png // Create the window let mut window_attrs = Window::default_attributes().with_title("Blockworld Indev"); // set screen size based on boot_args @@ -101,9 +101,7 @@ impl<'a> State<'a> { let _player_state: PlayerState = Default::default(); let size = window.inner_size(); - // \------------------- - // /------------------- // Instance is the way to create surface and adapter. let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { backends: wgpu::Backends::PRIMARY, @@ -276,10 +274,8 @@ impl<'a> State<'a> { }, )?; - let chunk = Chunk::default(); - let render_chunk = RenderChunk::new(&device, &chunk, ®ister_table); - - let game = Game::default(); + let mut game = Game::default(); + let render_array = RenderArray::new(&mut game.chunk_provider, &device, ®ister_table); let input_state = InputState::default(); let settings = Settings { @@ -324,7 +320,7 @@ impl<'a> State<'a> { depth_texture, - render_chunk, + render_array, camera, matrix_uniform, @@ -426,9 +422,9 @@ impl<'a> State<'a> { resolve_target: None, ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color { - r: 0.1, - g: 0.2, - b: 0.3, + r: 0.0, + g: 1.0, + b: 239.0 / 255.0, a: 1.0, }), store: wgpu::StoreOp::Store, @@ -458,8 +454,10 @@ impl<'a> State<'a> { render_pass.set_bind_group(0, &self.texture_bind_group, &[]); render_pass.set_bind_group(1, &self.matrix_uniform.bind_group, &[]); - render_pass.set_vertex_buffer(0, self.render_chunk.vertex_buffer.slice(..)); - render_pass.draw(0..self.render_chunk.vertex_count, 0..1); + for (i, chunk) in self.render_array.chunks().iter().enumerate() { + render_pass.set_vertex_buffer(0, chunk.vertex_buffer.slice(..)); + render_pass.draw(0..chunk.vertex_count, 0..1); + } self.brush.draw(&mut render_pass); } @@ -491,13 +489,11 @@ impl<'a> Debug for State<'a> { .field("size", &self.size) .field("main_pipeline", &self.main_pipeline) .field("wireframe_pipeline", &self.wireframe_pipeline) - .field("render_chunk", &self.render_chunk) .field("texture", &self.texture) .field("texture_bind_group", &self.texture_bind_group) .field("depth_texture", &self.depth_texture) .field("camera", &self.camera) .field("input_state", &self.input_state) - .field("game", &self.game) .field("fps", &self.fps) .field("dt_timer", &self.dt_timer) .field("global_timer", &self.global_timer) diff --git a/blockworld-client/render/mod.rs b/blockworld-client/render/mod.rs index 296af0a..ae27716 100644 --- a/blockworld-client/render/mod.rs +++ b/blockworld-client/render/mod.rs @@ -1,11 +1,12 @@ pub mod camera; pub mod draw; pub mod pipeline; +pub mod render_array; pub mod render_block; pub mod render_chunk; -pub mod render_chunk_pool; pub mod texture; pub mod uniform; pub mod utils; pub mod vertex; pub mod window_init; +pub mod world_renderer; diff --git a/blockworld-client/render/render_chunk.rs b/blockworld-client/render/render_chunk.rs index 70b7a6f..4b31717 100644 --- a/blockworld-client/render/render_chunk.rs +++ b/blockworld-client/render/render_chunk.rs @@ -1,5 +1,6 @@ use super::render_block::*; use glam::*; +use log::info; use wgpu::{util::DeviceExt, Device}; use crate::game::{chunk::*, register::RegisterTable}; @@ -14,6 +15,7 @@ pub struct RenderChunk { impl RenderChunk { pub fn new(device: &Device, chunk: &Chunk, register_table: &RegisterTable) -> Self { + info!("New renderchunk in {:?}", chunk.pos); let mut vertices: Vec = Vec::new(); for x in 0..CHUNK_SIZE as i32 { for y in 0..CHUNK_HEIGHT as i32 { diff --git a/blockworld-client/render/render_chunk_pool.rs b/blockworld-client/render/render_chunk_pool.rs deleted file mode 100644 index 2888136..0000000 --- a/blockworld-client/render/render_chunk_pool.rs +++ /dev/null @@ -1,13 +0,0 @@ -use crate::game::chunk::Chunk; - -pub struct RenderChunkPool {} - -impl RenderChunkPool { - fn new() -> Self { - todo!() - } - - fn add(&mut self, chunk: Chunk) { - todo!() - } -}