use crate::color::Color;
use crate::geometry::{Rect, Vec2, lerp, vec2};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Corner {
Radius(f32),
Capsule,
Stretched {
base: Vec2,
power: f32,
},
Circular(f32),
}
impl From<f32> for Corner {
fn from(radius: f32) -> Self {
Corner::Radius(radius)
}
}
impl Corner {
pub fn resolve(self, size: Vec2) -> (Vec2, f32) {
let half = Vec2::splat(size.x.min(size.y) * 0.5);
match self {
Corner::Capsule => {
let mut r = half * 1.16;
r = r.min(size * 0.5);
let k = ((size.x.max(size.y) / size.x.min(size.y).max(0.1)) - 1.0).clamp(0.0, 0.3) / 0.3;
(half.lerp(r, k), lerp(2.0, 2.18, k))
}
Corner::Stretched { base, power } => {
let (r, _) = Corner::Capsule.resolve(base);
(vec2(r.x * size.x / base.x.max(0.1), r.y * size.y / base.y.max(0.1)), power)
}
Corner::Radius(r) => {
let want = r * 1.35;
let room = (half.x - r).max(0.0) / (want - r).max(0.01);
let k = room.clamp(0.0, 1.0);
(Vec2::splat(lerp(r.min(half.x), want, k).max(0.01)), lerp(2.0, 2.83, k))
}
Corner::Circular(r) => (Vec2::splat(r.min(half.x)), 2.0),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Material {
pub refraction: f32,
pub depth: f32,
pub outer_refraction: f32,
pub outer_depth: f32,
pub pinch: f32,
pub pinch_width: f32,
pub sideways: f32,
pub dispersion: f32,
pub corner_dispersion: f32,
pub blur: f32,
pub magnify: f32,
pub light_range: (f32, f32),
pub dark_range: (f32, f32),
pub saturation: f32,
pub rim: f32,
pub rim_width: f32,
pub rim_base: f32,
pub dark_edge: f32,
pub light_spill: f32,
pub inner_glow: f32,
pub inner_shadow: f32,
pub shadow: f32,
pub shadow_radius: f32,
pub shadow_y: f32,
pub shadow_tint: f32,
pub tint: Option<Color>,
pub tint_follow: f32,
pub glass_tint_response: f32,
}
impl Material {
pub const REGULAR: Material = Material {
refraction: 17.0,
depth: 13.0,
outer_refraction: 3.0,
outer_depth: 4.0,
pinch: 0.0,
pinch_width: 20.0,
sideways: 0.0,
dispersion: 0.10,
corner_dispersion: 0.0,
blur: 4.2,
magnify: 1.0,
light_range: (0.62, 1.0),
dark_range: (0.06, 0.52),
saturation: 1.45,
rim: 0.75,
rim_width: 0.8,
rim_base: 0.34,
dark_edge: 0.22,
light_spill: 0.18,
inner_glow: 0.16,
inner_shadow: 0.05,
shadow: 0.16,
shadow_radius: 16.0,
shadow_y: 6.0,
shadow_tint: 0.35,
tint: None,
tint_follow: 1.0,
glass_tint_response: 1.0,
};
pub const CLEAR: Material = Material {
refraction: 24.0,
depth: 18.0,
outer_refraction: 4.0,
outer_depth: 4.0,
dispersion: 0.20,
blur: 0.5,
light_range: (0.10, 1.0),
dark_range: (0.04, 0.92),
saturation: 1.25,
rim: 0.85,
inner_glow: 0.12,
inner_shadow: 0.07,
..Material::REGULAR
};
pub const THICK: Material = Material {
refraction: 34.0,
depth: 26.0,
outer_refraction: 4.0,
outer_depth: 5.0,
dispersion: 0.10,
blur: 11.0,
light_range: (0.68, 1.0),
dark_range: (0.09, 0.44),
saturation: 1.6,
shadow: 0.26,
shadow_radius: 34.0,
shadow_y: 14.0,
light_spill: 0.26,
inner_glow: 0.10,
..Material::REGULAR
};
pub const LENS: Material = Material {
refraction: 20.0,
depth: 9.4,
outer_refraction: 6.0,
outer_depth: 1.6,
dispersion: 0.17,
blur: 0.0,
light_range: (0.15, 1.0),
dark_range: (0.06, 1.0),
saturation: 1.06,
rim: 1.0,
rim_width: 0.75,
rim_base: 0.06,
dark_edge: 0.92,
light_spill: 0.0,
inner_glow: 0.20,
inner_shadow: 0.09,
shadow: 0.17,
shadow_radius: 9.0,
shadow_y: 5.0,
shadow_tint: 0.95,
glass_tint_response: 0.0,
..Material::REGULAR
};
pub const BLOB: Material = Material {
refraction: 20.0,
depth: 3.5,
outer_refraction: 0.0,
outer_depth: 2.0,
pinch: 4.1,
pinch_width: 20.0,
dispersion: 0.75,
corner_dispersion: 1.0,
light_range: (0.0, 1.0),
dark_range: (0.0, 1.0),
saturation: 1.05,
rim: 0.85,
rim_width: 0.6,
rim_base: 0.62,
dark_edge: 0.0,
inner_glow: 0.05,
inner_shadow: 0.0,
shadow: 0.13,
shadow_radius: 11.0,
shadow_y: 5.0,
shadow_tint: 0.2,
..Material::LENS
};
pub const BLOB_LABELS: Material = Material {
refraction: 23.0,
depth: 16.0,
outer_refraction: 0.0,
pinch: 0.0,
sideways: 0.5,
dispersion: 0.18,
corner_dispersion: 0.0,
saturation: 1.0,
inner_glow: 0.0,
shadow: 0.0,
..Material::BLOB
};
pub fn blur(radius: f32) -> Material {
Material {
refraction: 0.0,
depth: 0.1,
outer_refraction: 0.0,
outer_depth: 0.1,
pinch: 0.0,
dispersion: 0.0,
corner_dispersion: 0.0,
blur: radius,
magnify: 1.0,
light_range: (0.0, 1.0),
dark_range: (0.0, 1.0),
saturation: 1.0,
rim: 0.0,
rim_base: 0.0,
dark_edge: 0.0,
light_spill: 0.0,
inner_glow: 0.0,
inner_shadow: 0.0,
shadow: 0.0,
shadow_radius: 0.0,
shadow_y: 0.0,
shadow_tint: 0.0,
tint: None,
glass_tint_response: 0.0,
..Material::REGULAR
}
}
pub fn tinted(mut self, color: Color) -> Self {
self.tint = Some(color);
self
}
pub fn scaled(mut self, k: f32) -> Material {
self.refraction *= k;
self.depth *= k;
self.outer_refraction *= k;
self.outer_depth *= k;
self.pinch *= k;
self.pinch_width *= k;
self.rim_width *= k.max(1.0).sqrt();
self.shadow_radius *= k;
self.shadow_y *= k;
self
}
pub fn lerp(&self, other: &Material, t: f32) -> Material {
let o = other;
let l = |a: f32, b: f32| lerp(a, b, t);
let l2 = |a: (f32, f32), b: (f32, f32)| (l(a.0, b.0), l(a.1, b.1));
Material {
refraction: l(self.refraction, o.refraction),
depth: l(self.depth, o.depth),
outer_refraction: l(self.outer_refraction, o.outer_refraction),
outer_depth: l(self.outer_depth, o.outer_depth),
pinch: l(self.pinch, o.pinch),
pinch_width: l(self.pinch_width, o.pinch_width),
sideways: l(self.sideways, o.sideways),
dispersion: l(self.dispersion, o.dispersion),
corner_dispersion: l(self.corner_dispersion, o.corner_dispersion),
blur: l(self.blur, o.blur),
magnify: l(self.magnify, o.magnify),
light_range: l2(self.light_range, o.light_range),
dark_range: l2(self.dark_range, o.dark_range),
saturation: l(self.saturation, o.saturation),
rim: l(self.rim, o.rim),
rim_width: l(self.rim_width, o.rim_width),
rim_base: l(self.rim_base, o.rim_base),
dark_edge: l(self.dark_edge, o.dark_edge),
light_spill: l(self.light_spill, o.light_spill),
inner_glow: l(self.inner_glow, o.inner_glow),
inner_shadow: l(self.inner_shadow, o.inner_shadow),
shadow: l(self.shadow, o.shadow),
shadow_radius: l(self.shadow_radius, o.shadow_radius),
shadow_y: l(self.shadow_y, o.shadow_y),
shadow_tint: l(self.shadow_tint, o.shadow_tint),
tint: match (self.tint, o.tint) {
(None, None) => None,
(Some(a), Some(b)) => Some(a.lerp(b, t)),
(Some(a), None) => Some(a.fade(1.0 - t)),
(None, Some(b)) => Some(b.fade(t)),
},
tint_follow: l(self.tint_follow, o.tint_follow),
glass_tint_response: l(self.glass_tint_response, o.glass_tint_response),
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct GlassShape {
pub rect: Rect,
pub corner: Corner,
pub tint: Option<Color>,
}
impl GlassShape {
pub fn new(rect: Rect, corner: impl Into<Corner>) -> Self {
Self { rect, corner: corner.into(), tint: None }
}
pub fn tint(mut self, color: Option<Color>) -> Self {
self.tint = color;
self
}
}
#[derive(Clone, Debug)]
pub struct Glass {
pub shapes: Vec<GlassShape>,
pub material: Material,
pub fuse: f32,
pub presence: f32,
pub darkness: f32,
pub shadow_boost: f32,
pub brightness: f32,
pub light_bend: f32,
pub(crate) stat_key: Option<u64>,
}
impl Glass {
pub fn new(rect: Rect, corner: impl Into<Corner>, material: Material) -> Self {
Self::group(vec![GlassShape::new(rect, corner)], material)
}
pub fn group(shapes: Vec<GlassShape>, material: Material) -> Self {
Self {
shapes,
material,
fuse: 12.0,
presence: 1.0,
darkness: 0.0,
shadow_boost: 0.0,
brightness: 0.0,
light_bend: 0.0,
stat_key: None,
}
}
pub fn presence(mut self, presence: f32) -> Self {
self.presence = presence.clamp(0.0, 1.0);
self
}
pub fn fuse(mut self, distance: f32) -> Self {
self.fuse = distance;
self
}
pub fn darkness(mut self, darkness: f32) -> Self {
self.darkness = darkness.clamp(0.0, 1.0);
self
}
pub fn bounds(&self) -> Rect {
self.shapes.iter().fold(Rect::NOTHING, |a, s| a.union(s.rect))
}
}