use bevy::{
prelude::*,
render::{primitives::Aabb, render_resource::ShaderType},
};
#[derive(Clone, Copy, Debug, ShaderType)]
#[repr(C)]
pub struct Cuboid {
pub minimum: Vec3,
pub meta_bits: u32,
pub maximum: Vec3,
pub color_rgba: u32,
}
impl Cuboid {
pub fn new(
minimum: Vec3,
maximum: Vec3,
color_rgba: u32,
visible: bool,
depth_bias: u8,
) -> Self {
assert_eq!(std::mem::size_of::<Cuboid>(), 32);
let mut meta_bits = (!visible) as u32;
meta_bits |= (depth_bias as u32) << 8;
Self {
minimum,
meta_bits,
maximum,
color_rgba,
}
}
}
#[derive(Clone, Component, Debug, Default, ShaderType)]
pub struct Cuboids {
#[size(runtime)]
pub instances: Vec<Cuboid>,
}
impl Cuboids {
pub fn new(instances: Vec<Cuboid>) -> Self {
Self { instances }
}
pub fn aabb(&self) -> Aabb {
let mut min = Vec3::splat(f32::MAX);
let mut max = Vec3::splat(f32::MIN);
for i in self.instances.iter() {
min = min.min(i.minimum);
max = max.max(i.maximum);
}
Aabb::from_min_max(min, max)
}
}
#[derive(Clone, Component, Default, ShaderType)]
pub struct ClippingPlaneRange {
pub min_sdist: f32,
pub max_sdist: f32,
}
#[derive(Bundle)]
pub struct ClippingPlaneBundle {
pub global_transform: GlobalTransform,
pub range: ClippingPlaneRange,
pub transform: Transform,
}