|
| 1 | +//! Shows how to render a polygonal [`Mesh`], generated from a [`Quad`] primitive, in a 2D scene. |
| 2 | +//! Adds a texture and colored vertices, giving per-vertex tinting. |
| 3 | +
|
| 4 | +use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; |
| 5 | + |
| 6 | +fn main() { |
| 7 | + App::new() |
| 8 | + .add_plugins(DefaultPlugins) |
| 9 | + .add_startup_system(setup) |
| 10 | + .run(); |
| 11 | +} |
| 12 | + |
| 13 | +fn setup( |
| 14 | + mut commands: Commands, |
| 15 | + mut meshes: ResMut<Assets<Mesh>>, |
| 16 | + mut materials: ResMut<Assets<ColorMaterial>>, |
| 17 | + asset_server: Res<AssetServer>, |
| 18 | +) { |
| 19 | + // Load the Bevy logo as a texture |
| 20 | + let texture_handle = asset_server.load("branding/banner.png"); |
| 21 | + // Build a default quad mesh |
| 22 | + let mut mesh = Mesh::from(shape::Quad::default()); |
| 23 | + // Build vertex colors for the quad. One entry per vertex (the corners of the quad) |
| 24 | + let vertex_colors: Vec<[f32; 4]> = vec![ |
| 25 | + Color::RED.as_rgba_f32(), |
| 26 | + Color::GREEN.as_rgba_f32(), |
| 27 | + Color::BLUE.as_rgba_f32(), |
| 28 | + Color::WHITE.as_rgba_f32(), |
| 29 | + ]; |
| 30 | + // Insert the vertex colors as an attribute |
| 31 | + mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, vertex_colors); |
| 32 | + // Spawn |
| 33 | + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); |
| 34 | + commands.spawn_bundle(MaterialMesh2dBundle { |
| 35 | + mesh: meshes.add(mesh).into(), |
| 36 | + transform: Transform::default().with_scale(Vec3::splat(128.)), |
| 37 | + material: materials.add(ColorMaterial::from(texture_handle)), |
| 38 | + ..default() |
| 39 | + }); |
| 40 | +} |
0 commit comments