From 99cdf56e7d59d4f89e995a3d163cbf2cd5e7a6fa Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Mon, 9 Mar 2020 23:43:40 -0700 Subject: [PATCH] add Color type --- examples/custom_shader.rs | 6 +- examples/entity_builder_comparison.rs | 12 ++-- examples/parenting.rs | 4 +- .../plugin_loading/example_plugin/src/lib.rs | 4 +- examples/simple.rs | 4 +- examples/spawner.rs | 16 ++--- examples/ui.rs | 20 +++--- src/prelude.rs | 2 +- src/render/color.rs | 61 ++++++++++++++++++- src/render/light.rs | 7 ++- .../shader/uniforms/standard_material.rs | 4 +- src/ui/node.rs | 9 +-- 12 files changed, 100 insertions(+), 49 deletions(-) diff --git a/examples/custom_shader.rs b/examples/custom_shader.rs index 59c11ad787..228771f714 100644 --- a/examples/custom_shader.rs +++ b/examples/custom_shader.rs @@ -4,7 +4,7 @@ use bevy_derive::Uniforms; #[derive(Uniforms, Default)] struct MyMaterial { - pub color: Vec4, + pub color: Color, #[uniform(ignore, shader_def)] pub always_red: bool, } @@ -79,7 +79,7 @@ fn setup(world: &mut World, resources: &mut Resources) { ..Default::default() }, material: MyMaterial { - color: Vec4::new(0.0, 0.8, 0.0, 1.0), + color: Color::rgb(0.0, 0.8, 0.0).into(), always_red: false, }, translation: Translation::new(-2.0, 0.0, 0.0), @@ -93,7 +93,7 @@ fn setup(world: &mut World, resources: &mut Resources) { ..Default::default() }, material: MyMaterial { - color: Vec4::new(0.0, 0.0, 0.0, 1.0), + color: Color::rgb(0.0, 0.0, 0.0).into(), always_red: true, }, translation: Translation::new(2.0, 0.0, 0.0), diff --git a/examples/entity_builder_comparison.rs b/examples/entity_builder_comparison.rs index b82055f074..6a0ecd83c1 100644 --- a/examples/entity_builder_comparison.rs +++ b/examples/entity_builder_comparison.rs @@ -16,7 +16,7 @@ fn create_entities_insert_vec( vec![( plane_handle, StandardMaterial { - albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(), + albedo: Color::rgb(0.1, 0.2, 0.1).into(), }, LocalToWorld::identity(), Translation::new(0.0, 0.0, 0.0), @@ -29,7 +29,7 @@ fn create_entities_insert_vec( vec![( cube_handle, StandardMaterial { - albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(), + albedo: Color::rgb(0.5, 0.3, 0.3).into(), }, LocalToWorld::identity(), Translation::new(0.0, 0.0, 1.0), @@ -79,7 +79,7 @@ fn create_entities_builder_add_component( .build_entity() .add(plane_handle) .add(StandardMaterial { - albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(), + albedo: Color::rgb(0.1, 0.2, 0.1).into(), }) .add(LocalToWorld::identity()) .add(Translation::new(0.0, 0.0, 0.0)) @@ -87,7 +87,7 @@ fn create_entities_builder_add_component( .build_entity() .add(cube_handle) .add(StandardMaterial { - albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(), + albedo: Color::rgb(0.5, 0.3, 0.3).into(), }) .add(LocalToWorld::identity()) .add(Translation::new(0.0, 0.0, 1.0)) @@ -125,7 +125,7 @@ fn create_entities_builder_archetype( .add_entity(MeshEntity { mesh: plane_handle, material: StandardMaterial { - albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(), + albedo: Color::rgb(0.1, 0.2, 0.1).into(), }, ..Default::default() }) @@ -133,7 +133,7 @@ fn create_entities_builder_archetype( .add_entity(MeshEntity { mesh: cube_handle, material: StandardMaterial { - albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(), + albedo: Color::rgb(0.5, 0.3, 0.3).into(), }, ..Default::default() }) diff --git a/examples/parenting.rs b/examples/parenting.rs index 20b934d1cf..be67830ba9 100644 --- a/examples/parenting.rs +++ b/examples/parenting.rs @@ -32,7 +32,7 @@ fn setup(world: &mut World, resources: &mut Resources) { .add_entity(MeshEntity { mesh: cube_handle, material: StandardMaterial { - albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(), + albedo: Color::rgb(0.5, 0.4, 0.3).into(), }, translation: Translation::new(0.0, 0.0, 1.0), ..Default::default() @@ -43,7 +43,7 @@ fn setup(world: &mut World, resources: &mut Resources) { builder.add_entity(MeshEntity { mesh: cube_handle, material: StandardMaterial { - albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(), + albedo: Color::rgb(0.5, 0.4, 0.3).into(), }, translation: Translation::new(0.0, 0.0, 3.0), ..Default::default() diff --git a/examples/plugin_loading/example_plugin/src/lib.rs b/examples/plugin_loading/example_plugin/src/lib.rs index 3fa1f5b861..616a7d8a47 100644 --- a/examples/plugin_loading/example_plugin/src/lib.rs +++ b/examples/plugin_loading/example_plugin/src/lib.rs @@ -24,7 +24,7 @@ pub fn setup(world: &mut World, resources: &mut Resources) { .add_archetype(MeshEntity { mesh: plane_handle, material: StandardMaterial { - albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(), + albedo: Color::rgb(0.1, 0.2, 0.1).into(), }, ..Default::default() }) @@ -32,7 +32,7 @@ pub fn setup(world: &mut World, resources: &mut Resources) { .add_archetype(MeshEntity { mesh: cube_handle, material: StandardMaterial { - albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(), + albedo: Color::rgb(0.5, 0.4, 0.3).into(), }, translation: Translation::new(0.0, 0.0, 1.0), ..Default::default() diff --git a/examples/simple.rs b/examples/simple.rs index 1474798f85..9e9cc0590b 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -15,7 +15,7 @@ fn setup(world: &mut World, resources: &mut Resources) { .add_entity(MeshEntity { mesh: plane_handle, material: StandardMaterial { - albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(), + albedo: Color::rgb(0.1, 0.2, 0.1).into(), }, ..Default::default() }) @@ -23,7 +23,7 @@ fn setup(world: &mut World, resources: &mut Resources) { .add_entity(MeshEntity { mesh: cube_handle, material: StandardMaterial { - albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(), + albedo: Color::rgb(0.5, 0.4, 0.3).into(), }, translation: Translation::new(0.0, 0.0, 1.0), ..Default::default() diff --git a/examples/spawner.rs b/examples/spawner.rs index 9fd8dc8dc6..d6f0a3d275 100644 --- a/examples/spawner.rs +++ b/examples/spawner.rs @@ -20,12 +20,7 @@ fn build_move_system() -> Box { translation.0 += math::vec3(1.0, 0.0, 0.0) * time.delta_seconds; if let ColorSource::Color(color) = material.albedo { material.albedo = (color - + math::vec4( - -time.delta_seconds, - -time.delta_seconds, - time.delta_seconds, - 0.0, - )) + + Color::rgb(-time.delta_seconds, -time.delta_seconds, time.delta_seconds)) .into(); } } @@ -72,7 +67,7 @@ fn setup(world: &mut World, resources: &mut Resources) { .add_entity(MeshEntity { mesh: plane_handle, material: StandardMaterial { - albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(), + albedo: Color::rgb(0.1, 0.2, 0.1).into(), }, ..Default::default() }) @@ -80,7 +75,7 @@ fn setup(world: &mut World, resources: &mut Resources) { .add_entity(MeshEntity { mesh: cube_handle, material: StandardMaterial { - albedo: math::vec4(1.0, 1.0, 1.0, 1.0).into(), + albedo: Color::rgb(1.0, 1.0, 1.0).into(), }, translation: Translation::new(0.0, 0.0, 1.0), ..Default::default() @@ -88,7 +83,7 @@ fn setup(world: &mut World, resources: &mut Resources) { .add_entity(MeshEntity { mesh: cube_handle, material: StandardMaterial { - albedo: math::vec4(0.0, 1.0, 0.0, 1.0).into(), + albedo: Color::rgb(0.0, 1.0, 0.0).into(), }, translation: Translation::new(-2.0, 0.0, 1.0), ..Default::default() @@ -119,11 +114,10 @@ fn setup(world: &mut World, resources: &mut Resources) { builder = builder.add_entity(MeshEntity { mesh: cube_handle, material: StandardMaterial { - albedo: math::vec4( + albedo: Color::rgb( rng.gen_range(0.0, 1.0), rng.gen_range(0.0, 1.0), rng.gen_range(0.0, 1.0), - 1.0, ) .into(), }, diff --git a/examples/ui.rs b/examples/ui.rs index d334060cbe..cf85e27d5a 100644 --- a/examples/ui.rs +++ b/examples/ui.rs @@ -14,7 +14,7 @@ fn setup(world: &mut World, resources: &mut Resources) { .add_entity(MeshEntity { mesh: cube_handle, material: StandardMaterial { - albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(), + albedo: Color::rgb(0.5, 0.3, 0.3).into(), }, translation: Translation::new(0.0, 0.0, 1.0), ..Default::default() @@ -57,7 +57,7 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(0.0, 0.0), Anchors::new(0.0, 0.0, 0.0, 1.0), Margins::new(10.0, 200.0, 10.0, 10.0), - math::vec4(0.1, 0.1, 0.1, 1.0), + Color::rgb(0.1, 0.1, 0.1), ), }) // top right anchor with vertical fill @@ -66,7 +66,7 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(0.0, 0.0), Anchors::new(1.0, 1.0, 0.0, 1.0), Margins::new(10.0, 100.0, 50.0, 100.0), - math::vec4(0.1, 0.1, 0.1, 1.0), + Color::rgb(0.1, 0.1, 0.1), ), }) // render order test: reddest in the back, whitest in the front @@ -75,7 +75,7 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(75.0, 75.0), Anchors::new(0.5, 0.5, 0.5, 0.5), Margins::new(0.0, 100.0, 0.0, 100.0), - math::vec4(1.0, 0.1, 0.1, 1.0), + Color::rgb(1.0, 0.1, 0.1), ), }) .add_entity(UiEntity { @@ -83,7 +83,7 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(50.0, 50.0), Anchors::new(0.5, 0.5, 0.5, 0.5), Margins::new(0.0, 100.0, 0.0, 100.0), - math::vec4(1.0, 0.3, 0.3, 1.0), + Color::rgb(1.0, 0.3, 0.3), ), }) .add_entity(UiEntity { @@ -91,7 +91,7 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(100.0, 100.0), Anchors::new(0.5, 0.5, 0.5, 0.5), Margins::new(0.0, 100.0, 0.0, 100.0), - math::vec4(1.0, 0.5, 0.5, 1.0), + Color::rgb(1.0, 0.5, 0.5), ), }) .add_entity(UiEntity { @@ -99,7 +99,7 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(150.0, 150.0), Anchors::new(0.5, 0.5, 0.5, 0.5), Margins::new(0.0, 100.0, 0.0, 100.0), - math::vec4(1.0, 0.7, 0.7, 1.0), + Color::rgb(1.0, 0.7, 0.7), ), }) // parenting @@ -108,7 +108,7 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(300.0, 300.0), Anchors::new(0.0, 0.0, 0.0, 0.0), Margins::new(0.0, 200.0, 0.0, 200.0), - math::vec4(0.1, 0.1, 1.0, 1.0), + Color::rgb(0.1, 0.1, 1.0), ), }) .add_children(|builder| { @@ -117,7 +117,7 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(0.0, 0.0), Anchors::new(0.0, 1.0, 0.0, 1.0), Margins::new(20.0, 20.0, 20.0, 20.0), - math::vec4(0.6, 0.6, 1.0, 1.0), + Color::rgb(0.6, 0.6, 1.0), ), }) }) @@ -127,7 +127,7 @@ fn setup(world: &mut World, resources: &mut Resources) { math::vec2(200.0, 200.0), Anchors::new(0.5, 0.5, 0.5, 0.5), Margins::new(0.0, 100.0, 0.0, 100.0), - math::vec4(1.0, 0.9, 0.9, 0.4), + Color::rgba(1.0, 0.9, 0.9, 0.4), ), }) .build(); diff --git a/src/prelude.rs b/src/prelude.rs index 9226e02fd3..9bc33e34ba 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -8,7 +8,7 @@ pub use crate::{ pipeline::PipelineDescriptor, render_resource::{resource_name, resource_providers::UniformResourceProvider}, shader::{uniforms::StandardMaterial, Shader, ShaderDefSuffixProvider, ShaderStage}, - ActiveCamera, ActiveCamera2d, Camera, CameraType, ColorSource, Instanced, Light, + ActiveCamera, ActiveCamera2d, Camera, CameraType, Color, ColorSource, Instanced, Light, Renderable, }, ui::{Anchors, Margins, Node}, diff --git a/src/render/color.rs b/src/render/color.rs index 9f6b185b05..6e16f2f585 100644 --- a/src/render/color.rs +++ b/src/render/color.rs @@ -4,15 +4,70 @@ use crate::{ math::Vec4, render::shader::ShaderDefSuffixProvider, }; +use std::ops::Add; + +#[derive(Debug, Default, Clone, Copy, PartialEq)] +pub struct Color(Vec4); + +impl Color { + pub fn rgb(r: f32, g: f32, b: f32) -> Color { + Color(Vec4::new(r, g, b, 1.0)) + } + + pub fn rgba(r: f32, g: f32, b: f32, a: f32) -> Color { + Color(Vec4::new(r, g, b, a)) + } +} + +impl Add for Color { + type Output = Color; + fn add(self, rhs: Color) -> Self::Output { + Color(self.0 + rhs.0) + } +} + +impl Add for Color { + type Output = Color; + fn add(self, rhs: Vec4) -> Self::Output { + Color(self.0 + rhs) + } +} + +impl From for Color { + fn from(vec4: Vec4) -> Self { + Color(vec4) + } +} + +impl Into<[f32; 4]> for Color { + fn into(self) -> [f32; 4] { + self.0.into() + } +} + +impl GetBytes for Color { + fn get_bytes(&self) -> Vec { + self.0.get_bytes() + } + fn get_bytes_ref(&self) -> Option<&[u8]> { + self.0.get_bytes_ref() + } +} pub enum ColorSource { - Color(Vec4), + Color(Color), Texture(Handle), } impl From for ColorSource { fn from(vec4: Vec4) -> Self { - ColorSource::Color(vec4) + ColorSource::Color(vec4.into()) + } +} + +impl From for ColorSource { + fn from(color: Color) -> Self { + ColorSource::Color(color) } } @@ -34,7 +89,7 @@ impl ShaderDefSuffixProvider for ColorSource { impl GetBytes for ColorSource { fn get_bytes(&self) -> Vec { match *self { - ColorSource::Color(color) => color.get_bytes(), + ColorSource::Color(ref color) => color.get_bytes(), ColorSource::Texture(_) => Vec::new(), // Texture is not a uniform } } diff --git a/src/render/light.rs b/src/render/light.rs index b7a9d4b058..d5b467399c 100644 --- a/src/render/light.rs +++ b/src/render/light.rs @@ -1,9 +1,10 @@ -use crate::{math, math::Vec4, prelude::Translation, render::camera}; +use super::Color; +use crate::{math, prelude::Translation, render::camera}; use std::ops::Range; use zerocopy::{AsBytes, FromBytes}; pub struct Light { - pub color: Vec4, + pub color: Color, pub fov: f32, pub depth: Range, } @@ -11,7 +12,7 @@ pub struct Light { impl Default for Light { fn default() -> Self { Light { - color: Vec4::new(1.0, 1.0, 1.0, 1.0), + color: Color::rgb(1.0, 1.0, 1.0), depth: 0.1..50.0, fov: f32::to_radians(60.0), } diff --git a/src/render/shader/uniforms/standard_material.rs b/src/render/shader/uniforms/standard_material.rs index 67d9f45135..1733ae1b56 100644 --- a/src/render/shader/uniforms/standard_material.rs +++ b/src/render/shader/uniforms/standard_material.rs @@ -1,4 +1,4 @@ -use crate::{math::Vec4, render::ColorSource}; +use crate::render::{Color, ColorSource}; use crate as bevy; // for macro imports use bevy_derive::Uniforms; @@ -12,7 +12,7 @@ pub struct StandardMaterial { impl Default for StandardMaterial { fn default() -> Self { StandardMaterial { - albedo: ColorSource::Color(Vec4::new(0.3, 0.3, 0.3, 1.0)), + albedo: Color::rgb(0.3, 0.3, 0.3).into(), } } } diff --git a/src/ui/node.rs b/src/ui/node.rs index 6c99b37785..508ec86b09 100644 --- a/src/ui/node.rs +++ b/src/ui/node.rs @@ -1,6 +1,7 @@ use crate::{ math, - math::{Vec2, Vec4}, + math::Vec2, + prelude::Color, ui::{Anchors, Margins}, }; @@ -16,7 +17,7 @@ pub struct Node { pub parent_dimensions: Vec2, pub anchors: Anchors, pub margins: Margins, - pub color: Vec4, + pub color: Color, } impl Default for Node { @@ -28,13 +29,13 @@ impl Default for Node { parent_dimensions: Vec2::default(), anchors: Anchors::default(), margins: Margins::default(), - color: math::vec4(0.0, 0.0, 0.0, 1.0), + color: Color::rgb(0.0, 0.0, 0.0), } } } impl Node { - pub fn new(position: Vec2, anchors: Anchors, margins: Margins, color: Vec4) -> Self { + pub fn new(position: Vec2, anchors: Anchors, margins: Margins, color: Color) -> Self { Node { position, global_position: Vec2::default(),