mgecumshooter/esotera
added chunk culling
Commit b2b90cec06a4 · mgecumshooter · 2026-10-21 14:43 +0300
Comments
No comments yet.
Diff
diff --git a/src/chunk/mod.rs b/src/chunk/mod.rs
--- a/src/chunk/mod.rs
+++ b/src/chunk/mod.rs
@@ -43,6 +43,41 @@
s
}
+ pub fn is_visible(&self, cam: &Camera3D) -> bool {
+ let aabb = self.get_chunk_aabb();
+ let center = Vector3::new(
+ (aabb.min.x + aabb.max.x) *0.5f32,
+ (aabb.min.y + aabb.max.y) *0.5f32,
+ (aabb.min.z + aabb.max.z) *0.5f32,
+ );
+
+ let dx = aabb.max.x - aabb.min.x;
+ let dy = aabb.max.y - aabb.min.y;
+ let dz = aabb.max.z - aabb.min.z;
+
+ let radius = (dx*dx + dy*dy + dz*dz).sqrt() * 0.5;
+
+
+ let ddx = center.x - cam.position.x;
+ let ddy = center.y - cam.position.y;
+ let ddz = center.z - cam.position.z;
+
+ let dist = (ddx*ddx + ddy*ddy + ddz*ddz).sqrt();
+
+ let fx = cam.target.x - cam.position.x;
+ let fy = cam.target.y - cam.position.y;
+ let fz = cam.target.z - cam.position.z;
+
+ let flen = (fx*fx + fy*fy + fz*fz).sqrt();
+
+ let cos_angle = (ddx*fx + ddy*fy + ddz*fz) / (dist*flen);
+
+ let half_fov = (cam.fovy.to_radians() / 2f32) * 1f32;
+ let angular_margin = (radius / dist).atan();
+
+ cos_angle.acos() <= half_fov + angular_margin
+ }
+
pub fn generate_mesh(&mut self, rl: &RaylibThread) {
let mut vertices: Vec<Vector3> = Vec::new();
let mut normals: Vec<Vector3> = Vec::new();
diff --git a/src/main.rs b/src/main.rs
--- a/src/main.rs
+++ b/src/main.rs
@@ -69,6 +69,7 @@
plr.update(&rl, &active_chunks);
plr.apply_to_camera(&mut cam);
+ let fps = rl.get_fps();
if rend_tex.width() != rl.get_render_width() || rend_tex.height() != rl.get_render_height() {
rend_tex = unsafe { load_depth_tex(rl.get_render_width(), rl.get_render_height()) };
@@ -82,6 +83,9 @@
tx.draw_mode3D(cam, |mut d3, _| {
d3.draw_grid(100, 1f32);
for chunk in world.chunks.values() {
+ if !chunk.is_visible(&cam) {
+ continue;
+ }
if let Some(ref mesh) = chunk.mesh {
d3.draw_mesh(mesh,
chunk_material.clone(),
@@ -126,6 +130,10 @@
20,
Color::WHITE,
);
+ d.draw_text(
+ &format!("FPS: {}", fps),
+ 0, 20, 20, Color::WHITE,
+ );
});
}
}