-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Merged by Bors] - Apply vertex colors to ColorMaterial and Mesh2D #4812
Changes from all commits
2398289
38281a5
2d15c42
9e548f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//! Shows how to render a polygonal [`Mesh`], generated from a [`Quad`] primitive, in a 2D scene. | ||
//! Adds a texture and colored vertices, giving per-vertex tinting. | ||
|
||
use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.add_startup_system(setup) | ||
.run(); | ||
} | ||
|
||
fn setup( | ||
mut commands: Commands, | ||
mut meshes: ResMut<Assets<Mesh>>, | ||
mut materials: ResMut<Assets<ColorMaterial>>, | ||
asset_server: Res<AssetServer>, | ||
) { | ||
// Load the Bevy logo as a texture | ||
let texture_handle = asset_server.load("branding/banner.png"); | ||
// Build a default quad mesh | ||
let mut mesh = Mesh::from(shape::Quad::default()); | ||
// Build vertex colors for the quad. One entry per vertex (the corners of the quad) | ||
let vertex_colors: Vec<[f32; 4]> = vec![ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This matrix is a little inscrutable for beginners. What do these numbers mean? I can guess that they're RGBA, with each row representing another vertex, but beginners won't be able to guess that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, so I've replaced them with a much more readable |
||
Color::RED.as_rgba_f32(), | ||
Color::GREEN.as_rgba_f32(), | ||
Color::BLUE.as_rgba_f32(), | ||
Color::WHITE.as_rgba_f32(), | ||
]; | ||
// Insert the vertex colors as an attribute | ||
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, vertex_colors); | ||
// Spawn | ||
commands.spawn_bundle(OrthographicCameraBundle::new_2d()); | ||
commands.spawn_bundle(MaterialMesh2dBundle { | ||
mesh: meshes.add(mesh).into(), | ||
transform: Transform::default().with_scale(Vec3::splat(128.)), | ||
material: materials.add(ColorMaterial::from(texture_handle)), | ||
..default() | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should mention the colored vertexes presumably.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a second line (the first is copied from the
mesh2d
example). I wasn't sure how to reference the other example, if that's preferred.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I don't think there's a need to crosslink examples. I'm happy with this description now.