pub struct ExprWriter { /* private fields */ }
Expand description

Expression writer.

Utility to write expressions with a simple functional syntax. Expressions created with the writer are gatherned into a Module which can be transfered once finish()ed to initialize an EffectAsset.

Because an EffectAsset contains a single Module, you generally want to keep using the same ExprWriter to write all the expressions used by all the Modifers assigned to a given EffectAsset, and only then once done call finish() to recover the ExprWriter’s underlying Module to assign it to the EffectAsset.

§Example

// Create a writer
let w = ExprWriter::new();

// Create a new expression: max(5. + particle.position, properties.my_prop)
let expr = (w.lit(5.) + w.attr(Attribute::POSITION)).max(w.prop("my_prop"));

// Finalize the expression and write it down into the `Module` as an `Expr`
let expr: ExprHandle = expr.expr();

// Create a modifier and assign the expression to one of its input(s)
let init_modifier = SetAttributeModifier::new(Attribute::LIFETIME, expr);

// Create an EffectAsset with the modifier and the Module from the writer
let effect = EffectAsset::new(1024, Spawner::rate(32_f32.into()), w.finish())
    .init(init_modifier);

Implementations§

source§

impl ExprWriter

source

pub fn new() -> Self

Create a new writer.

The writer owns a new Module internally, and write all expressions to it. The module can be released to the user with finish() once done using the writer.

source

pub fn from_module(module: Rc<RefCell<Module>>) -> Self

Create a new writer from an existing module.

This is an advanced use entry point to write expressions into an existing Module. In general, users should prefer using ExprWriter::new() to create a new Module, and keep using the same ExprWriter to write all expressions of the same EffectAsset.

source

pub fn push(&self, expr: impl Into<Expr>) -> WriterExpr

Push a new expression into the writer.

source

pub fn lit(&self, value: impl Into<Value>) -> WriterExpr

Create a new writer expression from a literal constant.

§Example
let mut w = ExprWriter::new();
let x = w.lit(-3.5); // x = -3.5;
source

pub fn attr(&self, attr: Attribute) -> WriterExpr

Create a new writer expression from an attribute.

§Example
let mut w = ExprWriter::new();
let x = w.attr(Attribute::POSITION); // x = particle.position;
source

pub fn prop(&self, name: impl Into<String>) -> WriterExpr

Create a new writer expression from a property.

§Example
let mut w = ExprWriter::new();
let x = w.prop("my_prop"); // x = properties.my_prop;
source

pub fn time(&self) -> WriterExpr

Create a new writer expression representing the current simulation time.

§Example
let mut w = ExprWriter::new();
let x = w.time(); // x = sim_params.time;
source

pub fn delta_time(&self) -> WriterExpr

Create a new writer expression representing the simulation delta time since last frame.

§Example
let mut w = ExprWriter::new();
let x = w.delta_time(); // x = sim_params.delta_time;
source

pub fn rand(&self, value_type: impl Into<ValueType>) -> WriterExpr

Create a new writer expression representing a random value of the given type.

The type can be any scalar or vector type. Matrix types are not supported. The random values generated are uniformly distributed in [0:1]. For vectors, each component is sampled separately.

§Panics

Panics in the same cases as BuiltInExpr::new() does.

§Example
let mut w = ExprWriter::new();
let x = w.rand(VectorType::VEC3F); // x = frand3();
source

pub fn alpha_cutoff(&self) -> WriterExpr

Create a new writer expression representing the alpha cutoff value used for alpha masking.

This expression is only valid when used in the context of the fragment shader, in the render context.

§Example
let mut w = ExprWriter::new();
let x = w.alpha_cutoff(); // x = alpha_cutoff;
source

pub fn finish(self) -> Module

Finish using the writer, and recover the Module where all Expr were written by the writer.

This module is typically passed to EffectAsset::new() before adding to that effect the modifiers which use the expressions created by this writer.

§Example
let mut w = ExprWriter::new();
// [...]
let module = w.finish();
let asset = EffectAsset::new(256, spawner, module);

Trait Implementations§

source§

impl Clone for ExprWriter

source§

fn clone(&self) -> ExprWriter

Returns a copy 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 ExprWriter

source§

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

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

impl Default for ExprWriter

source§

fn default() -> ExprWriter

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
§

impl<T, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U

Return the T [ShaderType] for self. When used in [AsBindGroup] derives, it is safe to assume that all images in self exist.
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
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

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

§

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.
§

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.
§

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.
§

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromWorld for T
where T: Default,

§

fn from_world(_world: &mut World) -> T

Creates Self using data from the given [World].
§

impl<T> Instrument for T

§

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

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

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,

§

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

§

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

§

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.
§

impl<T> Upcast<T> for T

§

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

§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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