use glam::Mat4;
use crate::bounds::Aabb;
slotmap::new_key_type! {
pub struct DecalKey;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u32)]
pub enum DecalBlendMode {
#[default]
AlphaBlend = 0,
}
#[derive(Debug, Clone)]
pub struct Decal {
pub transform: Mat4,
pub inverse_transform: Mat4,
pub texture_index: u32,
pub alpha: f32,
pub blend_mode: DecalBlendMode,
pub world_aabb: Aabb,
}
impl Decal {
pub fn new(transform: Mat4, texture_index: u32, alpha: f32) -> Self {
let inverse_transform = transform.inverse();
let world_aabb = aabb_of_unit_cube(&transform);
Self {
transform,
inverse_transform,
texture_index,
alpha,
blend_mode: DecalBlendMode::AlphaBlend,
world_aabb,
}
}
}
fn aabb_of_unit_cube(m: &Mat4) -> Aabb {
let mut min = glam::Vec3::splat(f32::INFINITY);
let mut max = glam::Vec3::splat(f32::NEG_INFINITY);
let signs: [f32; 2] = [-1.0, 1.0];
for &sx in &signs {
for &sy in &signs {
for &sz in &signs {
let corner = m.transform_point3(glam::Vec3::new(sx, sy, sz));
min = min.min(corner);
max = max.max(corner);
}
}
}
Aabb { min, max }
}