Struct bevy_hanabi::graph::expr::ExprWriter
source · 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 Modifer
s 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
impl ExprWriter
sourcepub fn from_module(module: Rc<RefCell<Module>>) -> Self
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
.
sourcepub fn push(&self, expr: impl Into<Expr>) -> WriterExpr
pub fn push(&self, expr: impl Into<Expr>) -> WriterExpr
Push a new expression into the writer.
sourcepub fn lit(&self, value: impl Into<Value>) -> WriterExpr
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;
sourcepub fn attr(&self, attr: Attribute) -> WriterExpr
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;
sourcepub fn prop(&self, name: impl Into<String>) -> WriterExpr
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;
sourcepub fn time(&self) -> WriterExpr
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;
sourcepub fn delta_time(&self) -> WriterExpr
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;
sourcepub fn rand(&self, value_type: impl Into<ValueType>) -> WriterExpr
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();
sourcepub fn alpha_cutoff(&self) -> WriterExpr
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;
sourcepub fn finish(self) -> Module
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
impl Clone for ExprWriter
source§fn clone(&self) -> ExprWriter
fn clone(&self) -> ExprWriter
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for ExprWriter
impl Debug for ExprWriter
source§impl Default for ExprWriter
impl Default for ExprWriter
source§fn default() -> ExprWriter
fn default() -> ExprWriter
Auto Trait Implementations§
impl !RefUnwindSafe for ExprWriter
impl !Send for ExprWriter
impl !Sync for ExprWriter
impl Unpin for ExprWriter
impl !UnwindSafe for ExprWriter
Blanket Implementations§
§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
[ShaderType
] for self
. When used in [AsBindGroup
]
derives, it is safe to assume that all images in self
exist.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
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§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
.§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
.§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.§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.§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given [World
].