SpawnContext

Struct SpawnContext 

Source
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: u32

Index of the particle being spawned (0 to count-1).

§count: u32

Total number of particles being spawned.

§bounds: f32

Simulation bounds (half-size of bounding cube).

Implementations§

Source§

impl SpawnContext

Source

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 circle
Source

pub fn random(&mut self) -> f32

Random f32 between 0.0 and 1.0.

Source

pub fn random_range(&mut self, min: f32, max: f32) -> f32

Random f32 in the given range.

Source

pub fn random_int(&mut self, min: i32, max: i32) -> i32

Random i32 in the given range.

Source

pub fn random_uint(&mut self, min: u32, max: u32) -> u32

Random u32 in the given range.

Source

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.

Source

pub fn random_on_sphere(&mut self, radius: f32) -> Vec3

Random point on the surface of a sphere of given radius.

Source

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.

Source

pub fn random_in_bounds(&mut self) -> Vec3

Random point within the simulation bounds.

Equivalent to random_in_cube(ctx.bounds).

Source

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 plane
  • half_height - Half the cylinder height
Source

pub fn random_in_disk(&mut self, radius: f32) -> Vec3

Random point inside a disk in the XZ plane at y=0.

Source

pub fn random_on_ring(&mut self, radius: f32) -> Vec3

Random point on a ring (circle) in the XZ plane at y=0.

Source

pub fn random_direction(&mut self) -> Vec3

Random unit vector (uniformly distributed on unit sphere).

Source

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.

Source

pub fn outward_velocity(&mut self, position: Vec3, speed: f32) -> Vec3

Random velocity pointing outward from origin.

Source

pub fn random_color(&mut self) -> Vec3

Random RGB color (each channel 0-1).

Source

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.

Source

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)
Source

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.

Source

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)
Source

pub fn grid_position_2d(&self, cols: u32, rows: u32) -> Vec3

Position on a 2D grid in the XZ plane (y=0).

Source

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.

Source

pub fn circle_position(&self, radius: f32) -> Vec3

Position on a circle in the XZ plane.

Particles are distributed evenly around the circle.

Source

pub fn helix_position(&self, radius: f32, height: f32, turns: f32) -> Vec3

Position on a helix/spiral.

  • radius - Helix radius
  • height - Total height of helix
  • turns - Number of complete rotations

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,