Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Make vertex colors work without textures in bevy_sprite #5685

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions crates/bevy_sprite/src/mesh2d/color_material.wgsl
Expand Up @@ -26,12 +26,11 @@ struct FragmentInput {
@fragment
fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
var output_color: vec4<f32> = material.color;
if ((material.flags & COLOR_MATERIAL_FLAGS_TEXTURE_BIT) != 0u) {
#ifdef VERTEX_COLORS
output_color = output_color * textureSample(texture, texture_sampler, in.uv) * in.color;
#else
output_color = output_color * textureSample(texture, texture_sampler, in.uv);
output_color = output_color * in.color;
#endif
if ((material.flags & COLOR_MATERIAL_FLAGS_TEXTURE_BIT) != 0u) {
output_color = output_color * textureSample(texture, texture_sampler, in.uv);
}
return output_color;
}
28 changes: 24 additions & 4 deletions examples/2d/mesh2d_vertex_color_texture.rs
@@ -1,7 +1,10 @@
//! 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};
use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};

fn main() {
App::new()
Expand Down Expand Up @@ -29,11 +32,28 @@ fn setup(
];
// Insert the vertex colors as an attribute
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, vertex_colors);
// Spawn

let mesh_handle: Mesh2dHandle = meshes.add(mesh).into();

// Spawn camera
commands.spawn_bundle(Camera2dBundle::default());

// Spawn the quad with vertex colors
commands.spawn_bundle(MaterialMesh2dBundle {
mesh: mesh_handle.clone(),
transform: Transform::default()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use from_translation.
Same for below :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint. Changed it.

.with_translation(Vec3::new(-96., 0., 0.))
.with_scale(Vec3::splat(128.)),
material: materials.add(ColorMaterial::default()),
..default()
});

// Spawning the quad with vertex colors and a texture results in tinting
commands.spawn_bundle(MaterialMesh2dBundle {
mesh: meshes.add(mesh).into(),
transform: Transform::default().with_scale(Vec3::splat(128.)),
mesh: mesh_handle,
transform: Transform::default()
.with_translation(Vec3::new(96., 0., 0.))
.with_scale(Vec3::splat(128.)),
material: materials.add(ColorMaterial::from(texture_handle)),
..default()
});
Expand Down