ability to rewind particle systems to a particular time
@@ -404,42 +404,8 @@ impl ParticleSystem {
pub fn texture_ref(&self) -> Option<&Texture> {
self.texture.as_ref()
}
-}
-
-impl Default for ParticleSystem {
- fn default() -> Self {
- ParticleSystemBuilder::new(BaseBuilder::new()).build_particle_system()
- }
-}
-
-impl NodeTrait for ParticleSystem {
- crate::impl_query_component!();
- fn local_bounding_box(&self) -> AxisAlignedBoundingBox {
- AxisAlignedBoundingBox::unit()
- }
-
- fn world_bounding_box(&self) -> AxisAlignedBoundingBox {
- self.local_bounding_box()
- .transform(&self.global_transform())
- }
-
- fn restore_resources(&mut self, resource_manager: ResourceManager) {
- self.base.restore_resources(resource_manager.clone());
-
- let mut state = resource_manager.state();
- let texture_container = &mut state.containers_mut().textures;
- texture_container.try_restore_inheritable_resource(&mut self.texture);
- }
-
- fn id(&self) -> Uuid {
- Self::type_uuid()
- }
-
- fn update(&mut self, context: &mut UpdateContext) {
- let dt = context.dt;
-
- if *self.is_playing {
+ fn tick(&mut self, dt: f32) {
for emitter in self.emitters.get_value_mut_silent().iter_mut() {
emitter.tick(dt);
}
@@ -491,6 +457,57 @@ impl NodeTrait for ParticleSystem {
}
}
}
+
+ /// Simulates particle system for the given `time` with given time step (`dt`). `dt` is usually `1.0 / 60.0`.
+ pub fn rewind(&mut self, dt: f32, time: f32) {
+ assert!(dt > 0.0);
+
+ self.clear_particles();
+
+ let mut t = 0.0;
+ while t < time {
+ self.tick(dt);
+ t += dt;
+ }
+ }
+}
+
+impl Default for ParticleSystem {
+ fn default() -> Self {
+ ParticleSystemBuilder::new(BaseBuilder::new()).build_particle_system()
+ }
+}
+
+impl NodeTrait for ParticleSystem {
+ crate::impl_query_component!();
+
+ fn local_bounding_box(&self) -> AxisAlignedBoundingBox {
+ AxisAlignedBoundingBox::unit()
+ }
+
+ fn world_bounding_box(&self) -> AxisAlignedBoundingBox {
+ self.local_bounding_box()
+ .transform(&self.global_transform())
+ }
+
+ fn restore_resources(&mut self, resource_manager: ResourceManager) {
+ self.base.restore_resources(resource_manager.clone());
+
+ let mut state = resource_manager.state();
+ let texture_container = &mut state.containers_mut().textures;
+ texture_container.try_restore_inheritable_resource(&mut self.texture);
+ }
+
+ fn id(&self) -> Uuid {
+ Self::type_uuid()
+ }
+
+ fn update(&mut self, context: &mut UpdateContext) {
+ let dt = context.dt;
+
+ if *self.is_playing {
+ self.tick(dt);
+ }
}
}
