From 75cb0ead6b4257bc79f53b64f4f1c14e825e808f Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Tue, 7 May 2024 01:15:44 +0200 Subject: [PATCH] clean up world renderer --- kubi/src/rendering/world.rs | 222 ++---------------------------------- 1 file changed, 7 insertions(+), 215 deletions(-) diff --git a/kubi/src/rendering/world.rs b/kubi/src/rendering/world.rs index 3df4b11..e25d545 100644 --- a/kubi/src/rendering/world.rs +++ b/kubi/src/rendering/world.rs @@ -1,13 +1,12 @@ -use glam::{IVec3, Vec3}; -use shipyard::{AllStoragesView, IntoIter, NonSendSync, Unique, UniqueView, UniqueViewMut, View}; -use kubi_shared::{chunk::CHUNK_SIZE, transform::Transform}; +use glam::Vec3; +use shipyard::{AllStoragesView, IntoIter, NonSendSync, Unique, UniqueView, View}; +use kubi_shared::chunk::CHUNK_SIZE; use crate::{ camera::Camera, prefabs::TexturePrefabs, - settings::GameSettings, world::{ChunkMeshStorage, ChunkStorage}, }; -use super::{camera::{self, CameraUniformBuffer}, depth::DepthTexture, RenderCtx}; +use super::{camera::CameraUniformBuffer, depth::DepthTexture, RenderCtx}; mod pipeline; mod vertex; @@ -16,19 +15,19 @@ pub use vertex::ChunkVertex; #[derive(Unique)] pub struct WorldRenderState { pub pipeline: wgpu::RenderPipeline, - pub trans_chunk_queue: Vec, + //pub trans_chunk_queue: Vec, } pub fn init_world_render_state(storages: AllStoragesView) { storages.add_unique(WorldRenderState { pipeline: storages.run(pipeline::init_world_pipeline), - trans_chunk_queue: Vec::with_capacity(512), + //trans_chunk_queue: Vec::with_capacity(512), }) } pub fn draw_world( ctx: &mut RenderCtx, - mut state: UniqueViewMut, + state: UniqueView, camera_ubo: UniqueView, depth: UniqueView, textures: UniqueView, @@ -95,210 +94,3 @@ pub fn draw_world( } } } - - -// fn draw_params(settings: &GameSettings) -> DrawParameters { -// DrawParameters { -// depth: Depth { -// test: DepthTest::IfLess, -// write: true, -// ..Default::default() -// }, -// multisampling: settings.msaa.is_some(), -// polygon_mode: PolygonMode::Fill, //Change to Line for wireframe -// backface_culling: BackfaceCullingMode::CullClockwise, -// ..Default::default() -// } -// } - -// fn texture_sampler<'a, T>(texture: &'a T, settings: &GameSettings) -> Sampler<'a, T> { -// Sampler(texture, SamplerBehavior { -// minify_filter: MinifySamplerFilter::LinearMipmapLinear, -// magnify_filter: MagnifySamplerFilter::Nearest, -// max_anisotropy: settings.max_anisotropy.unwrap_or_default(), -// wrap_function: (SamplerWrapFunction::Clamp, SamplerWrapFunction::Clamp, SamplerWrapFunction::Clamp), -// depth_texture_comparison: None, -// }) -// } - -// pub fn draw_world( -// mut target: NonSendSync>, -// chunks: UniqueView, -// meshes: NonSendSync>, -// program: NonSendSync>, -// texture: NonSendSync>, -// transform: View, -// camera: View, -// settings: UniqueView, -// mut trans_queue: UniqueViewMut, -// ) { -// // let (camera, transform) = (&camera, &transform).iter().next().expect("No cameras in the scene"); -// // let camera_position = transform.0.to_scale_rotation_translation().2; - -// let camera = camera.iter().next().expect("No cameras in the scene"); -// let view = camera.view_matrix.to_cols_array_2d(); -// let perspective = camera.perspective_matrix.to_cols_array_2d(); - -// let draw_parameters = draw_params(&settings); -// let texture_sampler = texture_sampler(&texture.0, &settings); - -// for (&position, chunk) in &chunks.chunks { -// if let Some(key) = chunk.mesh_index { -// let mesh = meshes.get(key).expect("Mesh index pointing to nothing"); -// let world_position = position.as_vec3() * CHUNK_SIZE as f32; - -// //Skip mesh if its empty -// if mesh.index_buffer.len() == 0 && mesh.trans_index_buffer.len() == 0 { -// continue -// } - -// //Frustum culling -// { -// let minp = world_position; -// let maxp = world_position + Vec3::splat(CHUNK_SIZE as f32); -// if !camera.frustum.is_box_visible(minp, maxp) { -// continue -// } -// } - -// //Draw chunk mesh -// if mesh.index_buffer.len() > 0 { -// target.0.draw( -// &mesh.vertex_buffer, -// &mesh.index_buffer, -// &program.0, -// &uniform! { -// position_offset: world_position.to_array(), -// view: view, -// perspective: perspective, -// tex: texture_sampler, -// discard_alpha: true, -// }, -// &draw_parameters -// ).unwrap(); -// } - -// if mesh.trans_index_buffer.len() > 0 { -// trans_queue.0.push(position); -// } -// } -// } - -// // const HALF_CHUNK_SIZE: IVec3 = IVec3::splat((CHUNK_SIZE >> 1) as i32); -// // trans_queue.0.sort_by_cached_key(|&pos| -( -// // (pos + HALF_CHUNK_SIZE).distance_squared(camera_position.as_ivec3()) -// // )); -// } - -// pub fn draw_world_trans( -// mut target: NonSendSync>, -// chunks: UniqueView, -// meshes: NonSendSync>, -// program: NonSendSync>, -// texture: NonSendSync>, -// camera: View, -// settings: UniqueView, -// mut trans_queue: UniqueViewMut, -// ) { -// let camera = camera.iter().next().expect("No cameras in the scene"); -// let view = camera.view_matrix.to_cols_array_2d(); -// let perspective = camera.perspective_matrix.to_cols_array_2d(); - -// let mut draw_parameters = draw_params(&settings); -// draw_parameters.blend = Blend::alpha_blending(); -// draw_parameters.backface_culling = BackfaceCullingMode::CullingDisabled; -// draw_parameters.smooth = Some(Smooth::Fastest); - -// let texture_sampler = texture_sampler(&texture.0, &settings); - -// for position in trans_queue.0.drain(..).rev() { -// let world_position = position.as_vec3() * CHUNK_SIZE as f32; -// let mesh_idx = chunks.chunks[&position].mesh_index.expect("No mesh index"); -// let mesh = meshes.get(mesh_idx).expect("Mesh index pointing to nothing"); -// target.0.draw( -// &mesh.trans_vertex_buffer, -// &mesh.trans_index_buffer, -// &program.0, -// &uniform! { -// position_offset: world_position.to_array(), -// view: view, -// perspective: perspective, -// tex: texture_sampler, -// discard_alpha: false, -// }, -// &draw_parameters -// ).unwrap(); -// } -// } - -// pub fn draw_current_chunk_border( -// mut target: NonSendSync>, -// player: View, -// transforms: View, -// buffers: NonSendSync>, -// program: NonSendSync>, -// camera: View, -// settings: UniqueView, -// ) { -// if cfg!(target_os = "android") { -// return -// } -// if !settings.debug_draw_current_chunk_border { -// return -// } -// let camera = camera.iter().next().expect("No cameras in the scene"); -// let view = camera.view_matrix.to_cols_array_2d(); -// let perspective = camera.perspective_matrix.to_cols_array_2d(); -// let (_, &player_transform) = (&player, &transforms).iter().next().expect("No player"); -// let (_, _, player_position) = player_transform.0.to_scale_rotation_translation(); -// let player_in_chunk = ivec3( -// (player_position.x as i32).div_euclid(CHUNK_SIZE as i32), -// (player_position.y as i32).div_euclid(CHUNK_SIZE as i32), -// (player_position.z as i32).div_euclid(CHUNK_SIZE as i32), -// ); -// let world_position = player_in_chunk.as_vec3() * CHUNK_SIZE as f32; -// target.0.draw( -// &buffers.0, -// &buffers.1, -// &program.0, -// &uniform! { -// model: Mat4::from_scale_rotation_translation( -// Vec3::splat(CHUNK_SIZE as f32), -// Quat::default(), -// world_position -// ).to_cols_array_2d(), -// color: [0.25f32; 4], -// view: view, -// perspective: perspective, -// }, -// &DrawParameters { -// depth: Depth { -// test: DepthTest::IfLess, -// ..Default::default() -// }, -// blend: Blend::alpha_blending(), -// ..Default::default() -// } -// ).unwrap(); -// target.0.draw( -// &buffers.0, -// &buffers.1, -// &program.0, -// &uniform! { -// model: Mat4::from_scale_rotation_translation( -// Vec3::splat(CHUNK_SIZE as f32), -// Quat::default(), -// world_position -// ).to_cols_array_2d(), -// color: [0.0f32; 4], -// view: view, -// perspective: perspective, -// }, -// &DrawParameters { -// polygon_mode: PolygonMode::Point, -// line_width: Some(2.), -// point_size: Some(5.), -// ..Default::default() -// } -// ).unwrap(); -// }