bevy_ecs_tilemap: Tiles probably should not be "children" of tilemaps
There are a few reasons one might make use of Bevy’s hierarchy:
- Transform propagation
- Visibility propagation
- Recursive despawns
I am reasonably sure that 1 and 2 are not applicable to bevy_ecs_tilemap. Tiles do not have transforms and visibility propagation to tiles is not needed – tilemaps that are not visible will not be rendered.
Using bevy’s hierarchy comes with a performance penalty because due to visibility propagation work being done. (Also, prior to 0.15, transform propagation affected us too. See https://github.com/bevyengine/bevy/pull/14384)
That just leaves 3. The relationship between a tilemap and its tiles is already defined in the tilemap’s TileStorage, so using Bevy’s hierarchy is just duplicating this data. Instead of using recursive despawns, we could add an OnRemove hook that drains the TileStorage and despawns its entities.
I saw this show up in profiling, but I’m writing this issue from memory and it has been a few weeks. I don’t remember how significant this was.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 15 (4 by maintainers)
Since people are trying things for the hierarchy-related comments here, its worth noting that if you (the reader) are going to explore changes to hierarchy, etc then the primary next step is to get some benchmarks. Repeating a previous comment, benchmarks for the scenarios below are a good start. Then we will have some information on which to compare alternative approaches.
The one thing I don’t want to do is guess. There are mesh2d improvements planned in bevy (to replace the sprite implementation with mesh2d one day), and transform propagation has had major improvements in 0.15 and 0.16. So if we make a performance-related decision on feature-set here I would like to do so with benchmarks.
Summarizing the previous discussion:
Its also worth noting that there is some movement on a built-in tilemap rendering abstraction in Bevy upstream. tbd on how that will affect ecs_tilemap but (IMO) the intent would be to align and build on that if we can.
This also looks really nice:
Weighing in late here, but I wonder if a custom relationship along with required components would work. I’m thinking something along the lines of:
In this case,
TileOfand the correspondingTilemaprelationship target would replaceTilemapIdandTilemapStorage. I’m not sure about visibility and transform propagation. I’m also not sure if it’s possible to replace TilemapStorage with a RelationshipTarget because of the TilePos grid storage, but it’d be neat if it worked.Well, yes, if you place all of your tiles in a hierarchy.
My big question is “do we want to teach Bevy users to do this universally?” Because we could easily provide a mechanism for this based on
TileStoragethat has no perf implications.Sprite performance is actually significantly worse in 0.15. I haven’t done this sort of comparison with
bevy_ecs_tilemapsince before 0.12 though, which is when we started using instancing for sprites, which was pretty major.related to this, we should probably add an example of how we’d expect people to add things like colliders to the right locations relating to the tilemap/tile positioning.