Drawing Lines in 2D
Bevy 0.11 added an immediate mode gizmos API that can draw lines.
The immediate mode API is useful for quick debugging, but it has some limitations:
- Gizmos are always drawn on top of other content
- Gizmos aren't retained between frames. You must write code that draws them every frame.
If you want to just "spawn a line segment entity," you can use a sprite! A line segment is just a very skinny rectangle, after all.
Another way to do this would be to create a custom Mesh
. A really handy plugin that takes this approach is bevy_prototype_lyon. It does tesselation with lyon and supports polylines with nice joinery and other arbitrary 2d shapes.
Note
Creating a unique mesh for each line segment you want to draw will be less performant than using sprites, because Bevy won't be able to use automatic batching.
Here's what DIYing it for a simple line segment looks like:
If you need more power, check out these options from the Bevy's third party ecosystem:
- bevy_vector_shapes
- bevy_vello (warning: no webgl2 support)