diff --git a/Cargo.toml b/Cargo.toml index 3e1220a898..e5402eeb56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,8 +38,6 @@ bevy_ui = { path = "crates/bevy_ui" } bevy_window = { path = "crates/bevy_window" } bevy_wgpu = { path = "crates/bevy_wgpu", optional = true } bevy_winit = { path = "crates/bevy_winit", optional = true } -# other -log = { version = "0.4", features = ["release_max_level_info"] } [dev-dependencies] rand = "0.7.2" @@ -189,10 +187,6 @@ path = "examples/ui/font_atlas_debug.rs" name = "ui" path = "examples/ui/ui.rs" -[[example]] -name = "ui_bench" -path = "examples/ui/ui_bench.rs" - [[example]] name = "clear_color" path = "examples/window/clear_color.rs" diff --git a/crates/bevy_render/src/color.rs b/crates/bevy_render/src/color.rs index 75dbcd388c..1bbad6e1c7 100644 --- a/crates/bevy_render/src/color.rs +++ b/crates/bevy_render/src/color.rs @@ -5,10 +5,10 @@ use crate::{ }; use bevy_asset::Handle; use bevy_core::{Byteable, Bytes}; -use bevy_math::Vec4; +use bevy_math::{Vec3, Vec4}; use bevy_property::Property; use serde::{Deserialize, Serialize}; -use std::ops::{Add, AddAssign}; +use std::ops::{Add, AddAssign, Mul, MulAssign}; #[repr(C)] #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Property)] @@ -95,6 +95,67 @@ impl Into<[f32; 4]> for Color { [self.r, self.g, self.b, self.a] } } +impl Mul for Color { + type Output = Color; + fn mul(self, rhs: f32) -> Self::Output { + Color { + r: self.r * rhs, + g: self.g * rhs, + b: self.b * rhs, + a: self.a * rhs, + } + } +} + +impl MulAssign for Color { + fn mul_assign(&mut self, rhs: f32) { + self.r *= rhs; + self.g *= rhs; + self.b *= rhs; + self.a *= rhs; + } +} + +impl Mul for Color { + type Output = Color; + fn mul(self, rhs: Vec4) -> Self::Output { + Color { + r: self.r * rhs.x(), + g: self.g * rhs.y(), + b: self.b * rhs.z(), + a: self.a * rhs.w(), + } + } +} + +impl MulAssign for Color { + fn mul_assign(&mut self, rhs: Vec4) { + self.r *= rhs.x(); + self.g *= rhs.y(); + self.b *= rhs.z(); + self.a *= rhs.w(); + } +} + +impl Mul for Color { + type Output = Color; + fn mul(self, rhs: Vec3) -> Self::Output { + Color { + r: self.r * rhs.x(), + g: self.g * rhs.y(), + b: self.b * rhs.z(), + a: self.a + } + } +} + +impl MulAssign for Color { + fn mul_assign(&mut self, rhs: Vec3) { + self.r *= rhs.x(); + self.g *= rhs.y(); + self.b *= rhs.z(); + } +} impl Bytes for ColorSource { fn write_bytes(&self, buffer: &mut [u8]) { diff --git a/examples/2d/texture_atlas.rs b/examples/2d/texture_atlas.rs index 6436efedd2..9dec91e324 100644 --- a/examples/2d/texture_atlas.rs +++ b/examples/2d/texture_atlas.rs @@ -4,10 +4,10 @@ use bevy::{ sprite::TextureAtlasBuilder, }; +/// In this example we generate a new texture atlas (sprite sheet) from a folder containing individual sprites fn main() { App::build() .init_resource::() - .init_resource::() .add_default_plugins() .add_startup_system(setup.system()) .add_system(load_atlas.system()) @@ -17,6 +17,7 @@ fn main() { #[derive(Default)] pub struct RpgSpriteHandles { handles: Vec, + atlas_loaded: bool, } fn setup( @@ -27,24 +28,19 @@ fn setup( rpg_sprite_handles.handles = asset_server .load_asset_folder("assets/textures/rpg") .unwrap(); - commands.spawn(Camera2dComponents::default()); -} - -#[derive(Default)] -struct State { - atlas_loaded: bool, + commands + .spawn(Camera2dComponents::default()); } fn load_atlas( mut commands: Commands, - mut state: ResMut, - rpg_sprite_handles: Res, + mut rpg_sprite_handles: ResMut, asset_server: Res, mut texture_atlases: ResMut>, mut textures: ResMut>, mut materials: ResMut>, ) { - if state.atlas_loaded { + if rpg_sprite_handles.atlas_loaded { return; } @@ -65,6 +61,8 @@ fn load_atlas( .unwrap(); let vendor_index = texture_atlas.get_texture_index(vendor_handle).unwrap(); let atlas_handle = texture_atlases.add(texture_atlas); + + // set up a scene to display our texture atlas commands // draw a sprite from the atlas .spawn(SpriteSheetComponents { @@ -81,6 +79,6 @@ fn load_atlas( ..Default::default() }); - state.atlas_loaded = true; + rpg_sprite_handles.atlas_loaded = true; } } diff --git a/examples/3d/3d_scene.rs b/examples/3d/3d_scene.rs index f791d8e05d..e71c0e2eb1 100644 --- a/examples/3d/3d_scene.rs +++ b/examples/3d/3d_scene.rs @@ -7,7 +7,7 @@ fn main() { .run(); } -/// set up a simple scene +/// set up a simple 3D scene fn setup( mut commands: Commands, mut meshes: ResMut>, diff --git a/examples/3d/spawner.rs b/examples/3d/spawner.rs index 9768b80ec8..fa579ec6f7 100644 --- a/examples/3d/spawner.rs +++ b/examples/3d/spawner.rs @@ -4,6 +4,10 @@ use bevy::{ }; use rand::{rngs::StdRng, Rng, SeedableRng}; +/// This example spawns a large number of cubes, each with its own changing position and material +/// This is intended to be a stress test of bevy's ability to render many objects with different properties +/// For the best results, run it in release mode: ```cargo run --example spawner --release +/// NOTE: Bevy still has a number of optimizations to do in this area. Expect the performance here to go way up in the future fn main() { App::build() .add_default_plugins() @@ -22,7 +26,8 @@ fn move_cubes( for (mut translation, material_handle) in &mut query.iter() { let material = materials.get_mut(&material_handle).unwrap(); translation.0 += Vec3::new(1.0, 0.0, 0.0) * time.delta_seconds; - material.albedo += Color::rgb(-time.delta_seconds, -time.delta_seconds, time.delta_seconds); + material.albedo = + Color::BLUE * Vec3::splat((3.0 * time.seconds_since_startup as f32).sin()); } } @@ -32,30 +37,7 @@ fn setup( mut materials: ResMut>, ) { let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 })); - let plane_handle = meshes.add(Mesh::from(shape::Plane { size: 10.0 })); - let cube_material_handle = materials.add(StandardMaterial { - albedo: Color::rgb(0.5, 0.4, 0.3), - ..Default::default() - }); - let plane_material_handle = materials.add(StandardMaterial { - albedo: Color::rgb(0.1, 0.2, 0.1), - ..Default::default() - }); - commands - // plane - .spawn(PbrComponents { - mesh: plane_handle, - material: plane_material_handle, - ..Default::default() - }) - // cube - .spawn(PbrComponents { - mesh: cube_handle, - material: cube_material_handle, - translation: Translation::new(0.0, 0.0, 1.0), - ..Default::default() - }) // light .spawn(LightComponents { translation: Translation::new(4.0, -4.0, 5.0), @@ -64,30 +46,29 @@ fn setup( // camera .spawn(Camera3dComponents { transform: Transform::new_sync_disabled(Mat4::face_toward( - Vec3::new(0.0, 150.0, -15.0), + Vec3::new(0.0, 15.0, 150.0), Vec3::new(0.0, 0.0, 0.0), - Vec3::new(0.0, 1.0, 0.0), + Vec3::new(0.0, 0.0, 1.0), )), ..Default::default() }); let mut rng = StdRng::from_entropy(); for _ in 0..10000 { - let spawned_material_handle = materials.add(StandardMaterial { - albedo: Color::rgb( - rng.gen_range(0.0, 1.0), - rng.gen_range(0.0, 1.0), - rng.gen_range(0.0, 1.0), - ), - ..Default::default() - }); commands.spawn(PbrComponents { mesh: cube_handle, - material: spawned_material_handle, + material: materials.add(StandardMaterial { + albedo: Color::rgb( + rng.gen_range(0.0, 1.0), + rng.gen_range(0.0, 1.0), + rng.gen_range(0.0, 1.0), + ), + ..Default::default() + }), translation: Translation::new( rng.gen_range(-50.0, 50.0), - 0.0, rng.gen_range(-50.0, 50.0), + 0.0, ), ..Default::default() }); diff --git a/examples/3d/z_sort_debug.rs b/examples/3d/z_sort_debug.rs index a2851977ad..20a74a83e6 100644 --- a/examples/3d/z_sort_debug.rs +++ b/examples/3d/z_sort_debug.rs @@ -6,8 +6,7 @@ use bevy::{ }, }; -struct Rotator; - +/// This example visualizes camera z-ordering by setting the material of rotating cubes to their distance from the camera fn main() { App::build() .add_default_plugins() @@ -17,6 +16,8 @@ fn main() { .run(); } +struct Rotator; + /// rotates the parent, which will result in the child also rotating fn rotator_system(time: Res