bevy_ecs_tilemap: Tiles probably should not be "children" of tilemaps

There are a few reasons one might make use of Bevy’s hierarchy:

  1. Transform propagation
  2. Visibility propagation
  3. 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)

Most upvoted comments

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.

  • A naive bevy hierarchy of sprites
  • A hierarchy-less tilemap
  • A hierarchy-full tilemap (perhaps also with Visibility for tiles)

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:

  • Hierarchies are useful
  • Transforms are useful (ex: placing colliders, even in the collider-merging case)
  • Easy recursive despawns are a desired feature
  • There might be performance wins to discover

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:

let map_size = TilemapSize { x: 32, y: 32 };
commands.spawn((
    Tilemap,
    map_size,
    TilemapTileSize { x: 16.0, y: 16.0 },
    TilemapTexture::Single(texture_handle),
)).with_related::<TileOf>(|t| {
    for x in 0..map_size.x {
        for y in 0..map_size.y {
            t.spawn(TilePos { x, y });
        }
    }
});

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:

// Before
let tile_pos = TilePos { x, y };
let tile_entity = commands
    .spawn(TileBundle {
        position: tile_pos,
        tilemap_id: TilemapId(tilemap_entity),
        ..Default::default()
    })
    .id();
tile_storage.set(&tile_pos, tile_entity);


// After
commands.spawn((
    TileOf(tilemap_entity),
    TilePos { x, y }
));

In this case, TileOf and the corresponding Tilemap relationship target would replace TilemapId and TilemapStorage. 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.

I believe it already works ? 😃

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 TileStorage that has no perf implications.

I’d be very interested to see how this solution performs. It may work thanks to retained rendering.

Sprite performance is actually significantly worse in 0.15. I haven’t done this sort of comparison with bevy_ecs_tilemap since 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.