From 110831150e8228ab96dce8a73123d9f1a1a56a90 Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Tue, 16 Aug 2022 20:46:45 +0000 Subject: [PATCH] Make vertex colors work without textures in bevy_sprite (#5685) # Objective This PR changes it possible to use vertex colors without a texture using the bevy_sprite ColorMaterial. Fixes #5679 ## Solution - Made multiplication of the output color independent of the COLOR_MATERIAL_FLAGS_TEXTURE_BIT bit - Extended mesh2d_vertex_color_texture example to show off both vertex colors and tinting Not sure if extending the existing example was the right call but it seems to be reasonable to me. I couldn't find any tests for the shaders and I think adding shader testing would be beyond the scope of this PR. So no tests in this PR. :grimacing: Co-authored-by: Jonas Wagner --- .../src/mesh2d/color_material.wgsl | 7 +++-- examples/2d/mesh2d_vertex_color_texture.rs | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/crates/bevy_sprite/src/mesh2d/color_material.wgsl b/crates/bevy_sprite/src/mesh2d/color_material.wgsl index 3b6a2b5217..3b5893d4ce 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 de3ec5c449..1633c2f736 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: meshes.add(mesh).into(), - transform: Transform::default().with_scale(Vec3::splat(128.)), + 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: mesh_handle, + transform: Transform::from_translation(Vec3::new(96., 0., 0.)) + .with_scale(Vec3::splat(128.)), material: materials.add(ColorMaterial::from(texture_handle)), ..default() });