From 5a747b2786bd64c75b327c99cbd1eb15f592873c Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Sun, 14 Aug 2022 09:24:21 +0000 Subject: [PATCH 1/5] Make vertex colors work without textures --- crates/bevy_sprite/src/mesh2d/color_material.wgsl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/bevy_sprite/src/mesh2d/color_material.wgsl b/crates/bevy_sprite/src/mesh2d/color_material.wgsl index 3b6a2b5217176..ad882be95c1fa 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; + #ifdef VERTEX_COLORS + output_color = output_color * in.color; + #endif 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); -#endif } return output_color; } From eff1e68c2315d16542e6dc7dfabf3dba53f10b8f Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Sun, 14 Aug 2022 09:26:46 +0000 Subject: [PATCH 2/5] Extend mesh2d_vertex_color_texture example --- examples/2d/mesh2d_vertex_color_texture.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/examples/2d/mesh2d_vertex_color_texture.rs b/examples/2d/mesh2d_vertex_color_texture.rs index de3ec5c4491a1..42217d4975651 100644 --- a/examples/2d/mesh2d_vertex_color_texture.rs +++ b/examples/2d/mesh2d_vertex_color_texture.rs @@ -29,11 +29,26 @@ fn setup( ]; // Insert the vertex colors as an attribute mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, vertex_colors); + // Spawn commands.spawn_bundle(Camera2dBundle::default()); + + // Just vertex colors + commands.spawn_bundle(MaterialMesh2dBundle { + mesh: meshes.add(mesh.clone()).into(), + transform: Transform::default() + .with_translation(Vec3::new(-96., 0., 0.)) + .with_scale(Vec3::splat(128.)), + material: materials.add(ColorMaterial::default()), + ..default() + }); + + // Combining 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.)), + transform: Transform::default() + .with_translation(Vec3::new(96., 0., 0.)) + .with_scale(Vec3::splat(128.)), material: materials.add(ColorMaterial::from(texture_handle)), ..default() }); From 6e5bbbf87b7e9bf62e7242d0684342ad5933ad89 Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Sun, 14 Aug 2022 10:11:48 +0000 Subject: [PATCH 3/5] Just clone the handle rather than the mesh --- examples/2d/mesh2d_vertex_color_texture.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/examples/2d/mesh2d_vertex_color_texture.rs b/examples/2d/mesh2d_vertex_color_texture.rs index 42217d4975651..e32011e0e9e75 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() @@ -30,12 +33,14 @@ 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()); - // Just vertex colors + // Spawn the quad with vertex colors commands.spawn_bundle(MaterialMesh2dBundle { - mesh: meshes.add(mesh.clone()).into(), + mesh: mesh_handle.clone(), transform: Transform::default() .with_translation(Vec3::new(-96., 0., 0.)) .with_scale(Vec3::splat(128.)), @@ -43,9 +48,9 @@ fn setup( ..default() }); - // Combining vertex colors and a texture results in tinting + // Spawning the quad with vertex colors and a texture results in tinting commands.spawn_bundle(MaterialMesh2dBundle { - mesh: meshes.add(mesh).into(), + mesh: mesh_handle, transform: Transform::default() .with_translation(Vec3::new(96., 0., 0.)) .with_scale(Vec3::splat(128.)), From 67ff2a8574904b6dceea81063c67de270081c24d Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Sun, 14 Aug 2022 13:22:48 +0000 Subject: [PATCH 4/5] Make indentation consistent --- crates/bevy_sprite/src/mesh2d/color_material.wgsl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_sprite/src/mesh2d/color_material.wgsl b/crates/bevy_sprite/src/mesh2d/color_material.wgsl index ad882be95c1fa..3b5893d4ce285 100644 --- a/crates/bevy_sprite/src/mesh2d/color_material.wgsl +++ b/crates/bevy_sprite/src/mesh2d/color_material.wgsl @@ -26,9 +26,9 @@ struct FragmentInput { @fragment fn fragment(in: FragmentInput) -> @location(0) vec4 { var output_color: vec4 = material.color; - #ifdef VERTEX_COLORS +#ifdef VERTEX_COLORS output_color = output_color * in.color; - #endif +#endif if ((material.flags & COLOR_MATERIAL_FLAGS_TEXTURE_BIT) != 0u) { output_color = output_color * textureSample(texture, texture_sampler, in.uv); } From de2633fa3b81eafebcbfc62eba05d0508762e647 Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Sun, 14 Aug 2022 14:07:03 +0000 Subject: [PATCH 5/5] s/default().with_translation/from_translation/ --- examples/2d/mesh2d_vertex_color_texture.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/2d/mesh2d_vertex_color_texture.rs b/examples/2d/mesh2d_vertex_color_texture.rs index e32011e0e9e75..1633c2f736c3f 100644 --- a/examples/2d/mesh2d_vertex_color_texture.rs +++ b/examples/2d/mesh2d_vertex_color_texture.rs @@ -41,8 +41,7 @@ fn setup( // Spawn the quad with vertex colors commands.spawn_bundle(MaterialMesh2dBundle { mesh: mesh_handle.clone(), - transform: Transform::default() - .with_translation(Vec3::new(-96., 0., 0.)) + transform: Transform::from_translation(Vec3::new(-96., 0., 0.)) .with_scale(Vec3::splat(128.)), material: materials.add(ColorMaterial::default()), ..default() @@ -51,8 +50,7 @@ fn setup( // Spawning the quad with vertex colors and a texture results in tinting commands.spawn_bundle(MaterialMesh2dBundle { mesh: mesh_handle, - transform: Transform::default() - .with_translation(Vec3::new(96., 0., 0.)) + transform: Transform::from_translation(Vec3::new(96., 0., 0.)) .with_scale(Vec3::splat(128.)), material: materials.add(ColorMaterial::from(texture_handle)), ..default()