diff --git a/crates/bevy_sprite/src/mesh2d/color_material.wgsl b/crates/bevy_sprite/src/mesh2d/color_material.wgsl index 3b6a2b5217176..3b5893d4ce285 100644 --- a/crates/bevy_sprite/src/mesh2d/color_material.wgsl +++ b/crates/bevy_sprite/src/mesh2d/color_material.wgsl @@ -26,12 +26,11 @@ struct FragmentInput { @fragment fn fragment(in: FragmentInput) -> @location(0) vec4 { var output_color: vec4 = 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; } diff --git a/examples/2d/mesh2d_vertex_color_texture.rs b/examples/2d/mesh2d_vertex_color_texture.rs index de3ec5c4491a1..1633c2f736c3f 100644 --- a/examples/2d/mesh2d_vertex_color_texture.rs +++ b/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() @@ -29,11 +32,26 @@ 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::from_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::from_translation(Vec3::new(96., 0., 0.)) + .with_scale(Vec3::splat(128.)), material: materials.add(ColorMaterial::from(texture_handle)), ..default() });