FieldConfig

Struct FieldConfig 

Source
pub struct FieldConfig {
    pub resolution: u32,
    pub world_extent: f32,
    pub decay: f32,
    pub blur: f32,
    pub blur_iterations: u32,
    pub field_type: FieldType,
    pub custom_update: Option<String>,
}
Expand description

Configuration for a 3D spatial field.

Fields are 3D grids that particles can read from and write to. Each frame, the field is processed: blur (diffusion), then decay. Optionally, custom WGSL code can replace the default blur/decay behavior.

Fields§

§resolution: u32

Grid resolution per axis (total cells = resolution³). Higher = more detail but more memory. Typical: 32, 64, 128.

§world_extent: f32

World-space extent of the field (cube from -extent to +extent). Should match or exceed your simulation bounds.

§decay: f32

Per-frame decay multiplier (0.0-1.0). 0.98 = slow decay, 0.5 = fast decay, 1.0 = no decay.

§blur: f32

Blur strength per frame (0.0-1.0). Controls diffusion rate. 0.0 = no blur, 1.0 = max blur.

§blur_iterations: u32

Number of blur iterations per frame. More iterations = smoother but more expensive.

§field_type: FieldType

Type of field (Scalar or Vector).

§custom_update: Option<String>

Custom WGSL code for field updates (replaces blur/decay if set).

Available variables:

  • cell_idx: u32 - Linear index of current cell
  • pos: vec3<u32> - Grid position (x, y, z)
  • world_pos: vec3<f32> - World-space position
  • value: f32 (scalar) or value: vec3<f32> (vector) - Current cell value
  • params.resolution, params.extent, params.decay, params.blur
  • uniforms.time, uniforms.delta_time

Helper functions:

  • read_neighbor(dx: i32, dy: i32, dz: i32) -> f32/vec3<f32> - Read neighbor cell
  • idx_3d(x, y, z) -> u32 - Convert 3D coords to linear index

Output: Write to new_value (same type as value)

Example (reaction-diffusion):

let lap = read_neighbor(-1,0,0) + read_neighbor(1,0,0)
        + read_neighbor(0,-1,0) + read_neighbor(0,1,0)
        + read_neighbor(0,0,-1) + read_neighbor(0,0,1) - 6.0 * value;
new_value = value + lap * 0.1 * uniforms.delta_time;

Implementations§

Source§

impl FieldConfig

Source

pub fn new(resolution: u32) -> Self

Create a new field configuration with the given resolution.

Default values:

  • world_extent: 1.0 (cube from -1 to +1)
  • decay: 0.99 (slow decay)
  • blur: 0.1 (light diffusion)
  • blur_iterations: 1
§Memory Usage
  • 32³ = 128KB
  • 64³ = 1MB
  • 128³ = 8MB
§Example
let field = FieldConfig::new(64);
Source

pub fn new_vector(resolution: u32) -> Self

Create a vector field configuration.

Vector fields store a vec3<f32> per cell instead of a scalar. Use for velocity fields, force fields, or flow visualization.

§Memory Usage (3x scalar fields)
  • 32³ = 384KB
  • 64³ = 3MB
  • 128³ = 24MB
Source

pub fn vector(self) -> Self

Convert this field to a vector field.

Vector fields store vec3<f32> per cell for velocity/force data.

Source

pub fn with_extent(self, extent: f32) -> Self

Set the world-space extent of the field.

The field covers a cube from -extent to +extent on all axes. Should match or exceed your simulation bounds.

Source

pub fn with_decay(self, decay: f32) -> Self

Set the decay rate (0.0-1.0).

Applied each frame: field *= decay

  • 1.0 = no decay (field persists forever)
  • 0.99 = slow decay
  • 0.9 = fast decay
  • 0.0 = instant decay (field clears each frame)
Source

pub fn with_blur(self, blur: f32) -> Self

Set the blur/diffusion strength (0.0-1.0).

Controls how much values spread to neighboring cells each frame.

  • 0.0 = no diffusion
  • 0.1 = light spread
  • 0.5 = heavy spread
  • 1.0 = maximum diffusion
Source

pub fn with_blur_iterations(self, iterations: u32) -> Self

Set the number of blur iterations per frame.

More iterations = smoother diffusion but more expensive. Usually 1-3 is sufficient.

Source

pub fn with_custom_update(self, code: impl Into<String>) -> Self

Set custom WGSL code for field updates.

When set, replaces the default blur/decay behavior with custom logic. See FieldConfig::custom_update for available variables and helpers.

§Example
FieldConfig::new(64)
    .with_custom_update(r#"
        // Simple diffusion with custom decay
        let lap = read_neighbor(-1,0,0) + read_neighbor(1,0,0)
                + read_neighbor(0,-1,0) + read_neighbor(0,1,0)
                + read_neighbor(0,0,-1) + read_neighbor(0,0,1) - 6.0 * value;
        new_value = (value + lap * 0.2) * 0.99;
    "#)
Source

pub fn has_custom_update(&self) -> bool

Check if this field has custom update logic.

Source

pub fn total_cells(&self) -> u32

Total number of cells in the field.

Source

pub fn memory_size(&self) -> usize

Memory size in bytes (for the main field buffer).

Source

pub fn is_vector(&self) -> bool

Whether this is a vector field.

Trait Implementations§

Source§

impl Clone for FieldConfig

Source§

fn clone(&self) -> FieldConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FieldConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FieldConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 + Send + Sync>

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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,