pub trait RuntimePlugin: Send + 'static {
// Required methods
fn priority(&self) -> i32;
fn step(&mut self, ctx: &mut RuntimeStepContext<'_>);
// Provided methods
fn type_name(&self) -> &'static str { ... }
fn submit(&mut self, _ctx: &RuntimeStepContext<'_>) { ... }
fn collect(&mut self, _ctx: &mut RuntimeStepContext<'_>) { ... }
fn on_event(
&mut self,
_event: &RuntimeEvent,
_ctx: &mut RuntimeStepContext<'_>,
) { ... }
}Expand description
A plugin that executes during the scene step.
Register plugins with super::ViewportRuntime::with_plugin. Each frame
the runtime calls submit, then step (once or more for simulate phases),
then collect.
Registration model: runtime plugins are multi-instance. They carry no
external identity, so registering two of the same type runs both, in
priority order; the runtime does not deduplicate. This is intentional (two
emitters, two solvers with different config are valid) and is frozen
behavior. Item-type plugins differ: they are singleton-by-type because
their type_name is referenced externally. See
ItemTypePlugin.
§Example
use viewport_lib::runtime::{RuntimePlugin, RuntimeStepContext};
use viewport_lib::runtime::plugin::phase;
struct GravityPlugin {
gravity: glam::Vec3,
}
impl RuntimePlugin for GravityPlugin {
fn priority(&self) -> i32 {
phase::SIMULATE
}
fn step(&mut self, ctx: &mut RuntimeStepContext) {
// Apply gravity to tracked bodies via ctx.writeback.set(id, new_transform).
}
}Required Methods§
Sourcefn priority(&self) -> i32
fn priority(&self) -> i32
Numeric execution priority. Lower values run first. Use the constants
in phase as base values and offset within a band if needed.
Sourcefn step(&mut self, ctx: &mut RuntimeStepContext<'_>)
fn step(&mut self, ctx: &mut RuntimeStepContext<'_>)
Called once per phase execution.
For plugins in the [SIMULATE, POST_SIM) range with a fixed timestep,
ctx.dt is the fixed step size and this is called once per step. For
all other priorities, ctx.dt is the wall-clock frame delta and this
is called once per frame.
Provided Methods§
Sourcefn type_name(&self) -> &'static str
fn type_name(&self) -> &'static str
A stable name for this plugin, used to key per-plugin timing in
RuntimeStats. Defaults to the concrete type
name; override to disambiguate when several plugins share a type or to
give a friendlier label.
Sourcefn submit(&mut self, _ctx: &RuntimeStepContext<'_>)
fn submit(&mut self, _ctx: &RuntimeStepContext<'_>)
Called once per frame before the step loop, in priority order.
Use this to kick off background work (e.g. async physics) that will
be read in collect on the next frame.
Sourcefn collect(&mut self, _ctx: &mut RuntimeStepContext<'_>)
fn collect(&mut self, _ctx: &mut RuntimeStepContext<'_>)
Called once per frame after the step loop, in priority order.
Use this to read results from background work started in submit.
Sourcefn on_event(&mut self, _event: &RuntimeEvent, _ctx: &mut RuntimeStepContext<'_>)
fn on_event(&mut self, _event: &RuntimeEvent, _ctx: &mut RuntimeStepContext<'_>)
Called for each lifecycle event before the step loop.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".