-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply vertex colors to ColorMaterial and Mesh2D (#4812)
# Objective - Add Vertex Color support to 2D meshes and ColorMaterial. This extends the work from #4528 (which in turn builds on the excellent tangent handling). ## Solution - Added `#ifdef` wrapped support for vertex colors in the 2D mesh shader and `ColorMaterial` shader. - Added an example, `mesh2d_vertex_color_texture` to demonstrate it in action. ![image](https://user-images.githubusercontent.com/14896751/169530930-6ae0c6be-2f69-40e3-a600-ba91d7178bc3.png) --- ## Changelog - Added optional (ifdef wrapped) vertex color support to the 2dmesh and color material systems.
- Loading branch information
1 parent
1bbd5c2
commit a6eb3fa
Showing
5 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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![ | ||
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() | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters