berdicles 0.0.3

Expressive CPU particle system for the bevy engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#![allow(clippy::too_many_arguments)]
#![allow(clippy::type_complexity)]
#![doc = include_str!("../README.md")]
use std::{
    any::Any,
    fmt::Debug,
    ops::{Deref, DerefMut, Range},
    sync::Arc,
};

use bevy::{
    app::{Plugin, PostUpdate, Update},
    asset::Assets,
    color::{ColorToComponents, Srgba},
    ecs::query::QueryItem,
    math::{Quat, Vec3},
    prelude::{Commands, Component, Entity, IntoSystemConfigs, Query, Res},
    render::{
        extract_component::{ExtractComponent, ExtractComponentPlugin},
        render_resource::Shader,
        Render, RenderApp, RenderSet,
    },
    time::Time,
    transform::{
        components::{GlobalTransform, Transform},
        systems::{propagate_transforms, sync_simple_transforms},
    },
};
use noop::NoopParticleSystem;

mod material;
pub use material::*;
mod pipeline;
use pipeline::InstanceBuffer;
pub use pipeline::ParticleMaterialPlugin;
pub mod shader;
mod sub;
pub use sub::*;
mod buffer;
pub mod trail;
pub mod util;
pub use buffer::*;
use trail::{trail_rendering, TrailParticleSystem};
mod noop;
mod ring_buffer;
pub use ring_buffer::RingBuffer;
mod billboard;
pub use billboard::*;

/// Plugin for `berdicle`.
///
/// Adds support for [`StandardParticle`],
/// other particle materials must be manually added via
/// [`ParticleMaterialPlugin`].
pub struct ParticlePlugin;

impl Plugin for ParticlePlugin {
    fn build(&self, app: &mut bevy::prelude::App) {
        app.world_mut().resource_mut::<Assets<Shader>>().insert(
            &shader::PARTICLE_VERTEX,
            Shader::from_wgsl(shader::SHADER_VERTEX, "berdicle/particle_vertex.wgsl"),
        );
        app.world_mut().resource_mut::<Assets<Shader>>().insert(
            &shader::PARTICLE_FRAGMENT,
            Shader::from_wgsl(shader::SHADER_FRAGMENT, "berdicle/particle_fragment.wgsl"),
        );
        app.world_mut().resource_mut::<Assets<Shader>>().insert(
            &shader::PARTICLE_DBG_FRAGMENT,
            Shader::from_wgsl(shader::SHADER_DBG, "berdicle/particle_dbg_fragment.wgsl"),
        );
        app.add_plugins(ExtractComponentPlugin::<ExtractedParticleBuffer>::default());
        app.add_plugins(ExtractComponentPlugin::<ParticleRef>::default());
        app.add_plugins(ParticleMaterialPlugin::<StandardParticle>::default());
        app.add_plugins(ParticleMaterialPlugin::<DebugParticle>::default());
        app.add_systems(Update, particle_system);
        app.add_systems(Update, trail_rendering.after(particle_system));
        app.add_systems(
            PostUpdate,
            billboard_system
                .after(propagate_transforms)
                .after(sync_simple_transforms),
        );
        app.sub_app_mut(RenderApp).add_systems(
            Render,
            particle_ref_system.in_set(RenderSet::PrepareResourcesFlush),
        );
    }
}

/// The main system of `berdicle`, runs in [`Update`].
pub fn particle_system(
    time: Res<Time>,
    mut particles: Query<(
        Entity,
        &mut ParticleInstance,
        &mut ParticleBuffer,
        Option<&mut ParticleEventBuffer>,
        Option<&ParticleParent>,
    )>,
) {
    let dt = time.delta_seconds();
    particles
        .par_iter_mut()
        .for_each(|(_, mut system, mut buffer, events, _)| {
            if buffer.is_uninit() {
                *buffer = system.spawn_particle_buffer();
            }
            if let Some(mut events) = events {
                events.clear();
                system.update_with_event_buffer(dt, &mut buffer, &mut events);
            } else {
                system.update(dt, &mut buffer);
            }
        });

    // Safety: parent is checked to not be the same entity.
    for (entity, mut system, mut buffer, _, parent) in unsafe { particles.iter_unsafe() } {
        let Some(ParticleParent(parent)) = parent else {
            continue;
        };
        if entity == *parent {
            panic!("ParticleSystem's parent cannot be itself.")
        }
        if let Some(sub) = system.as_sub_particle_system() {
            // Safety: parent is checked to not be the same entity.
            let Ok((_, _, mut parent, _, _)) = (unsafe { particles.get_unchecked(*parent) }) else {
                continue;
            };
            sub.spawn_from_parent(dt, &mut buffer, &mut parent);
        }
        if let Some(sub) = system.as_event_particle_system() {
            let Ok((_, _, _, Some(parent), _)) = particles.get(*parent) else {
                continue;
            };
            sub.spawn_on_event(&mut buffer, parent);
        }
    }
}

fn sort_unstable<T>(buf: &mut [T], mut key: impl FnMut(&T) -> bool) {
    if buf.len() < 2 {
        return;
    }
    let mut start = 0;
    let mut end = buf.len() - 1;
    while start < end {
        if key(&buf[start]) {
            while key(&buf[end]) && end > 0 {
                end -= 1;
            }
            if start < end {
                buf.swap(start, end)
            }
        }
        start += 1;
    }
}

/// If and how a particle has expired.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExpirationState {
    None,
    Fizzle,
    Explode,
}

impl ExpirationState {
    pub const fn is_expired(&self) -> bool {
        !matches!(self, Self::None)
    }

    /// Returns [`ExpirationState::Fizzle`] if true.
    pub const fn fizzle_if(&self, cond: bool) -> Self {
        if cond {
            Self::Fizzle
        } else {
            Self::None
        }
    }

    /// Returns [`ExpirationState::Explode`] if true.
    pub const fn explode_if(&self, cond: bool) -> Self {
        if cond {
            Self::Explode
        } else {
            Self::None
        }
    }
}

/// A [`Particle`]. Must be [`Copy`] and have alignment less than `16`.
pub trait Particle: Copy + 'static {
    /// Obtain the seed used to generate the particle.
    fn get_seed(&self) -> f32;
    /// Obtain the index of the particle inserted, optional.
    fn get_index(&self) -> u32 {
        0
    }
    /// Obtain the time span for which the particle is alive.
    fn get_lifetime(&self) -> f32;
    /// Obtain a value, usually normalized lifetime in `0.0..=1.0`, but not a requirement.
    fn get_fac(&self) -> f32 {
        self.get_lifetime()
    }
    /// Obtain the transform of the particle.
    fn get_transform(&self) -> Transform;
    /// Obtain the translation of the particle.
    fn get_position(&self) -> Vec3 {
        self.get_transform().translation
    }
    /// Obtain the tangent of the particle. Default to [`Transform::forward`].
    fn get_tangent(&self) -> Vec3 {
        self.get_transform().forward().as_vec3()
    }
    /// Obtain the color of the particle.
    fn get_color(&self) -> Srgba {
        Srgba::WHITE
    }

    /// Advance time on this particle.
    fn update(&mut self, dt: f32);

    /// Update and write events to a buffer.
    fn update_with_event_buffer(&mut self, dt: f32, buffer: &mut ParticleEventBuffer) {
        if self.is_expired() {
            return;
        }
        self.update(dt);
        let expr = self.expiration_state();
        if expr.is_expired() {
            buffer.push(ParticleEvent {
                event: expr.into(),
                seed: self.get_seed(),
                lifetime: self.get_lifetime(),
                position: self.get_position(),
                tangent: self.get_tangent(),
            })
        }
    }

    /// Obtain if and how this particle has expired.
    ///
    /// This can be written to a particle event queue.
    fn expiration_state(&self) -> ExpirationState;

    /// Returns true if the particle has expired.
    fn is_expired(&self) -> bool {
        self.expiration_state().is_expired()
    }
}

/// A particle spawner type.
pub trait ParticleSystem {
    /// If true, ignore [`Transform`] and [`GlobalTransform`].
    const WORLD_SPACE: bool = false;

    /// Changes what strategy to use when cleaning up used particles.
    ///
    /// * Retain(default): Remove expired particles by moving alive particles in front.
    /// * RingBuffer: Particles are not removed explicitly, but can expire and be reused later.
    /// Should only be used if lifetime is constant and capacity is well predicted.
    ///
    /// If rendering trails using ring buffer, capacity for detached trails should be reserved.
    const STRATEGY: ParticleBufferStrategy = ParticleBufferStrategy::Retain;

    /// Particle type of the system.
    ///
    /// # Panics
    ///
    /// If alignment is not in `1`, `2`, `4`, `8` or `16`.
    type Particle: Particle;

    /// Obtain debug information.
    ///
    /// If not specified, `Debug` will only print a generic struct.
    fn as_debug(&self) -> &dyn Debug {
        #[derive(Debug)]
        pub struct ErasedParticleSystem;
        &ErasedParticleSystem
    }

    /// Obtain the capacity of the buffer, this value is read once upon initialization
    /// and will not be changed during simulation.
    ///
    /// We might increment this value by a little bit for alignment.
    fn capacity(&self) -> usize;

    /// Generate a random `0.0..=1.0` number as a seed.
    fn rng(&mut self) -> f32 {
        fastrand::f32()
    }

    /// Determines how many particles to spawn when a time step passes.
    ///
    /// If used as a sub particle system, this step will still be called,
    /// set this to 0 if not needed.
    fn spawn_step(&mut self, time: f32) -> usize;

    /// Convert a random seed into a particle.
    ///
    /// If `spawn_step` is always `0`,
    /// it's safe to implement with [`unreachable!`].
    fn build_particle(&self, seed: f32) -> Self::Particle;

    /// Additional actions to perform during update.
    ///
    /// if rendering trails using `Retain`, we should call [`ParticleBuffer::update_detached`] here.
    #[allow(unused_variables)]
    fn on_update(&mut self, dt: f32, buffer: &mut ParticleBuffer) {}

    /// If rendering trails using `Retain`, call [`ParticleBuffer::detach_slice`].
    #[allow(unused_variables)]
    fn detach_slice(&mut self, detached: Range<usize>, buffer: &mut ParticleBuffer) {}

    /// Perform a meta action on the ParticleSystem.
    ///
    /// Since [`ParticleInstance`] is type erased, this is the standard way to modify
    /// a [`ParticleSystem`] at runtime.
    ///
    /// # Example
    ///
    /// Change the spawner's transform matrix when `GlobalTransform` is changed.
    ///
    /// ```
    /// # /*
    /// fn apply_meta(&mut self, command: &dyn Any) {
    ///     if let Some(transform) = command.downcast_ref::<GlobalTransform>() {
    ///         self.transform_matrix = (*transform).into();
    ///     }
    /// }
    ///
    /// fn system_sync_transform_with_emitter(
    ///     mut query: Query<(&mut ParticleInstance, &GlobalTransform), Changed<GlobalTransform>>
    /// ) {
    ///     for (mut particle, transform) in query {
    ///         particle.apply_meta(transform);
    ///     }
    /// }
    /// # */
    /// ```
    #[allow(unused_variables)]
    fn apply_meta(&mut self, command: &dyn Any) {}

    /// Downcast into a [`SubParticleSystem`].
    fn as_sub_particle_system(&mut self) -> Option<&mut dyn ErasedSubParticleSystem> {
        None
    }

    /// Downcast into a [`EventParticleSystem`].
    fn as_event_particle_system(&mut self) -> Option<&mut dyn ErasedEventParticleSystem> {
        None
    }

    /// Downcast into a [`TrailParticleSystem`], requires `Self::Particle` to be a [`TrailedParticle`](trail::TrailedParticle).
    fn as_trail_particle_system(&mut self) -> Option<&mut dyn TrailParticleSystem> {
        None
    }
}

/// Type erased version of [`ParticleSystem`].
pub trait ErasedParticleSystem: Send + Sync {
    /// Obtain debug information.
    ///
    /// If not specified, `Debug` will only print a generic struct.
    fn as_debug(&self) -> &dyn Debug;
    /// Convert to [`Any`].
    fn as_any(&self) -> &dyn Any;
    /// Convert to mutable [`Any`].
    fn as_any_mut(&mut self) -> &mut dyn Any;
    /// Returns [`ParticleSystem::WORLD_SPACE`].
    fn is_world_space(&self) -> bool;
    /// Advance by time.
    fn update(&mut self, dt: f32, buffer: &mut ParticleBuffer);
    /// Advance by time, write to an event buffer.
    fn update_with_event_buffer(
        &mut self,
        dt: f32,
        buffer: &mut ParticleBuffer,
        events: &mut ParticleEventBuffer,
    );
    /// Create an empty [`ParticleBuffer`].
    fn spawn_particle_buffer(&self) -> ParticleBuffer;
    /// Perform a meta action on the ParticleSystem.
    fn apply_meta(&mut self, command: &dyn Any);
    fn extract(
        &self,
        buffer: &ParticleBuffer,
        transform: &GlobalTransform,
        billboard: Option<Quat>,
        vec: &mut Vec<ExtractedParticle>,
    );
    /// Downcast into a [`SubParticleSystem`];
    fn as_sub_particle_system(&mut self) -> Option<&mut dyn ErasedSubParticleSystem>;
    /// Downcast into a [`EventParticleSystem`];
    fn as_event_particle_system(&mut self) -> Option<&mut dyn ErasedEventParticleSystem>;
    /// Downcast into a [`TrailParticleSystem`];
    fn as_trail_particle_system(&mut self) -> Option<&mut dyn TrailParticleSystem>;
}

/// Component form of a type erased [`ParticleSystem`].
#[derive(Debug, Component)]
pub struct ParticleInstance(Box<dyn ErasedParticleSystem>);

impl Default for ParticleInstance {
    fn default() -> Self {
        ParticleInstance::new(NoopParticleSystem)
    }
}

impl ParticleInstance {
    pub fn new<P: ParticleSystem + Send + Sync + 'static>(particles: P) -> Self {
        Self(Box::new(particles))
    }

    /// Try obtain a [`ParticleSystem`] by downcasting.
    pub fn downcast_ref<P: ParticleSystem + Send + Sync + 'static>(&self) -> Option<&P> {
        self.0.as_any().downcast_ref()
    }

    /// Try obtain a mutable [`ParticleSystem`] by downcasting.
    ///
    /// Alternatively use [`ParticleSystem::apply_meta`].
    pub fn downcast_mut<P: ParticleSystem + Send + Sync + 'static>(&mut self) -> Option<&mut P> {
        self.0.as_any_mut().downcast_mut()
    }
}

impl Deref for ParticleInstance {
    type Target = dyn ErasedParticleSystem;

    fn deref(&self) -> &Self::Target {
        self.0.as_ref()
    }
}

impl DerefMut for ParticleInstance {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.0.as_mut()
    }
}

fn spawn_particle<T: ParticleSystem>(particles: &mut T) -> T::Particle {
    let seed = particles.rng();
    particles.build_particle(seed)
}

impl<T> ErasedParticleSystem for T
where
    T: ParticleSystem + Send + Sync + 'static,
{
    fn as_debug(&self) -> &dyn Debug {
        ParticleSystem::as_debug(self)
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn is_world_space(&self) -> bool {
        T::WORLD_SPACE
    }

    fn update(&mut self, dt: f32, buffer: &mut ParticleBuffer) {
        match Self::STRATEGY {
            ParticleBufferStrategy::Retain => {
                let original_len = buffer.len;
                let buf = buffer.get_mut::<T::Particle>();
                let mut len = 0;
                for item in buf.iter_mut() {
                    item.update(dt);
                    len += (!item.is_expired()) as usize
                }
                if len != original_len {
                    sort_unstable(buf, |x| x.is_expired());
                    self.detach_slice(len..original_len, buffer)
                }
                buffer.len = len;
                buffer.extend((0..self.spawn_step(dt)).map(|_| spawn_particle(self)))
            }
            ParticleBufferStrategy::RingBuffer => {
                let buf = buffer.get_mut::<T::Particle>();
                let mut len = 0;
                for item in buf {
                    item.update(dt);
                    len += (!item.is_expired()) as usize
                }
                buffer.len = len;
                buffer.extend((0..self.spawn_step(dt)).map(|_| spawn_particle(self)))
            }
        }
        self.on_update(dt, buffer)
    }

    fn update_with_event_buffer(
        &mut self,
        dt: f32,
        buffer: &mut ParticleBuffer,
        events: &mut ParticleEventBuffer,
    ) {
        match Self::STRATEGY {
            ParticleBufferStrategy::Retain => {
                let original_len = buffer.len;
                let buf = buffer.get_mut::<T::Particle>();
                let mut len = 0;
                for item in buf.iter_mut() {
                    item.update_with_event_buffer(dt, events);
                    len += (!item.is_expired()) as usize
                }
                if len != original_len {
                    sort_unstable(buf, |x| x.is_expired());
                    self.detach_slice(len..original_len, buffer)
                }
                buffer.len = len;
                buffer.extend((0..self.spawn_step(dt)).map(|_| spawn_particle(self)))
            }
            ParticleBufferStrategy::RingBuffer => {
                let buf = buffer.get_mut::<T::Particle>();
                let mut len = 0;
                for item in buf {
                    item.update_with_event_buffer(dt, events);
                    len += (!item.is_expired()) as usize
                }
                buffer.len = len;
                buffer.extend((0..self.spawn_step(dt)).map(|_| spawn_particle(self)))
            }
        }
        self.on_update(dt, buffer)
    }

    fn spawn_particle_buffer(&self) -> ParticleBuffer {
        match Self::STRATEGY {
            ParticleBufferStrategy::Retain => {
                ParticleBuffer::new_retain::<T::Particle>(self.capacity())
            }
            ParticleBufferStrategy::RingBuffer => {
                ParticleBuffer::new_ring::<T::Particle>(self.capacity())
            }
        }
    }

    fn apply_meta(&mut self, command: &dyn Any) {
        ParticleSystem::apply_meta(self, command)
    }

    #[allow(clippy::collapsible_else_if)]
    fn extract(
        &self,
        buffer: &ParticleBuffer,
        transform: &GlobalTransform,
        billboard: Option<Quat>,
        vec: &mut Vec<ExtractedParticle>,
    ) {
        vec.clear();
        vec.extend(
            buffer
                .get::<T::Particle>()
                .iter()
                .filter(|x| !x.is_expired())
                .map(|x| {
                    let transform = if let Some(bb) = billboard {
                        if T::WORLD_SPACE {
                            x.get_transform().with_rotation(bb).compute_matrix()
                        } else {
                            let (scale, _, translation) = transform
                                .mul_transform(x.get_transform())
                                .to_scale_rotation_translation();
                            Transform {
                                translation,
                                rotation: bb,
                                scale,
                            }
                            .compute_matrix()
                        }
                    } else {
                        if T::WORLD_SPACE {
                            x.get_transform().compute_matrix()
                        } else {
                            transform.mul_transform(x.get_transform()).compute_matrix()
                        }
                    };
                    ExtractedParticle {
                        index: x.get_index(),
                        lifetime: x.get_lifetime(),
                        seed: x.get_seed(),
                        fac: x.get_fac(),
                        color: x.get_color().to_vec4(),
                        transform_x: transform.row(0),
                        transform_y: transform.row(1),
                        transform_z: transform.row(2),
                    }
                }),
        )
    }

    fn as_sub_particle_system(&mut self) -> Option<&mut dyn ErasedSubParticleSystem> {
        ParticleSystem::as_sub_particle_system(self)
    }

    fn as_event_particle_system(&mut self) -> Option<&mut dyn ErasedEventParticleSystem> {
        ParticleSystem::as_event_particle_system(self)
    }

    fn as_trail_particle_system(&mut self) -> Option<&mut dyn TrailParticleSystem> {
        ParticleSystem::as_trail_particle_system(self)
    }
}

impl Debug for dyn ErasedParticleSystem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_debug().fmt(f)
    }
}

impl ExtractComponent for ExtractedParticleBuffer {
    type QueryData = (
        &'static ParticleInstance,
        &'static ParticleBuffer,
        &'static GlobalTransform,
        Option<&'static BillboardParticle>,
    );
    type QueryFilter = ();
    type Out = ExtractedParticleBuffer;

    fn extract_component(
        (system, buffer, transform, billboard): QueryItem<'_, Self::QueryData>,
    ) -> Option<Self::Out> {
        let mut lock = buffer.extracted_allocation.lock().unwrap();
        if let Some(vec) = Arc::get_mut(&mut lock) {
            system.extract(buffer, transform, billboard.map(|x| x.0), vec);
            Some(ExtractedParticleBuffer(lock.clone()))
        } else {
            None
        }
    }
}

impl ExtractComponent for ParticleRef {
    type QueryData = &'static ParticleRef;
    type QueryFilter = ();
    type Out = ParticleRef;

    fn extract_component(r: QueryItem<'_, Self::QueryData>) -> Option<Self::Out> {
        Some(*r)
    }
}

/// Create a cheap copy of a [`ParticleInstance`]'s output
/// to use with a different set of material and mesh.
///
/// See also [`ParticleRefBundle`].
#[derive(Debug, Component, Clone, Copy)]
pub struct ParticleRef(pub Entity);

impl Default for ParticleRef {
    fn default() -> Self {
        ParticleRef(Entity::PLACEHOLDER)
    }
}

fn particle_ref_system(
    mut commands: Commands,
    particles: Query<&InstanceBuffer>,
    query: Query<(Entity, &ParticleRef)>,
) {
    for (entity, ParticleRef(parent)) in &query {
        if let Ok(buffer) = particles.get(*parent) {
            commands.entity(entity).insert(buffer.clone());
        }
    }
}