MultiParticle

Derive Macro MultiParticle 

Source
#[derive(MultiParticle)]
{
    // Attributes available to this derive:
    #[color]
}
Expand description

Derive macro for multi-particle enums with inline struct definitions.

Generates standalone particle structs AND a unified enum, enabling both single-type and heterogeneous particle simulations from one definition.

§Overview

MultiParticle lets you define multiple particle types inline as struct-like enum variants. The macro generates:

  1. Standalone structs - Each variant becomes its own struct implementing ParticleTrait
  2. Unified enum - The enum implements ParticleTrait with all fields combined
  3. Rust type constants - EnumName::VARIANT constants for use in typed rules
  4. WGSL helpers - Type constants and helper functions for shaders

§Example

use rdpe::prelude::*;

#[derive(MultiParticle, Clone)]
enum Creature {
    Boid {
        position: Vec3,
        velocity: Vec3,
        flock_id: u32,
    },
    Predator {
        position: Vec3,
        velocity: Vec3,
        hunger: f32,
        target_id: u32,
    },
}

// All three work:
Simulation::<Creature>::new()  // Mixed simulation
Simulation::<Boid>::new()      // Boid-only simulation (uses generated struct)
Simulation::<Predator>::new()  // Predator-only simulation (uses generated struct)

// Creating particles with clean struct-like syntax:
Creature::Boid { position: Vec3::ZERO, velocity: Vec3::ZERO, flock_id: 0 }
Creature::Predator { position: Vec3::ZERO, velocity: Vec3::ZERO, hunger: 1.0, target_id: 0 }

// Using type constants in rules:
Rule::Chase {
    self_type: Creature::PREDATOR,
    target_type: Creature::BOID,
    radius: 0.5,
    strength: 3.0,
}

§Requirements

  • The enum must also derive Clone (for ParticleTrait bounds)
  • Each variant must use struct-like syntax with named fields
  • Each variant must have position: Vec3 and velocity: Vec3 fields
  • Use #[color] attribute on a Vec3 field for custom particle color

§Generated Code

For an enum Creature { Boid { ... }, Predator { ... } }, the macro generates:

// Standalone structs (separate types for single-particle simulations)
struct Boid { position: Vec3, velocity: Vec3, flock_id: u32 }
impl ParticleTrait for Boid { ... }

struct Predator { position: Vec3, velocity: Vec3, hunger: f32, target_id: u32 }
impl ParticleTrait for Predator { ... }

// ParticleTrait on the original enum (for mixed simulations)
impl ParticleTrait for Creature { ... }  // Unified GPU struct with all fields

§WGSL Usage

In custom rules, use the generated constants and helpers:

// Type constants
const BOID: u32 = 0u;
const PREDATOR: u32 = 1u;

// Helper functions
fn is_boid(p: Particle) -> bool { return p.particle_type == 0u; }
fn is_predator(p: Particle) -> bool { return p.particle_type == 1u; }

// Usage in custom rules
if is_predator(p) {
    p.hunger -= uniforms.delta_time * 0.1;
}