Added frustum culling for LoD objects

This commit is contained in:
Joshua Barretto 2022-05-10 16:21:14 +01:00
parent 19d19df63c
commit c7e51a6c59
3 changed files with 73 additions and 12 deletions

View File

@ -318,7 +318,7 @@ impl<'a> Widget for Video<'a> {
if let Some(new_val) = ImageSlider::discrete( if let Some(new_val) = ImageSlider::discrete(
self.global_state.settings.graphics.lod_distance, self.global_state.settings.graphics.lod_distance,
0, 0,
250, 500,
self.imgs.slider_indicator, self.imgs.slider_indicator,
self.imgs.slider, self.imgs.slider,
) )

View File

@ -4,6 +4,7 @@ use crate::{
FirstPassDrawer, Instances, LodObjectInstance, LodObjectVertex, LodTerrainVertex, Mesh, FirstPassDrawer, Instances, LodObjectInstance, LodObjectVertex, LodTerrainVertex, Mesh,
Model, Quad, Renderer, Tri, Model, Quad, Renderer, Tri,
}, },
scene::{camera, Camera},
settings::Settings, settings::Settings,
}; };
use client::Client; use client::Client;
@ -14,13 +15,26 @@ use common::{
util::srgba_to_linear, util::srgba_to_linear,
}; };
use hashbrown::HashMap; use hashbrown::HashMap;
use std::ops::Range;
use treeculler::{BVol, Frustum, AABB};
use vek::*; use vek::*;
// For culling
const MAX_OBJECT_RADIUS: i32 = 64;
struct ObjectGroup {
instances: Instances<LodObjectInstance>,
// None implies no instances
z_range: Option<Range<i32>>,
frustum_last_plane_index: u8,
visible: bool,
}
pub struct Lod { pub struct Lod {
model: Option<(u32, Model<LodTerrainVertex>)>, model: Option<(u32, Model<LodTerrainVertex>)>,
data: LodData, data: LodData,
zone_objects: HashMap<Vec2<i32>, HashMap<lod::ObjectKind, Instances<LodObjectInstance>>>, zone_objects: HashMap<Vec2<i32>, HashMap<lod::ObjectKind, ObjectGroup>>,
object_data: HashMap<lod::ObjectKind, Model<LodObjectVertex>>, object_data: HashMap<lod::ObjectKind, Model<LodObjectVertex>>,
} }
@ -62,7 +76,13 @@ impl Lod {
self.data.tgt_detail = (detail - detail % 2).max(100).min(2500); self.data.tgt_detail = (detail - detail % 2).max(100).min(2500);
} }
pub fn maintain(&mut self, renderer: &mut Renderer, client: &Client) { pub fn maintain(
&mut self,
renderer: &mut Renderer,
client: &Client,
focus_pos: Vec3<f32>,
camera: &Camera,
) {
// Update LoD terrain mesh according to detail // Update LoD terrain mesh according to detail
if self if self
.model .model
@ -78,14 +98,21 @@ impl Lod {
)); ));
} }
// Maintain LoD object instances // Create new LoD groups new a new zone has loaded
for (p, zone) in client.lod_zones() { for (p, zone) in client.lod_zones() {
self.zone_objects.entry(*p).or_insert_with(|| { self.zone_objects.entry(*p).or_insert_with(|| {
let mut objects = HashMap::<_, Vec<_>>::new(); let mut objects = HashMap::<_, Vec<_>>::new();
let mut z_range = None;
for object in zone.objects.iter() { for object in zone.objects.iter() {
let pos = p.map(|e| lod::to_wpos(e) as f32).with_z(0.0) let pos = p.map(|e| lod::to_wpos(e) as f32).with_z(0.0)
+ object.pos.map(|e| e as f32) + object.pos.map(|e| e as f32)
+ Vec2::broadcast(0.5).with_z(0.0); + Vec2::broadcast(0.5).with_z(0.0);
z_range = Some(z_range.map_or(
pos.z as i32..pos.z as i32,
|z_range: Range<i32>| {
z_range.start.min(pos.z as i32)..z_range.end.max(pos.z as i32)
},
));
objects objects
.entry(object.kind) .entry(object.kind)
.or_default() .or_default()
@ -94,19 +121,53 @@ impl Lod {
objects objects
.into_iter() .into_iter()
.map(|(kind, instances)| { .map(|(kind, instances)| {
( (kind, ObjectGroup {
kind, instances: renderer
renderer
.create_instances(&instances) .create_instances(&instances)
.expect("Renderer error?!"), .expect("Renderer error?!"),
) z_range: z_range.clone(),
frustum_last_plane_index: 0,
visible: false,
})
}) })
.collect() .collect()
}); });
} }
// Remove zones that are unloaded
self.zone_objects self.zone_objects
.retain(|p, _| client.lod_zones().contains_key(p)); .retain(|p, _| client.lod_zones().contains_key(p));
// Determine visiblity of zones based on view frustum
let camera::Dependents {
view_mat,
proj_mat_treeculler,
..
} = camera.dependents();
let focus_off = focus_pos.map(|e| e.trunc());
let frustum = Frustum::from_modelview_projection(
(proj_mat_treeculler * view_mat * Mat4::translation_3d(-focus_off)).into_col_arrays(),
);
for (pos, groups) in &mut self.zone_objects {
for group in groups.values_mut() {
if let Some(z_range) = &group.z_range {
let group_min = (pos.map(lod::to_wpos).with_z(z_range.start)
- MAX_OBJECT_RADIUS)
.map(|e| e as f32);
let group_max = ((pos + 1).map(lod::to_wpos).with_z(z_range.end)
+ MAX_OBJECT_RADIUS)
.map(|e| e as f32);
let (in_frustum, last_plane_index) =
AABB::new(group_min.into_array(), group_max.into_array())
.coherent_test_against_frustum(
&frustum,
group.frustum_last_plane_index,
);
group.visible = in_frustum;
group.frustum_last_plane_index = last_plane_index;
}
}
}
} }
pub fn render<'a>(&'a self, drawer: &mut FirstPassDrawer<'a>) { pub fn render<'a>(&'a self, drawer: &mut FirstPassDrawer<'a>) {
@ -116,10 +177,10 @@ impl Lod {
// Draw LoD objects // Draw LoD objects
let mut drawer = drawer.draw_lod_objects(); let mut drawer = drawer.draw_lod_objects();
for objects in self.zone_objects.values() { for groups in self.zone_objects.values() {
for (kind, instances) in objects { for (kind, group) in groups.iter().filter(|(_, g)| g.visible) {
if let Some(model) = self.object_data.get(kind) { if let Some(model) = self.object_data.get(kind) {
drawer.draw(model, instances); drawer.draw(model, &group.instances);
} }
} }
} }

View File

@ -681,7 +681,7 @@ impl Scene {
renderer.update_postprocess_locals(PostProcessLocals::new(proj_mat_inv, view_mat_inv)); renderer.update_postprocess_locals(PostProcessLocals::new(proj_mat_inv, view_mat_inv));
// Maintain LoD. // Maintain LoD.
self.lod.maintain(renderer, client); self.lod.maintain(renderer, client, focus_pos, &self.camera);
// Maintain debug shapes // Maintain debug shapes
self.debug.maintain(renderer); self.debug.maintain(renderer);