use rand::Rng;
use sciart_core::timeline::ease;
pub const SKY_SUB: usize = 4;
#[derive(Debug, Clone, Copy)]
pub struct Terrain {
pub ridge: (f64, f64),
pub trees: Option<(f64, f64)>,
pub gap: (f64, f64),
pub clearing: f64,
}
#[cfg(any(test, feature = "test-support"))]
pub const FOREST: Terrain = Terrain {
ridge: (0.6, 2.2),
trees: Some((1.4, 4.2)),
gap: (0.35, 1.6),
clearing: 0.2,
};
const TIERS: f64 = 5.0;
const CROWN: f64 = 0.27;
#[derive(Debug, Clone, Default)]
pub struct Skyline {
sub: Vec<f32>,
}
#[derive(Debug, Clone, Copy)]
struct Spire {
at: f64,
height: f64,
half: f64,
}
impl Spire {
fn crown(&self, off: f64) -> f64 {
let off = off.abs();
if off >= self.half {
return 0.0;
}
const STEPS: usize = 40;
for i in 0..=STEPS {
let v = 1.0 - i as f64 / STEPS as f64;
let frac = (v * TIERS).fract();
if self.half * (1.0 - v) * (0.72 + 0.28 * frac) >= off {
return self.height * v;
}
}
0.0
}
}
impl Skyline {
pub fn new(rng: &mut impl Rng, width: usize, px_per_deg: f64, terrain: Terrain) -> Self {
let px_per_deg = if px_per_deg.is_finite() && px_per_deg > 0.0 {
px_per_deg
} else {
1.0
};
let span = width as f64 / px_per_deg;
let coarse: Vec<f64> = (0..(span / 9.0) as usize + 3)
.map(|_| rng.random_range(0.0..1.0))
.collect();
let fine: Vec<f64> = (0..(span / 2.5) as usize + 3)
.map(|_| rng.random_range(0.0..1.0))
.collect();
let noise = |nodes: &[f64], x: f64| {
let i = (x as usize).min(nodes.len() - 2);
ease::lerp(nodes[i], nodes[i + 1], ease::smoothstep(x - i as f64))
};
let mut spires = Vec::new();
if let Some((lo, hi)) = terrain.trees {
let mut at = rng.random_range(-1.0..0.5);
let mut clearing = 0.0;
while at < span + 2.0 {
if clearing > 0.0 {
clearing -= 1.0;
} else if rng.random_bool(terrain.clearing.clamp(0.0, 1.0) / 6.0) {
clearing = f64::from(rng.random_range(2u8..7));
} else {
let u: f64 = rng.random_range(0.0..1.0);
let height = lo + (hi - lo) * u * u;
spires.push(Spire {
at,
height,
half: CROWN * height * rng.random_range(0.8..1.25),
});
}
at += rng.random_range(terrain.gap.0..terrain.gap.1);
}
}
let sub = (0..width * SKY_SUB)
.map(|i| {
let deg = (i as f64 + 0.5) / (SKY_SUB as f64 * px_per_deg);
let ridge = terrain.ridge.0
+ terrain.ridge.1
* (0.7 * noise(&coarse, deg / 9.0) + 0.3 * noise(&fine, deg / 2.5));
let trees = spires
.iter()
.filter(|s| (s.at - deg).abs() < s.half)
.map(|s| s.crown(deg - s.at))
.fold(0.0, f64::max);
(px_per_deg * ridge.max(0.6 * terrain.ridge.0 + trees + 0.4 * ridge)) as f32
})
.collect();
Self { sub }
}
pub fn height_at(&self, x: f64) -> f64 {
if self.sub.is_empty() || !x.is_finite() {
return 0.0;
}
let i = ((x * SKY_SUB as f64).max(0.0) as usize).min(self.sub.len() - 1);
f64::from(self.sub[i])
}
pub fn cover(&self, x: i32, y: i32, base: f64) -> f32 {
let Ok(x) = usize::try_from(x) else {
return 0.0;
};
let Some(col) = self.sub.get(x * SKY_SUB..(x + 1) * SKY_SUB) else {
return 0.0;
};
let bottom = y as f32 + 1.0;
let sum: f32 = col
.iter()
.map(|&h| (bottom - (base as f32 - h)).clamp(0.0, 1.0))
.sum();
sum / SKY_SUB as f32
}
}
#[cfg(test)]
mod tests {
use super::*;
use sciart_core::SplitMix64;
#[test]
fn skyline_cover_is_soft_and_complete_below_the_horizon() {
let mut rng = SplitMix64::new(9);
let land = Skyline::new(&mut rng, 100, 2.0, FOREST);
for x in 0..100 {
assert_eq!(land.cover(x, 60, 50.0), 1.0);
assert_eq!(land.cover(x, 20, 50.0), 0.0);
}
let partial = (0..100)
.flat_map(|x| (30..50).map(move |y| (x, y)))
.filter(|&(x, y)| {
let c = land.cover(x, y, 50.0);
c > 0.05 && c < 0.95
})
.count();
assert!(partial > 30, "{partial}");
}
}