Skip to content

Commit

Permalink
fix tree culling
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Dec 15, 2023
1 parent c7b5b12 commit 0c146de
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions geom/src/aabb3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ impl AABB3 {
Self { ll, ur }
}

#[inline]
pub fn new_size(ll: Vec3, size: Vec3) -> Self {
Self { ll, ur: ll + size }
}

#[inline]
pub fn centered(pos: Vec3, size: Vec3) -> Self {
Self {
Expand Down
5 changes: 5 additions & 0 deletions geom/src/heightmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ impl<const RESOLUTION: usize, const SIZE: u32> HeightmapChunk<RESOLUTION, SIZE>
pub fn heights(&self) -> &[[f32; RESOLUTION]; RESOLUTION] {
&self.heights
}

#[inline]
pub fn max_height(&self) -> f32 {
self.max_height
}
}

#[derive(Clone, Serialize, Deserialize)]
Expand Down
18 changes: 13 additions & 5 deletions native_app/src/rendering/map_rendering/trees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use engine::wgpu::RenderPass;
use engine::{
Drawable, FrameContext, GfxContext, InstancedMesh, InstancedMeshBuilder, MeshInstance,
};
use geom::{vec3, vec4, Camera, InfiniteFrustrum, Intersect3, LinearColor, Matrix4, Vec3, AABB3};
use geom::{
vec3, vec4, Camera, HeightmapChunk, InfiniteFrustrum, Intersect3, LinearColor, Matrix4, Vec3,
AABB3,
};
use simulation::config;
use simulation::map::{Map, MapSubscriber, SubscriberChunkID, UpdateType};
use std::ops::Mul;
Expand Down Expand Up @@ -104,13 +107,18 @@ impl TreesRender {

for (cid, mesh) in self.trees_cache.iter() {
let chunkcenter = cid.center().z0();

if !frustrum.intersects(&AABB3::centered(
chunkcenter,
let max_height = cid
.convert()
.filter_map(|c| map.terrain.get_chunk(c))
.map(HeightmapChunk::max_height)
.fold(0.0, f32::max);

if !frustrum.intersects(&AABB3::new_size(
cid.corner().z(-40.0),
vec3(
5.0 + SubscriberChunkID::SIZE_F32,
5.0 + SubscriberChunkID::SIZE_F32,
100.0,
40.0 + max_height + 100.0,
),
)) || camcenter.distance(chunkcenter.xy()) > 5000.0
{
Expand Down

0 comments on commit 0c146de

Please sign in to comment.