From f312299e62d98799b2e2f2e51aebaa675660ef39 Mon Sep 17 00:00:00 2001 From: scott-c Date: Fri, 16 Aug 2019 21:45:10 +0800 Subject: [PATCH] Replace view distance culling with frustum culling --- voxygen/src/scene/camera.rs | 10 ++++++++++ voxygen/src/scene/figure.rs | 18 +++--------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/voxygen/src/scene/camera.rs b/voxygen/src/scene/camera.rs index 3021652bef..b5923b0129 100644 --- a/voxygen/src/scene/camera.rs +++ b/voxygen/src/scene/camera.rs @@ -2,6 +2,7 @@ use client::Client; use common::vol::{ReadVol, Vox}; use std::f32::consts::PI; use vek::*; +use frustum_query::frustum::Frustum; const NEAR_PLANE: f32 = 0.01; const FAR_PLANE: f32 = 10000.0; @@ -92,6 +93,15 @@ impl Camera { (view_mat, proj_mat, cam_pos) } + pub fn frustum(&self, client: &Client) -> Frustum { + let (view_mat, proj_mat, _) = self.compute_dependents(client); + + Frustum::from_modelview_and_projection( + &view_mat.into_col_array(), + &proj_mat.into_col_array(), + ) + } + /// Rotate the camera about its focus by the given delta, limiting the input accordingly. pub fn rotate_by(&mut self, delta: Vec3) { // Wrap camera yaw diff --git a/voxygen/src/scene/figure.rs b/voxygen/src/scene/figure.rs index 80bcdf5b09..ddbe94e902 100644 --- a/voxygen/src/scene/figure.rs +++ b/voxygen/src/scene/figure.rs @@ -851,14 +851,7 @@ impl FigureMgr { let tick = client.get_tick(); let ecs = client.state().ecs(); - let view_distance = client.view_distance().unwrap_or(1); - // Get player position. - let player_pos = client - .state() - .ecs() - .read_storage::() - .get(client.entity()) - .map_or(Vec3::zero(), |pos| pos.0); + let frustum = camera.frustum(client); for (entity, _, _, _, body, _) in ( &ecs.entities(), @@ -869,13 +862,8 @@ impl FigureMgr { ecs.read_storage::().maybe(), ) .join() - // Don't render figures outside the vd - .filter(|(_, pos, _, _, _, _)| { - (pos.0 - player_pos) - .map2(TerrainChunkSize::SIZE, |d, sz| d.abs() as f32 / sz as f32) - .magnitude() - < view_distance as f32 - }) + // Don't render figures outside of frustum (camera viewport, max draw distance is farplane) + .filter(|(_, pos, _, _, _, _)| frustum.sphere_intersecting(&pos.0.x, &pos.0.y, &pos.0.z, &0.0)) // Don't render dead entities .filter(|(_, _, _, _, _, stats)| stats.map_or(true, |s| !s.is_dead)) {