diff options
author | Lizzy Fleckenstein <eliasfleckenstein@web.de> | 2023-05-15 18:14:14 +0200 |
---|---|---|
committer | Lizzy Fleckenstein <eliasfleckenstein@web.de> | 2023-05-15 18:14:14 +0200 |
commit | 6f887d475d32f32ebdd1f914f5067031b12d025d (patch) | |
tree | 95800ba09223eb2107d95181bee53dde7733749e /src | |
parent | f1e5cdf25d1fd905fa307232124c361851b51758 (diff) | |
download | mt_client-6f887d475d32f32ebdd1f914f5067031b12d025d.tar.xz |
Frustum culling
Diffstat (limited to 'src')
-rw-r--r-- | src/gfx/map.rs | 27 | ||||
-rw-r--r-- | src/gfx/map/mesh.rs | 4 | ||||
-rw-r--r-- | src/gfx/state.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 2 |
4 files changed, 26 insertions, 11 deletions
diff --git a/src/gfx/map.rs b/src/gfx/map.rs index 3d49afd..5a714a2 100644 --- a/src/gfx/map.rs +++ b/src/gfx/map.rs @@ -4,6 +4,7 @@ mod mesh; use super::{media::MediaMgr, state::State, util::MatrixUniform}; use atlas::create_atlas; use cgmath::{prelude::*, Matrix4, Point3, Vector3}; +use collision::{prelude::*, Aabb3, Relation}; use mesh::{create_mesh, MeshData}; use mt_net::{MapBlock, NodeDef}; use serde::{Deserialize, Serialize}; @@ -119,7 +120,7 @@ struct BlockModel { transform: MatrixUniform, } -fn block_float_pos(pos: Vector3<i16>) -> Vector3<f32> { +fn block_float_pos(pos: Point3<i16>) -> Point3<f32> { pos.cast::<f32>().unwrap() * 16.0 } @@ -138,7 +139,19 @@ impl MapRender { let mut blend = Vec::new(); - for (index, (pos, model)) in self.blocks.iter().enumerate() { + for (index, (&pos, model)) in self.blocks.iter().enumerate() { + if model.mesh.is_none() && model.mesh_blend.is_none() { + continue; + } + + let fpos = block_float_pos(Point3::from(pos)); + let one = Vector3::new(1.0, 1.0, 1.0); + let aabb = Aabb3::new(fpos - one * 0.5, fpos + one * 15.5).transform(&state.view); + + if state.frustum.contains(&aabb) == Relation::Out { + continue; + } + if let Some(mesh) = &model.mesh { mesh.render(pass, &model.transform); } @@ -146,9 +159,9 @@ impl MapRender { if let Some(mesh) = &model.mesh_blend { blend.push(BlendEntry { index, - dist: (block_float_pos(Vector3::from(*pos)) - - Vector3::from(state.camera.position)) - .magnitude(), + dist: (state.view * (fpos + one * 8.5).to_homogeneous()) + .truncate() + .magnitude(), mesh, transform: &model.transform, }); @@ -181,9 +194,7 @@ impl MapRender { transform: MatrixUniform::new( &state.device, &self.model, - Matrix4::from_translation( - block_float_pos(pos.to_vec()) + Vector3::new(8.5, 8.5, 8.5), - ), + Matrix4::from_translation(block_float_pos(pos).to_vec()), "mapblock", false, ), diff --git a/src/gfx/map/mesh.rs b/src/gfx/map/mesh.rs index 3f8dcbc..51bf9f6 100644 --- a/src/gfx/map/mesh.rs +++ b/src/gfx/map/mesh.rs @@ -57,7 +57,7 @@ pub(super) fn create_mesh( } let light = match def.param1_type { - Param1Type::Light => block.param_1[index] as f32 / 15.0, + Param1Type::Light => block.param_1[index] as f32 / 15.0, // FIXME _ => 1.0, }; @@ -81,7 +81,7 @@ pub(super) fn create_mesh( let mut add_vertex = |vertex: (usize, &([f32; 3], [f32; 2]))| { buffer.vertices.push(Vertex { - pos: array(|i| pos[i] as f32 - 8.5 + vertex.1 .0[i]), + pos: array(|i| pos[i] as f32 + vertex.1 .0[i]), tex_coords: texture[vertex.0], light, }); diff --git a/src/gfx/state.rs b/src/gfx/state.rs index 43ac41c..1af6653 100644 --- a/src/gfx/state.rs +++ b/src/gfx/state.rs @@ -1,5 +1,6 @@ use super::util::MatrixUniform; use cgmath::{prelude::*, Deg, Matrix4, Rad}; +use collision::Frustum; use fps_camera::{FirstPerson, FirstPersonSettings}; use std::time::Duration; @@ -11,6 +12,7 @@ pub struct State { pub fov: Rad<f32>, pub view: Matrix4<f32>, pub proj: Matrix4<f32>, + pub frustum: Frustum<f32>, pub camera: FirstPerson, pub camera_uniform: MatrixUniform, pub camera_bind_group_layout: wgpu::BindGroupLayout, @@ -100,6 +102,7 @@ impl State { fov: Deg(90.0).into(), proj: Matrix4::identity(), view: Matrix4::identity(), + frustum: Frustum::from_matrix4(Matrix4::identity()).unwrap(), camera, camera_uniform, camera_bind_group_layout, @@ -174,6 +177,7 @@ impl State { 0.1, 100000.0, ); + self.frustum = Frustum::from_matrix4(self.proj).unwrap(); } pub fn update(&mut self, dt: Duration) { diff --git a/src/main.rs b/src/main.rs index 0d83ae5..04e8a91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ pub enum NetEvent { fn main() { println!(include_str!("../assets/ascii-art.txt")); - println!("Early WIP. Expext breakage. Trans rights <3"); + println!("Early WIP. Expect breakage. Trans rights <3"); let (net_tx, net_rx) = mpsc::unbounded_channel(); let event_loop = winit::event_loop::EventLoopBuilder::<GfxEvent>::with_user_event().build(); |