euv-core 0.52.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use super::*;

/// Default glowing neon spark emitter preset for `NiagaraEmitterConfig3D`.
impl Default for NiagaraEmitterConfig3D {
    /// Creates a glowing neon spark burst configuration.
    ///
    /// # Returns
    ///
    /// - `Self` - Default 3D Niagara emitter configuration.
    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,
        }
    }
}

/// Default 2,048-slot `NiagaraSystem3D`.
impl Default for NiagaraSystem3D {
    /// Creates a pre-allocated 2,048-slot `NiagaraSystem3D` with Earth gravity.
    ///
    /// # Returns
    ///
    /// - `Self` - Ready zero-allocation 3D particle system.
    fn default() -> Self {
        Self::with_capacity(NIAGARA_DEFAULT_CAPACITY)
    }
}

/// SoA 3D particle emission, curl-noise kinematics, floor collision, and depth-tested billboard compositing for `NiagaraSystem3D`.
impl NiagaraSystem3D {
    /// Constructs a pre-allocated `NiagaraSystem3D` sized for `capacity` live 3D particles with zero runtime heap allocations.
    ///
    /// # Arguments
    ///
    /// - `usize` - Maximum concurrent 3D particle slots (`>= 16`).
    ///
    /// # Returns
    ///
    /// - `Self` - Pre-allocated 3D Niagara particle pool.
    #[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],
        }
    }

    /// Generates a deterministic pseudo-random `f32` in `[-1.0, 1.0)` advancing `state`.
    ///
    /// # Arguments
    ///
    /// - `&mut u64` - Mutable PRNG state word.
    ///
    /// # Returns
    ///
    /// - `f32` - Signed pseudo-random scalar in `[-1.0, 1.0)`.
    #[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
    }

    /// Emits `count` 3D particles configured by `config` directly into the compacted SoA pool with zero heap allocations.
    ///
    /// # Arguments
    ///
    /// - `usize` - Number of particles to emit.
    /// - `&NiagaraEmitterConfig3D` - Emission origin, velocity, lifetime, and optical configuration.
    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;
    }

    /// Advances all active 3D particles by `dt_secs` with gravity, drag, curl-noise turbulence, and ground bounce, returning remaining active count.
    ///
    /// # Arguments
    ///
    /// - `f32` - Simulation timestep in seconds.
    /// - `f32` - Ground plane Y elevation in meters for bounce collision.
    ///
    /// # Returns
    ///
    /// - `usize` - Remaining active particle count.
    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
    }

    /// Clears all live 3D particles in O(1) while retaining pre-allocated SoA capacities.
    pub fn clear(&mut self) {
        self.set_active_count(0);
        let Self { ring_cursor, .. } = self;
        *ring_cursor = 0;
    }

    /// Projects and depth-tests all active 3D Niagara particles onto `rgba_out` using the reverse-Z `depth_buffer`, returning rendered particle count.
    ///
    /// # Arguments
    ///
    /// - `&Mat4` - Active frame view-projection matrix.
    /// - `f32` - Projected viewport focal length in pixels.
    /// - `(usize, usize)` - Target framebuffer `(width, height)` in pixels.
    /// - `&[f32]` - Reverse-Z `1/w` depth buffer (`0.0` = sky, larger = nearer).
    /// - `&mut [u8]` - Target RGBA8 framebuffer updated with depth-tested glowing billboards.
    ///
    /// # Returns
    ///
    /// - `usize` - Number of visible 3D particles composited onto the framebuffer.
    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
    }
}