use super::*;
impl Default for NiagaraEmitterConfig3D {
fn default() -> Self {
Self {
origin: Vec3::new(0.0, 0.5, 0.0),
base_velocity: Vec3::new(0.0, 2.2, 2.5),
velocity_spread: 1.6,
min_lifetime: 0.35,
max_lifetime: 0.95,
start_size_m: 0.22,
end_size_m: 0.04,
start_color: Color::rgba(56, 189, 248, 235),
end_color: Color::rgba(168, 85, 247, 0),
drag: 1.4,
turbulence: 1.8,
restitution: 0.45,
emissive_boost: 1.4,
}
}
}
impl Default for NiagaraSystem3D {
fn default() -> Self {
Self::with_capacity(NIAGARA_DEFAULT_CAPACITY)
}
}
impl NiagaraSystem3D {
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
let cap: usize = capacity.max(16);
Self {
capacity: cap,
active_count: 0,
gravity: Vec3::new(0.0, -9.81, 0.0),
sim_time_secs: 0.0,
ring_cursor: 0,
rng_state: 0xD1B5_4A32_D192_ED03,
pos_x: vec![0.0; cap],
pos_y: vec![0.0; cap],
pos_z: vec![0.0; cap],
vel_x: vec![0.0; cap],
vel_y: vec![0.0; cap],
vel_z: vec![0.0; cap],
age: vec![0.0; cap],
lifetime: vec![0.0; cap],
start_size: vec![0.0; cap],
end_size: vec![0.0; cap],
drag: vec![0.0; cap],
turbulence: vec![0.0; cap],
restitution: vec![0.0; cap],
emissive_boost: vec![0.0; cap],
start_color: vec![COLOR_TRANSPARENT; cap],
end_color: vec![COLOR_TRANSPARENT; cap],
}
}
#[cfg_attr(not(target_arch = "wasm32"), inline)]
fn next_signed_f32(state: &mut u64) -> f32 {
let next: u64 = state
.wrapping_mul(NIAGARA_RNG_MULT)
.wrapping_add(NIAGARA_RNG_INC);
*state = next;
let unit: f32 = ((next >> 40) as f32) * (1.0 / ((1_u32 << 24) as f32));
unit * 2.0 - 1.0
}
pub fn emit_burst(&mut self, count: usize, config: &NiagaraEmitterConfig3D) {
let cap: usize = self.get_capacity();
if cap == 0 || count == 0 {
return;
}
let orig: Vec3 = config.get_origin();
let base_v: Vec3 = config.get_base_velocity();
let spread: f32 = config.get_velocity_spread().max(0.0);
let min_life: f32 = config.get_min_lifetime().max(0.04);
let life_span: f32 = (config.get_max_lifetime() - min_life).max(0.0);
let s_size: f32 = config.get_start_size_m().max(0.01);
let e_size: f32 = config.get_end_size_m().max(0.005);
let s_col: Color = config.get_start_color();
let e_col: Color = config.get_end_color();
let drag_v: f32 = config.get_drag().max(0.0);
let turb_v: f32 = config.get_turbulence().max(0.0);
let rest_v: f32 = config.get_restitution().clamp(0.0, 0.95);
let boost_v: f32 = config.get_emissive_boost().clamp(0.0, 4.0);
let Self {
active_count,
ring_cursor,
rng_state,
pos_x,
pos_y,
pos_z,
vel_x,
vel_y,
vel_z,
age,
lifetime,
start_size,
end_size,
drag,
turbulence,
restitution,
emissive_boost,
start_color,
end_color,
..
} = self;
let mut live: usize = *active_count;
let mut cursor: usize = *ring_cursor;
let mut rng: u64 = *rng_state;
for _ in 0..count {
let slot: usize = if live < cap {
let assigned: usize = live;
live += 1;
assigned
} else {
let assigned: usize = cursor % cap;
cursor = (assigned + 1) % cap;
assigned
};
let rx: f32 = Self::next_signed_f32(&mut rng);
let ry: f32 = Self::next_signed_f32(&mut rng);
let rz: f32 = Self::next_signed_f32(&mut rng);
let r_life: f32 = (Self::next_signed_f32(&mut rng) * 0.5 + 0.5).clamp(0.0, 1.0);
pos_x[slot] = orig.get_x();
pos_y[slot] = orig.get_y();
pos_z[slot] = orig.get_z();
vel_x[slot] = base_v.get_x() + rx * spread;
vel_y[slot] = base_v.get_y() + ry * spread;
vel_z[slot] = base_v.get_z() + rz * spread;
age[slot] = 0.0;
lifetime[slot] = min_life + life_span * r_life;
start_size[slot] = s_size;
end_size[slot] = e_size;
drag[slot] = drag_v;
turbulence[slot] = turb_v;
restitution[slot] = rest_v;
emissive_boost[slot] = boost_v;
start_color[slot] = s_col;
end_color[slot] = e_col;
}
*active_count = live;
*ring_cursor = cursor;
*rng_state = rng;
}
pub fn update(&mut self, dt_secs: f32, ground_y: f32) -> usize {
let safe_dt: f32 = dt_secs.clamp(0.0, 0.1);
if safe_dt <= 0.0 || self.get_active_count() == 0 {
return self.get_active_count();
}
let sim_t: f32 = self.get_sim_time_secs() + safe_dt;
self.set_sim_time_secs(sim_t);
let grav: Vec3 = self.get_gravity();
let gx_dt: f32 = grav.get_x() * safe_dt;
let gy_dt: f32 = grav.get_y() * safe_dt;
let gz_dt: f32 = grav.get_z() * safe_dt;
let Self {
active_count,
pos_x,
pos_y,
pos_z,
vel_x,
vel_y,
vel_z,
age,
lifetime,
start_size,
end_size,
drag,
turbulence,
restitution,
emissive_boost,
start_color,
end_color,
..
} = self;
let mut live: usize = *active_count;
let mut idx: usize = 0;
while idx < live {
let next_age: f32 = age[idx] + safe_dt;
if next_age >= lifetime[idx] {
live -= 1;
if idx < live {
pos_x[idx] = pos_x[live];
pos_y[idx] = pos_y[live];
pos_z[idx] = pos_z[live];
vel_x[idx] = vel_x[live];
vel_y[idx] = vel_y[live];
vel_z[idx] = vel_z[live];
age[idx] = age[live];
lifetime[idx] = lifetime[live];
start_size[idx] = start_size[live];
end_size[idx] = end_size[live];
drag[idx] = drag[live];
turbulence[idx] = turbulence[live];
restitution[idx] = restitution[live];
emissive_boost[idx] = emissive_boost[live];
start_color[idx] = start_color[live];
end_color[idx] = end_color[live];
}
continue;
}
age[idx] = next_age;
let px: f32 = pos_x[idx];
let py: f32 = pos_y[idx];
let pz: f32 = pos_z[idx];
let turb: f32 = turbulence[idx];
let (tx, ty, tz): (f32, f32, f32) = if turb > 1.0e-4 {
let phase_x: f32 = py * 1.7 + sim_t * 3.1;
let phase_y: f32 = pz * 1.7 - sim_t * 2.7;
let phase_z: f32 = px * 1.7 + sim_t * 2.3;
(
phase_x.sin() * turb * safe_dt,
phase_y.cos() * turb * 0.5 * safe_dt,
phase_z.sin() * turb * safe_dt,
)
} else {
(0.0, 0.0, 0.0)
};
let damp: f32 = (1.0 - drag[idx] * safe_dt).clamp(0.0, 1.0);
let mut nvx: f32 = (vel_x[idx] + gx_dt + tx) * damp;
let mut nvy: f32 = (vel_y[idx] + gy_dt + ty) * damp;
let mut nvz: f32 = (vel_z[idx] + gz_dt + tz) * damp;
let npx: f32 = nvx.mul_add(safe_dt, px);
let mut npy: f32 = nvy.mul_add(safe_dt, py);
let npz: f32 = nvz.mul_add(safe_dt, pz);
if npy < ground_y {
npy = ground_y;
if nvy < 0.0 {
nvy = -nvy * restitution[idx];
nvx *= 0.84;
nvz *= 0.84;
}
}
pos_x[idx] = npx;
pos_y[idx] = npy;
pos_z[idx] = npz;
vel_x[idx] = nvx;
vel_y[idx] = nvy;
vel_z[idx] = nvz;
idx += 1;
}
*active_count = live;
live
}
pub fn clear(&mut self) {
self.set_active_count(0);
let Self { ring_cursor, .. } = self;
*ring_cursor = 0;
}
pub fn composite_depth_tested(
&self,
view_proj: &Mat4,
focal_px: f32,
dims: (usize, usize),
depth_buffer: &[f32],
rgba_out: &mut [u8],
) -> usize {
let (width, height): (usize, usize) = dims;
let live: usize = self.get_active_count();
if live == 0
|| width == 0
|| height == 0
|| depth_buffer.len() < width * height
|| rgba_out.len() < width * height * 4
{
return 0;
}
let w_f: f32 = width as f32;
let h_f: f32 = height as f32;
let w_max: i32 = (width as i32) - 1;
let h_max: i32 = (height as i32) - 1;
let mut drawn: usize = 0;
let Self {
pos_x,
pos_y,
pos_z,
age,
lifetime,
start_size,
end_size,
emissive_boost,
start_color,
end_color,
..
} = self;
for idx in 0..live {
let world_pt: Vec3 = Vec3::new(pos_x[idx], pos_y[idx], pos_z[idx]);
let clip: [f32; 4] = view_proj.mul_point4(world_pt);
if clip[3] <= NIAGARA_NEAR_CLIP_M {
continue;
}
let inv_w: f32 = 1.0 / clip[3];
let sx: f32 = (clip[0] * inv_w * 0.5 + 0.5) * w_f;
let sy: f32 = (0.5 - clip[1] * inv_w * 0.5) * h_f;
let life_t: f32 = (age[idx] / lifetime[idx].max(1.0e-4)).clamp(0.0, 1.0);
let world_rad: f32 = start_size[idx] + (end_size[idx] - start_size[idx]) * life_t;
let px_rad: f32 =
(world_rad * focal_px * inv_w).clamp(1.0, NIAGARA_MAX_BILLBOARD_RADIUS_PX);
let x0: i32 = ((sx - px_rad).floor() as i32).max(0);
let x1: i32 = ((sx + px_rad).ceil() as i32).min(w_max);
let y0: i32 = ((sy - px_rad).floor() as i32).max(0);
let y1: i32 = ((sy + px_rad).ceil() as i32).min(h_max);
if x0 > x1 || y0 > y1 {
continue;
}
let c0: Color = start_color[idx];
let c1: Color = end_color[idx];
let keep: f32 = 1.0 - life_t;
let boost: f32 = emissive_boost[idx];
let cr: f32 = ((c0.get_red() as f32) * keep + (c1.get_red() as f32) * life_t) * boost;
let cg: f32 =
((c0.get_green() as f32) * keep + (c1.get_green() as f32) * life_t) * boost;
let cb: f32 = ((c0.get_blue() as f32) * keep + (c1.get_blue() as f32) * life_t) * boost;
let base_alpha: f32 =
(((c0.get_alpha() as f32) * keep + (c1.get_alpha() as f32) * life_t) / 255.0)
.clamp(0.0, 1.0);
if base_alpha <= 0.01 {
continue;
}
let inv_r2: f32 = 1.0 / (px_rad * px_rad).max(1.0);
let mut wrote_any: bool = false;
for py in y0..=y1 {
let dy: f32 = (py as f32) + 0.5 - sy;
let dy2: f32 = dy * dy;
let row_off: usize = (py as usize) * width;
for px in x0..=x1 {
let dx: f32 = (px as f32) + 0.5 - sx;
let q2: f32 = (dx * dx + dy2) * inv_r2;
if q2 >= 1.0 {
continue;
}
let pix_idx: usize = row_off + (px as usize);
if inv_w < depth_buffer[pix_idx] {
continue;
}
let falloff: f32 = (1.0 - q2) * (1.0 - q2);
let alpha: f32 = (base_alpha * falloff).clamp(0.0, 1.0);
let inv_a: f32 = 1.0 - alpha;
let off: usize = pix_idx * 4;
let nr: f32 = ((rgba_out[off] as f32) * inv_a + cr * alpha).clamp(0.0, 255.0);
let ng: f32 =
((rgba_out[off + 1] as f32) * inv_a + cg * alpha).clamp(0.0, 255.0);
let nb: f32 =
((rgba_out[off + 2] as f32) * inv_a + cb * alpha).clamp(0.0, 255.0);
rgba_out[off] = nr as u8;
rgba_out[off + 1] = ng as u8;
rgba_out[off + 2] = nb as u8;
wrote_any = true;
}
}
if wrote_any {
drawn += 1;
}
}
drawn
}
}