pub struct SpawnContext {
pub index: u32,
pub count: u32,
pub bounds: f32,
/* private fields */
}Expand description
Context provided to spawner functions with helpers for common spawn patterns.
Instead of manually setting up RNG and computing random positions, use the
helper methods on SpawnContext:
// Before: verbose manual setup
let mut rng = rand::thread_rng();
let particles: Vec<Spark> = (0..count)
.map(|_| {
let theta = rng.gen_range(0.0..TAU);
let phi = rng.gen_range(0.0..PI);
let r = rng.gen_range(0.0..0.5);
let x = r * phi.sin() * theta.cos();
let y = r * phi.sin() * theta.sin();
let z = r * phi.cos();
Spark { position: Vec3::new(x, y, z), velocity: Vec3::ZERO, color: Vec3::ONE }
})
.collect();
sim.with_spawner(move |i, _| particles[i].clone())
// After: clean and simple
sim.with_spawner(|ctx| Spark {
position: ctx.random_in_sphere(0.5),
velocity: Vec3::ZERO,
color: Vec3::ONE,
})Fields§
§index: u32Index of the particle being spawned (0 to count-1).
count: u32Total number of particles being spawned.
bounds: f32Simulation bounds (half-size of bounding cube).
Implementations§
Source§impl SpawnContext
impl SpawnContext
Sourcepub fn progress(&self) -> f32
pub fn progress(&self) -> f32
Normalized progress through the spawn (0.0 to 1.0).
Useful for distributing particles evenly:
let angle = ctx.progress() * TAU; // Particles around a circleSourcepub fn random_range(&mut self, min: f32, max: f32) -> f32
pub fn random_range(&mut self, min: f32, max: f32) -> f32
Random f32 in the given range.
Sourcepub fn random_int(&mut self, min: i32, max: i32) -> i32
pub fn random_int(&mut self, min: i32, max: i32) -> i32
Random i32 in the given range.
Sourcepub fn random_uint(&mut self, min: u32, max: u32) -> u32
pub fn random_uint(&mut self, min: u32, max: u32) -> u32
Random u32 in the given range.
Sourcepub fn random_in_sphere(&mut self, radius: f32) -> Vec3
pub fn random_in_sphere(&mut self, radius: f32) -> Vec3
Random point inside a sphere of given radius, centered at origin.
Distribution is uniform throughout the volume.
Sourcepub fn random_on_sphere(&mut self, radius: f32) -> Vec3
pub fn random_on_sphere(&mut self, radius: f32) -> Vec3
Random point on the surface of a sphere of given radius.
Sourcepub fn random_in_cube(&mut self, half_size: f32) -> Vec3
pub fn random_in_cube(&mut self, half_size: f32) -> Vec3
Random point inside a cube of given half-size, centered at origin.
For a cube from -1 to 1, use half_size = 1.0.
Sourcepub fn random_in_bounds(&mut self) -> Vec3
pub fn random_in_bounds(&mut self) -> Vec3
Random point within the simulation bounds.
Equivalent to random_in_cube(ctx.bounds).
Sourcepub fn random_in_cylinder(&mut self, radius: f32, half_height: f32) -> Vec3
pub fn random_in_cylinder(&mut self, radius: f32, half_height: f32) -> Vec3
Random point inside a cylinder along the Y axis.
radius- Cylinder radius in XZ planehalf_height- Half the cylinder height
Sourcepub fn random_in_disk(&mut self, radius: f32) -> Vec3
pub fn random_in_disk(&mut self, radius: f32) -> Vec3
Random point inside a disk in the XZ plane at y=0.
Sourcepub fn random_on_ring(&mut self, radius: f32) -> Vec3
pub fn random_on_ring(&mut self, radius: f32) -> Vec3
Random point on a ring (circle) in the XZ plane at y=0.
Sourcepub fn random_direction(&mut self) -> Vec3
pub fn random_direction(&mut self) -> Vec3
Random unit vector (uniformly distributed on unit sphere).
Sourcepub fn tangent_velocity(&self, position: Vec3, speed: f32) -> Vec3
pub fn tangent_velocity(&self, position: Vec3, speed: f32) -> Vec3
Random velocity tangent to position (for orbital motion).
Returns a velocity perpendicular to the position vector, in the XZ plane. Useful for setting up swirling/orbiting particles.
Sourcepub fn outward_velocity(&mut self, position: Vec3, speed: f32) -> Vec3
pub fn outward_velocity(&mut self, position: Vec3, speed: f32) -> Vec3
Random velocity pointing outward from origin.
Sourcepub fn random_color(&mut self) -> Vec3
pub fn random_color(&mut self) -> Vec3
Random RGB color (each channel 0-1).
Sourcepub fn random_hue(&mut self, saturation: f32, value: f32) -> Vec3
pub fn random_hue(&mut self, saturation: f32, value: f32) -> Vec3
Random color with given saturation and value (HSV model).
Hue is randomized, giving vibrant varied colors.
Sourcepub fn hsv(&self, hue: f32, saturation: f32, value: f32) -> Vec3
pub fn hsv(&self, hue: f32, saturation: f32, value: f32) -> Vec3
Color from HSV values.
hue- 0.0 to 1.0 (wraps: red → yellow → green → cyan → blue → magenta → red)saturation- 0.0 (gray) to 1.0 (vivid)value- 0.0 (black) to 1.0 (bright)
Sourcepub fn rainbow(&mut self, saturation: f32, value: f32) -> Vec3
pub fn rainbow(&mut self, saturation: f32, value: f32) -> Vec3
Color based on spawn progress (rainbow gradient).
First particle is red, middle is cyan, last is back to red.
Sourcepub fn grid_position(&self, cols: u32, rows: u32, layers: u32) -> Vec3
pub fn grid_position(&self, cols: u32, rows: u32, layers: u32) -> Vec3
Position in a 3D grid layout.
Distributes particles evenly in a grid within bounds.
cols- Number of columns (X axis)rows- Number of rows (Y axis)layers- Number of layers (Z axis)
Sourcepub fn grid_position_2d(&self, cols: u32, rows: u32) -> Vec3
pub fn grid_position_2d(&self, cols: u32, rows: u32) -> Vec3
Position on a 2D grid in the XZ plane (y=0).
Sourcepub fn line_position(&self, start: Vec3, end: Vec3) -> Vec3
pub fn line_position(&self, start: Vec3, end: Vec3) -> Vec3
Position along a line from start to end.
Particles are distributed evenly along the line.
Sourcepub fn circle_position(&self, radius: f32) -> Vec3
pub fn circle_position(&self, radius: f32) -> Vec3
Position on a circle in the XZ plane.
Particles are distributed evenly around the circle.
Auto Trait Implementations§
impl Freeze for SpawnContext
impl RefUnwindSafe for SpawnContext
impl Send for SpawnContext
impl Sync for SpawnContext
impl Unpin for SpawnContext
impl UnwindSafe for SpawnContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.