|
1 | | -# Decorators |
| 1 | +## Performance Warning: Decorators vs Effects |
2 | 2 |
|
3 | | -**Decorators** are classes that can encapsulate certain visual effects and then apply those visual |
4 | | -effects to a sequence of canvas drawing operations. Decorators are not [Component]s, but they can |
5 | | -be applied to components either manually or via the [HasDecorator] mixin. Likewise, decorators are |
6 | | -not [Effect]s, although they can be used to implement certain `Effect`s. |
| 3 | +Applying a [Decorator] to a component can have a significant performance overhead, especially when |
| 4 | +it involves `canvas.saveLayer()`. |
7 | 5 |
|
8 | | -There are a certain number of decorators available in Flame, and it is simple to add one's own if |
9 | | -necessary. We are planning to add shader-based decorators once Flutter fully supports them on the |
10 | | -web. |
| 6 | +- **Decorators** (e.g., [HueDecorator]): Use `canvas.saveLayer()` to isolate rendering and apply |
| 7 | + filters. This requires off-screen buffer allocation and GPU context switches. In high-density |
| 8 | + scenes (100+ sprites), this can be significantly slower than direct paint manipulation. |
| 9 | +- **Effects** (e.g., [HueEffect]): Modify the component's [Paint] directly. These are |
| 10 | + hardware-accelerated in the fragment shader with virtually zero memory overhead. |
| 11 | + |
| 12 | +**Recommendation**: Always prefer **Effects** for simple color transformations on large numbers of |
| 13 | +units. Use **Decorators** only when you need complex visual composition or when targeting raw |
| 14 | +canvas drawing that isn't governed by a single [Paint]. |
11 | 15 |
|
12 | 16 |
|
13 | 17 | ## Flame built-in decorators |
@@ -157,6 +161,29 @@ limitation is that the shadows are flat and cannot interact with the environment |
157 | 161 | decorator cannot handle shadows that fall onto walls or other vertical structures. |
158 | 162 |
|
159 | 163 |
|
| 164 | +### HueDecorator |
| 165 | + |
| 166 | +```{flutter-app} |
| 167 | +:sources: ../flame/examples |
| 168 | +:page: decorator_hue |
| 169 | +:show: widget code infobox |
| 170 | +:width: 180 |
| 171 | +:height: 160 |
| 172 | +``` |
| 173 | + |
| 174 | +This decorator shifts the hue of the underlying component by the specified angle in radians. |
| 175 | + |
| 176 | +```dart |
| 177 | +final decorator = HueDecorator(tau / 4); |
| 178 | +``` |
| 179 | + |
| 180 | +Possible uses: |
| 181 | + |
| 182 | +- alternative color schemes for enemies ("palette swapping"); |
| 183 | +- environmental changes (e.g., world turning purple/surreal); |
| 184 | +- power-up indicators. |
| 185 | + |
| 186 | + |
160 | 187 | ## Using decorators |
161 | 188 |
|
162 | 189 |
|
|
0 commit comments