In the bevy examples breakout only uses rectangles, there are examples of loading sprites, there is an example of loading a 3d mesh. In 2d I'd like to draw a triangle (or other polygons), but I haven't been able to figure it out through the docs.
Asked
Active
Viewed 3,360 times
3
-
`bevy_rapier` has a triangle. You could use that, or look at how it is draw. – Will Aug 29 '20 at 21:55
-
3You could use `bevy_prototype_lyon` to draw all kinds of 2D shapes & paths: https://github.com/Nilirad/bevy_prototype_lyon – Paul Götze Oct 15 '20 at 07:40
2 Answers
5
Not sure if that is still relevant, but today I have had the same issue in here is how I have been able to draw simple trianle
fn create_triangle() -> Mesh {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.set_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0]],
);
mesh.set_attribute(Mesh::ATTRIBUTE_COLOR, vec![[0.0, 0.0, 0.0, 1.0]; 3]);
mesh.set_indices(Some(Indices::U32(vec![0, 1, 2])));
mesh
}
This will create a triangle mesh. As for me the tricky part was to figure out that by default triangle is drawn transparent and alfa value should be set for the vertexes. Later you can use this mesh generating function in your system like this:
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>
) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(MaterialMesh2dBundle {
mesh: meshes.add(create_triangle()).into(),
transform: Transform::default().with_scale(Vec3::splat(128.)),
material: materials.add(ColorMaterial::from(Color::PURPLE)),
..Default::default()
});
}

Kirill
- 51
- 1
- 1
4
Currently there is no support for 'drawing' in 2D. This is being looked at, but is not there yet.

Boris Boutillier
- 56
- 2