use super::culling::clip_intersects;
use super::lod::LodSelector;
use super::pipelines::{Alpha, Cull, Variant};
use crate::content::identity::Identity;
use crate::content::material::SurfaceMaterial;
use crate::scene::instances::Instance;
use crate::scene::materials::Material;
use crate::scene::static_edits::{posed_bounds, posed_grown};
use crate::{Mobility, Scene};
use glam::{Mat4, Vec3};
pub(crate) enum Population<'a> {
Blended {
lod: Option<LodSelector>,
cull: bool,
},
ProbeFace,
CaptureShadow,
LocalShadow {
position: Vec3,
range: f32,
casters: Casters,
reach: &'a LightReach,
},
}
#[derive(Default)]
pub(crate) struct LightReach {
groups: Vec<(u32, u32, u32)>,
}
pub(crate) fn within(bounds: [Vec3; 2], (position, range): (Vec3, f32)) -> bool {
position
.clamp(bounds[0], bounds[1])
.distance_squared(position)
<= range * range
}
impl LightReach {
pub fn find(&mut self, scene: &Scene, light: (Vec3, f32)) {
self.groups.clear();
for (id, instance) in scene.instances.slots.iter() {
if instance.mobility != Mobility::Static || !instance.state.capture_visible {
continue;
}
let model = scene.drawn_model(instance.state.model);
for (mesh, posed) in instance.casters.iter().enumerate() {
let grow = scene
.drawn_material(model.meshes[mesh].material)
.displacement_bound();
for (group, &bounds) in posed.groups.iter().enumerate() {
if within(posed_grown(bounds, instance.state.pose, grow), light) {
self.groups
.push((id.index() as u32, mesh as u32, group as u32));
}
}
}
}
}
pub(super) fn of(&self, index: usize) -> impl Iterator<Item = (usize, usize)> + '_ {
let index = index as u32;
let start = self
.groups
.partition_point(|&(instance, _, _)| instance < index);
self.groups[start..]
.iter()
.take_while(move |&&(instance, _, _)| instance == index)
.map(|&(_, mesh, group)| (mesh as usize, group as usize))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Casters {
Static,
Moving,
All,
}
impl Casters {
fn holds(self, mobility: Mobility) -> bool {
match self {
Self::Static => mobility == Mobility::Static,
Self::Moving => mobility == Mobility::Moving,
Self::All => true,
}
}
}
pub(crate) fn moving_caster_reaches(
bounds: [Vec3; 2],
pose: Mat4,
view_projection: Mat4,
(position, range): (Vec3, f32),
) -> bool {
within(posed_bounds(bounds, pose), (position, range))
&& clip_intersects(bounds, view_projection * pose)
}
pub(crate) fn camera_variant(
material: &SurfaceMaterial,
mirrored: bool,
deformed: bool,
) -> Variant {
Variant {
cull: Cull::of(
Cull::Back,
material.double_sided && !material.volume(),
mirrored,
),
alpha: Alpha::of(material),
deformed,
}
}
impl Population<'_> {
pub(super) fn shows(&self, instance: &Instance) -> bool {
let state = &instance.state;
match self {
Self::Blended { .. } => state.visible,
Self::ProbeFace | Self::CaptureShadow => {
state.capture_visible && instance.mobility == Mobility::Static
}
Self::LocalShadow { casters, .. } => {
state.capture_visible && casters.holds(instance.mobility)
}
}
}
pub(super) fn variant(
&self,
material: &SurfaceMaterial,
mirrored: bool,
deformed: bool,
) -> Variant {
match self {
Self::ProbeFace => Variant {
cull: Cull::None,
alpha: Alpha::of(material),
deformed,
},
Self::Blended { .. } | Self::CaptureShadow | Self::LocalShadow { .. } => {
camera_variant(material, mirrored, deformed)
}
}
}
pub(super) fn draws(&self, material: &Material, mask: Option<u32>) -> bool {
if let Self::LocalShadow { .. } = self {
return material.casts_local_shadow(mask);
}
if material.values.blended() != matches!(self, Self::Blended { .. }) {
return false;
}
match self {
Self::CaptureShadow => material.casts_directional_shadow(mask.unwrap_or(0)),
_ => material.enabled(mask),
}
}
}