Replace view distance culling with frustum culling

This commit is contained in:
scott-c 2019-08-16 21:45:10 +08:00
parent a8e0d6943f
commit f312299e62
2 changed files with 13 additions and 15 deletions

View File

@ -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<f32>) {
// Wrap camera yaw

View File

@ -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::<comp::Pos>()
.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::<comp::Stats>().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))
{