Files
horizon-airdef/crates/horizon-client/src/game/entity_culling.rs
T
socksprox ee0af20e16 Cap the view scale's render distance by the server's
streaming.radius reverts to the client setting in apply_settings, so
an options change on a server with a lower view distance raised every
entity's render range past vanilla's. min(render_distance,
remote.view_distance) is Options.getEffectiveRenderDistance.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-27 01:27:41 +01:00

138 lines
5.3 KiB
Rust

//! Entity occlusion affects mesh submission, never entity ticking or interaction.
use super::{Game, Vertex};
use horizon_client::chunk::MIN_Y;
use horizon_client::{
blocks::{RenderShape, StateFacts},
entity_culling::Culler,
};
fn opaque(facts: &StateFacts) -> bool {
facts.render_shape == RenderShape::Model
&& facts.can_occlude
&& facts.solid_render
&& facts.full_collision
}
impl Game<'_> {
pub(super) fn prepare_entity_culling(&mut self, camera: glam::DVec3) {
// Spans tell the pack's passes which entities are hidden; without them, nothing changes.
self.pack_shadow_casters = match (&self.renderer, &self.pack_ids) {
(Some(r), Some(_)) => r.pack_shadow_casters(),
_ => (false, false),
};
let at = camera.to_array().map(|v| v.floor() as i32);
self.entity_culler = (self.settings.horizon.entity_culling
// Packs can replace an opaque block's model or texture.
&& self.settings.resource_packs.iter().all(|id| id == "vanilla")
&& self.player.is_some()
&& !self.spectator()
&& !opaque(self.res.facts().get(self.world.block(at[0], at[1], at[2]))))
.then(|| Culler::new(camera.to_array(), 8_192));
}
/// Use complete generated geometry so equipment, animation and custom
/// models cannot stick out of an assumed collision-sized culling box.
pub(super) fn cull_entity_vertices(&self, vertices: &mut Vec<Vertex>, start: usize) {
let Some(culler) = &self.entity_culler else {
return;
};
let hidden = culler.hidden(vertices, start, |pos| {
let state = self.world.block(pos[0], pos[1], pos[2]);
if !opaque(self.res.facts().get(state)) {
return false;
}
let Some(y) = pos[1].checked_sub(MIN_Y) else {
return false;
};
let section = (pos[0] >> 4, y.div_euclid(16), pos[2] >> 4);
// Newly edited or not-yet-uploaded terrain is not an occluder.
self.renderer
.as_ref()
.is_some_and(|r| r.has_section(section))
&& !self.world.dirty_sections.contains(&section)
&& !self.streaming.is_meshing(section)
&& !self.pending_uploads.iter().any(|(key, _)| *key == section)
});
// A shader pack's shadow pass still draws what the camera cannot see.
if hidden && !(self.pack_shadow_casters.0 && self.hide_spans_from(start, vertices.len())) {
vertices.truncate(start);
}
}
}
impl Game<'_> {
/// Entity.setViewScale (LevelExtractor.extractVisibleEntities): the entity
/// distance option times the render distance's boost — 1 at 8 chunks, 2.5
/// at 20 or more. The render distance is the client setting, capped by
/// the server's view distance when connected
/// (Options.getEffectiveRenderDistance).
pub(super) fn view_scale(&self) -> f64 {
let distance = match &self.remote {
Some(r) => self.settings.render_distance.min(r.view_distance),
None => self.settings.render_distance,
};
(f64::from(distance) / 8.0).clamp(1.0, 2.5) * f64::from(self.settings.entity_distance)
}
}
/// Entity.shouldRenderAtSqrDistance's default range: 64 times the bounding
/// box's mean edge, scaled by the view scale.
pub(super) fn default_range(size: glam::DVec3, view_scale: f64) -> f64 {
let edge = (size.x + size.y + size.z) / 3.0;
let edge = if edge.is_nan() { 1.0 } else { edge };
edge * 64.0 * view_scale
}
impl Game<'_> {
/// EntityRenderer.shouldRender: the entity within `range` blocks of the
/// camera, measured from its current `position`, and its culling box
/// (`lo`..`hi`, interpolated, in world space) grown by 0.5 in the view.
pub(super) fn entity_in_view(
&self,
camera: glam::DVec3,
position: glam::DVec3,
range: f64,
lo: glam::DVec3,
hi: glam::DVec3,
) -> bool {
if (position - camera).length_squared() >= range * range {
return false;
}
self.view_frustum.as_ref().is_none_or(|view| {
view.intersects(
(lo - camera - glam::DVec3::splat(0.5)).as_vec3(),
(hi - camera + glam::DVec3::splat(0.5)).as_vec3(),
)
})
}
}
/// An entity's position as a `DVec3`.
pub(super) fn at(p: horizon_core::geometry::Vec3) -> glam::DVec3 {
glam::DVec3::new(p.x, p.y, p.z)
}
impl Game<'_> {
/// `entity_in_view` for an entity whose box stands on its position,
/// `width` wide and `height` tall, interpolated by `alpha` and grown by
/// `grow` (a renderer's getBoundingBoxForCulling).
#[allow(clippy::too_many_arguments)]
pub(super) fn standing_entity_in_view(
&self,
camera: glam::DVec3,
previous: glam::DVec3,
position: glam::DVec3,
alpha: f64,
width: f64,
height: f64,
range: f64,
grow: f64,
) -> bool {
let p = previous.lerp(position, alpha);
let half = glam::DVec3::new(width / 2.0 + grow, grow, width / 2.0 + grow);
let lo = p - half;
let hi = p + glam::DVec3::new(half.x, height + grow, half.z);
self.entity_in_view(camera, position, range, lo, hi)
}
}