|
| 1 | +# Migration Guide v0.14 -> v0.15 |
| 2 | + |
| 3 | +🎆 Hanabi v0.15 contains a few major API breaking changes. |
| 4 | + |
| 5 | +This guide helps the user migrate from v0.14 to v0.15 of 🎆 Hanabi. |
| 6 | +Users are encouraged to also read the [`CHANGELOG`](../CHANGELOG.md) |
| 7 | +for an exhaustive list of all changes. |
| 8 | + |
| 9 | +## Hierarchical effects |
| 10 | + |
| 11 | +🎆 Hanabi v0.15 supports a new feature called _hierarchical effects_, |
| 12 | +whereby an effect can be parented to another effect. |
| 13 | +Parenting an effect unlocks new features for effect authoring: |
| 14 | + |
| 15 | +- A child effect has read-only access to the particles of its parent, |
| 16 | + and can therefore inherit _e.g._ their position via the new `InheritAttributeModifier`. |
| 17 | +- A child effect's particles can be spawned dynamically on GPU by its parent, |
| 18 | + allowing to "chain" effects. |
| 19 | + For example, the parent can spawn a particle in a child effect |
| 20 | + when one of its own particles dies. |
| 21 | + |
| 22 | +The effect parent/child hierarchy forms a tree (no cycles). |
| 23 | +An effect can declare its parent with the `EffectParent` component, |
| 24 | +and can have a single parent only. |
| 25 | +A parent effect can have multiple children, |
| 26 | +and can itself be a child of a third effect. |
| 27 | + |
| 28 | +## GPU spawn events |
| 29 | + |
| 30 | +Parent effects (see [Hierarchical effects](#hierarchical-effects)) can emit _GPU spawn events_, |
| 31 | +which are GPU-side "events" to spawn particles into one of their child effects. |
| 32 | +With GPU spawn events, child events can be entirely GPU driven, |
| 33 | +and react to the behavior of their parent. |
| 34 | +For example, a GPU spawn event can be emitted when a particle dies. |
| 35 | +This allows _e.g._ a child event to emit an explosion of particles, |
| 36 | +which visually looks in direct relation with the death of the parent's particle. |
| 37 | + |
| 38 | +Particles spawned via GPU spawn events often inherit one or more attribute from their parent, |
| 39 | +via the new `InheritAttributeModifier`. |
| 40 | +This is made possible by the fact that a GPU spawn event contains the ID of the parent particle, |
| 41 | +which allows reading its attributes from the child effect's init pass. |
| 42 | + |
| 43 | +To declare a parent effect emitting GPU spawn events, use: |
| 44 | + |
| 45 | +```rust |
| 46 | +let parent_effect = EffectAsset::new(32, spawner, module) |
| 47 | + .update(EmitSpawnEventModifier { |
| 48 | + condition: EventEmitCondition::OnDie, |
| 49 | + count: 45, |
| 50 | + child_index: 0, |
| 51 | + }); |
| 52 | +let parent_handle = effects.add(parent_effect); |
| 53 | +let parent_entity = commands.spawn(ParticleEffect::new(parent_handle)).id(); |
| 54 | +``` |
| 55 | + |
| 56 | +The child index determines which child effect will consume those emitted events, |
| 57 | +since a parent effect can have multiple children. |
| 58 | + |
| 59 | +For the child effect, which consumes those GPU spawn events, use: |
| 60 | + |
| 61 | +```rust |
| 62 | +let child_effect = EffectAsset::new(250, unused_spawner, module) |
| 63 | + // On spawn, copy the POSITION of the particle which emitted the GPU event |
| 64 | + .init(InheritAttributeModifier::new(Attribute::POSITION)); |
| 65 | +let child_handle = effects.add(child_effect); |
| 66 | +commands.spawn(( |
| 67 | + ParticleEffect::new(child_handle), |
| 68 | + EffectParent(parent_entity), |
| 69 | +)); |
| 70 | +``` |
| 71 | + |
| 72 | +See the updated `firework.rs` example for a full-featured demo. |
| 73 | + |
| 74 | +## New group-less ribbon and trail implementation |
| 75 | + |
| 76 | +Previously in 🎆 Hanabi v0.14, ribbons and trails made use of _groups_, |
| 77 | +which allowed partitioning a particle buffer into sub-buffers, one per group, |
| 78 | +and spawn particles from one group into the other. |
| 79 | + |
| 80 | +If this sounds familiar, this is because that feature is nearly identical to GPU spawn events, |
| 81 | +at least conceptually. |
| 82 | +The API and implementation however were extremely confusing for the user, |
| 83 | +with things like initializing a group particle from the Update pass instead of the Init one, |
| 84 | +and the inability to use init shaders for those particles. |
| 85 | + |
| 86 | +The new ribbon and trail implementation gets rid of all those restrictions, |
| 87 | +and restores the common patterns established for all effects: |
| 88 | + |
| 89 | +- particles are initialized on spawn in the init pass, via init modifiers. |
| 90 | +- particles are updated every frame while alive in the update pass, via update modifiers. |
| 91 | + |
| 92 | +The entire group feature has been removed. |
| 93 | +Instead, a ribbon or trail is now defined by the `Attribute::RIBBON_ID` assigned to each particle. |
| 94 | +All particles with a same ribbon ID are part of the same ribbon. |
| 95 | +There's no other meaning to that value, so you can use any value that makes sense. |
| 96 | +At runtime, after the update pass, all particles are sorted by their `RIBBON_ID` to group them into ribbons, |
| 97 | +and inside a given ribbon (same ribbon ID) the particles are sorted by their age. |
| 98 | +This sorting not only solves the issue of grouping particles without complex buffer management, |
| 99 | +but also gets rid of the annoying edge cases where a particle in a middle of a ribbon would die, |
| 100 | +leaving a gap in it, with forced the previous implementation to constraint to lifetime of particles |
| 101 | +via an external mechanism instead of using the `Attribute::LIFETIME`. |
| 102 | + |
| 103 | +If you're familiar with trails and ribbons in 🎆 Hanabi v0.14 and earlier, |
| 104 | +you may remember about the `Attribute::PREV` and `Attribute::NEXT`. |
| 105 | +Those attributes were used to chain together particles into trails and ribbons, |
| 106 | +forming a linked list. |
| 107 | +Not only did they use a lot of storage space (twice as much as `RIBBON_ID`), |
| 108 | +the operations on the linked list were difficult to perform atomically, |
| 109 | +and have led to several bugs in the past. |
| 110 | +With 🎆 Hanabi v0.15, those attributes are not used anymore, and are soft-deprected. |
| 111 | +You can continue to use them for any other purpose, |
| 112 | +but they do not have anymore a built-in effect. |
| 113 | + |
| 114 | +To create a trail or ribbon, simply assign the `Attribute::RIBBON_ID`: |
| 115 | + |
| 116 | +```rust |
| 117 | +// Example: single trail/ribbon effect |
| 118 | + |
| 119 | +let init_ribbon_id = SetAttributeModifier { |
| 120 | + attribute: Attribute::RIBBON_ID, |
| 121 | + // We use a constant '0' for all particles; any value works. |
| 122 | + value: writer.lit(0u32).expr(), |
| 123 | +}; |
| 124 | +``` |
| 125 | + |
| 126 | +When using multi-trail / multi-ribbon, each trail/ribbon needs a unique ID. |
| 127 | +You can calculate that value from the parent effect, store it, |
| 128 | +and read it back in the child effect. |
| 129 | + |
| 130 | +```rust |
| 131 | +// Example: multi-trail/multi-ribbon |
| 132 | + |
| 133 | +// In the parent effect: |
| 134 | +let parent_init_ribbon_id = SetAttributeModifier::new( |
| 135 | + Attribute::U32_0, |
| 136 | + // Store a unique value per parent particle, used as ribbon ID in children |
| 137 | + writer.attr(Attribute::PARTICLE_COUNTER).expr(), |
| 138 | +); |
| 139 | + |
| 140 | +// In the child effect: |
| 141 | +let child_init_ribbon_id = SetAttributeModifier { |
| 142 | + attribute: Attribute::RIBBON_ID, |
| 143 | + // Read back the unique value from the parent particle |
| 144 | + value: writer.parent_attr(Attribute::U32_0).expr(), |
| 145 | +}; |
| 146 | +``` |
| 147 | + |
| 148 | +See the updated `ribbon.rs` example for a full-featured demo of a single ribbon, |
| 149 | +and the `worms.rs` and `firework.rs` examples |
| 150 | +for an example of combining hierarchical effects and ribbons, |
| 151 | +and use multiple ribbons in the same effect. |
0 commit comments