use valo_geometry::{Color, Matrix};
use crate::{MaskBlur, Paint};
const LIGHT_POSITION_Y: f32 = -1.0;
const LIGHT_RADIUS: f32 = (800 / 600) as f32;
const KERNEL_RADIUS_PER_SIGMA: f32 = 1.732_050_8;
#[derive(Clone, Debug, PartialEq)]
pub struct SpotShadow {
pub drop: f32,
pub paint: Paint,
}
impl SpotShadow {
pub fn resolve(
color: Color,
elevation: f32,
device_pixel_ratio: f32,
transform: &Matrix,
) -> Option<Self> {
let vertical_scale = vertical_scale(transform);
if !(vertical_scale > 0.0 && vertical_scale.is_finite()) {
return None;
}
let occluder_z = device_pixel_ratio * elevation;
let radius = LIGHT_RADIUS * occluder_z / vertical_scale;
let paint = Paint {
mask_blur: Some(MaskBlur::new(radius_to_sigma(radius))),
..Paint::from_color(tonal_spot_color(color))
};
Some(Self {
drop: -occluder_z * LIGHT_POSITION_Y,
paint,
})
}
pub fn local_drop(&self, transform: &Matrix) -> f32 {
self.drop / vertical_scale(transform)
}
}
fn vertical_scale(transform: &Matrix) -> f32 {
transform.to_mat4().y_axis.truncate().length()
}
fn radius_to_sigma(radius: f32) -> f32 {
if radius > 0.0 {
radius / KERNEL_RADIUS_PER_SIGMA + 0.5
} else {
0.0
}
}
fn tonal_spot_color(color: Color) -> Color {
let alpha = color.a * 0.25;
let max = color.r.max(color.g).max(color.b);
let min = color.r.min(color.g).min(color.b);
let luminance = (min + max) * 0.5;
let alpha_adjust = (2.6 + (-2.66667 + 1.06667 * alpha) * alpha) * alpha;
let color_alpha = (3.544762 + (-4.891428 + 2.3466 * luminance) * luminance) * luminance;
let color_alpha = (alpha_adjust * color_alpha).clamp(0.0, 1.0);
let greyscale_alpha = (alpha * (1.0 - 0.4 * luminance)).clamp(0.0, 1.0);
let color_scale = color_alpha * (1.0 - greyscale_alpha);
let tonal_alpha = color_scale + greyscale_alpha;
let unpremultiplied_scale = if tonal_alpha != 0.0 {
color_scale / tonal_alpha
} else {
0.0
};
Color {
r: unpremultiplied_scale * color.r,
g: unpremultiplied_scale * color.g,
b: unpremultiplied_scale * color.b,
a: tonal_alpha,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sigma(shadow: &SpotShadow) -> f32 {
shadow.paint.mask_blur.expect("a shadow is blurred").sigma
}
#[test]
fn a_shadow_falls_by_its_height_and_blurs_by_it_over_the_scale() {
let shadow = SpotShadow::resolve(Color::BLACK, 4.0, 2.0, &Matrix::scale(2.0, 2.0))
.expect("the transform keeps its y axis");
assert_eq!(shadow.drop, 8.0);
assert_eq!(shadow.local_drop(&Matrix::scale(2.0, 2.0)), 4.0);
assert!((sigma(&shadow) - 2.809_401).abs() < 1e-5, "{shadow:?}");
}
#[test]
fn a_scaled_transform_narrows_the_blur_in_local_coordinates() {
let at_ratio = SpotShadow::resolve(Color::BLACK, 4.0, 2.0, &Matrix::scale(2.0, 2.0));
let scaled = SpotShadow::resolve(Color::BLACK, 4.0, 2.0, &Matrix::scale(4.0, 4.0));
let radius = |shadow: Option<SpotShadow>| (sigma(&shadow.unwrap()) - 0.5) * 1.732_050_8;
assert!((radius(at_ratio) - 4.0).abs() < 1e-4);
assert!((radius(scaled) - 2.0).abs() < 1e-4);
}
#[test]
fn the_scale_is_the_y_axis_length_whatever_the_rotation() {
let turned = Matrix::rotation(0.7).then(&Matrix::scale(1.0, 3.0));
let shadow = SpotShadow::resolve(Color::BLACK, 3.0, 1.0, &turned).unwrap();
assert!(((sigma(&shadow) - 0.5) * 1.732_050_8 - 1.0).abs() < 1e-4);
}
#[test]
fn a_flattened_transform_shows_no_shadow() {
assert!(SpotShadow::resolve(Color::BLACK, 4.0, 1.0, &Matrix::scale(1.0, 0.0)).is_none());
}
#[test]
fn a_black_occluder_casts_a_quarter_alpha_black_spot() {
let spot = tonal_spot_color(Color::BLACK);
assert_eq!((spot.r, spot.g, spot.b), (0.0, 0.0, 0.0));
assert!((spot.a - 0.25).abs() < 1e-6, "{spot:?}");
}
#[test]
fn a_coloured_occluder_casts_a_tinted_spot() {
let spot = tonal_spot_color(Color::rgb(1.0, 0.0, 0.0));
let alpha_adjust = (2.6 + (-2.66667 + 1.06667 * 0.25) * 0.25) * 0.25;
let color_alpha = alpha_adjust * (3.544762 + (-4.891428 + 2.3466 * 0.5) * 0.5) * 0.5;
let color_scale = color_alpha * 0.8;
let tonal_alpha = color_scale + 0.2;
assert!((spot.a - tonal_alpha).abs() < 1e-6, "{spot:?}");
assert!(
(spot.r - color_scale / tonal_alpha).abs() < 1e-6,
"{spot:?}"
);
assert_eq!((spot.g, spot.b), (0.0, 0.0));
}
}