use super::SolidStyle;
use glam::Vec3;
use indicatrix::optics::raytracer::Camera;
pub(super) fn blend_toward(base: [u8; 3], target: [u8; 3], amount: f32) -> [u8; 3] {
std::array::from_fn(|i| {
let b = f32::from(base[i]);
let t = f32::from(target[i]);
to_byte(amount.mul_add(t - b, b))
})
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the value is rounded and clamped to 0..=255 before the conversion"
)]
const fn to_byte(value: f32) -> u8 {
value.round().clamp(0.0, 255.0) as u8
}
pub(super) fn shade(
normal: Vec3,
world_point: Vec3,
camera: &Camera,
style: &SolidStyle,
base_color: [u8; 3],
) -> [u8; 3] {
let n = normal.normalize();
let light_dir = style.key_light_dir.normalize();
let view_dir = (camera.origin - world_point).normalize();
let n_dot_l = n.dot(light_dir).max(0.0);
let n_dot_v = n.dot(view_dir).max(0.0);
let rim = (1.0 - n_dot_v).powi(2) * style.rim_strength;
let intensity = style
.diffuse
.mul_add(n_dot_l, style.ambient + rim)
.clamp(0.0, 1.0);
[
to_byte(f32::from(base_color[0]) * intensity),
to_byte(f32::from(base_color[1]) * intensity),
to_byte(f32::from(base_color[2]) * intensity),
]
}