'Bevy How to Render Triangle (or Polygon) in 2D
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.
Solution 1:[1]
Currently there is no support for 'drawing' in 2D. This is being looked at, but is not there yet.
Solution 2:[2]
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()
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Boris Boutillier |
| Solution 2 | Kirill |
