use crate::core::{Mesh, Object3D, ObjectId, ObjectKind};
use crate::math::Color;
use crate::scene::Scene;
use super::{Heightfield, ParticleSystem, Rng};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Downwind {
PlusX,
MinusX,
PlusZ,
MinusZ,
}
impl Downwind {
pub(crate) fn step(self) -> (i64, i64) {
match self {
Downwind::PlusX => (1, 0),
Downwind::MinusX => (-1, 0),
Downwind::PlusZ => (0, 1),
Downwind::MinusZ => (0, -1),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Saltation {
pub slab: f32,
pub hop: usize,
pub on_sand: f32,
pub on_bare: f32,
pub shadow: f32,
}
impl Default for Saltation {
fn default() -> Self {
Self {
slab: 0.05,
hop: 3,
on_sand: 0.6,
on_bare: 0.4,
shadow: 15f32.to_radians(),
}
}
}
#[derive(Debug, Clone)]
pub struct SandBed {
pub field: Heightfield,
pub angle_of_repose: f32,
pub sweeps: usize,
pub saltation: Saltation,
pub color: Color,
pub ripple_wind: crate::math::Vector3,
pub ripples: f32,
pub stylize: f32,
pub shadow_color: Color,
pub streaks: f32,
pub fog_glow: f32,
pub ripple_wavelength: f32,
rng: Rng,
time: f32,
object: Option<ObjectId>,
drawn_version: u64,
}
impl SandBed {
pub fn new(field: Heightfield) -> Self {
Self {
field,
angle_of_repose: 34f32.to_radians(),
sweeps: 24,
saltation: Saltation::default(),
color: Color::from_hex(0xd8b98a),
ripple_wind: crate::math::Vector3::new(1.0, 0.0, 0.0),
ripples: 0.7,
stylize: 0.0,
shadow_color: Color::new(0.34, 0.22, 0.13),
streaks: 0.0,
fog_glow: 0.0,
ripple_wavelength: 0.08,
rng: Rng::new(0x5a2d),
time: 0.0,
object: None,
drawn_version: u64::MAX,
}
}
pub fn spread(&mut self, depth: f32, roughness: f32) {
for d in &mut self.field.depth {
*d = (depth + (self.rng.next() * 2.0 - 1.0) * roughness).max(0.0);
}
self.field.touch_all();
self.field.version += 1;
}
pub fn absorb(&mut self, grains: &mut ParticleSystem) {
self.take_up(grains);
self.settle();
}
pub fn take_up(&mut self, grains: &mut ParticleSystem) {
for l in grains.take_landed() {
self.field.deposit(l.position.x, l.position.z, l.volume);
}
}
pub fn settle(&mut self) {
self.field.relax(self.angle_of_repose, self.sweeps);
}
pub fn settle_within(&mut self, budget: usize) {
self.field
.relax_within(self.angle_of_repose, self.sweeps, budget);
}
pub fn blow(&mut self, downwind: Downwind, slabs: usize) {
self.carry(downwind, slabs);
self.settle();
}
pub(crate) fn carry(&mut self, downwind: Downwind, slabs: usize) {
self.field
.saltate(downwind, slabs, &self.saltation, &mut self.rng, true);
}
pub fn object(&self) -> Object3D {
let mut look = super::surface::SurfaceLook::sand(self.color);
look.wind = self.ripple_wind;
look.ripple_strength = self.ripples;
look.stylize = self.stylize;
look.shadow_color = self.shadow_color;
look.streaks = self.streaks;
look.fog_glow = self.fog_glow;
look.ripple_wavelength = self.ripple_wavelength;
let mut o = Object3D::mesh(Mesh::new(self.field.mesh(), look.material()));
o.cast_shadow = true;
o.receive_shadow = true;
o.name = "sand".into();
o
}
pub fn bind(&mut self, object: ObjectId) {
self.object = Some(object);
self.drawn_version = u64::MAX;
}
pub fn add_to(&mut self, scene: &mut Scene) -> ObjectId {
let id = scene.add(self.object());
self.object = Some(id);
self.drawn_version = self.field.version;
id
}
pub fn restyle(&self, scene: &mut Scene) {
let Some(id) = self.object else { return };
let material = self.object().kind;
if let (Some(object), ObjectKind::Mesh(fresh)) = (scene.get_mut(id), material)
&& let ObjectKind::Mesh(mesh) = &mut object.kind
{
mesh.material = fresh.material;
}
}
pub fn advance(&mut self, scene: &mut Scene, dt: f32) {
if self.streaks <= 0.0 {
return;
}
self.time += dt;
let Some(id) = self.object else { return };
if let Some(object) = scene.get_mut(id)
&& let ObjectKind::Mesh(mesh) = &mut object.kind
&& let Some(m) = super::surface::with_time(&mesh.material, self.time)
{
mesh.material = std::sync::Arc::new(m);
}
}
pub fn sync(&mut self, scene: &mut Scene) {
let Some(id) = self.object else { return };
if self.drawn_version == self.field.version {
return;
}
if let Some(object) = scene.get_mut(id)
&& let ObjectKind::Mesh(mesh) = &mut object.kind
{
mesh.geometry = std::sync::Arc::new(self.field.mesh());
}
self.drawn_version = self.field.version;
}
}