diff --git a/geom/src/aabb3.rs b/geom/src/aabb3.rs index 60bd252c..c3085483 100644 --- a/geom/src/aabb3.rs +++ b/geom/src/aabb3.rs @@ -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 { diff --git a/geom/src/heightmap.rs b/geom/src/heightmap.rs index ba9a27c5..0896fb7b 100644 --- a/geom/src/heightmap.rs +++ b/geom/src/heightmap.rs @@ -84,6 +84,11 @@ impl HeightmapChunk pub fn heights(&self) -> &[[f32; RESOLUTION]; RESOLUTION] { &self.heights } + + #[inline] + pub fn max_height(&self) -> f32 { + self.max_height + } } #[derive(Clone, Serialize, Deserialize)] diff --git a/native_app/src/rendering/map_rendering/trees.rs b/native_app/src/rendering/map_rendering/trees.rs index 68335bb6..5c38a5e7 100644 --- a/native_app/src/rendering/map_rendering/trees.rs +++ b/native_app/src/rendering/map_rendering/trees.rs @@ -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; @@ -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 {