add Color type

This commit is contained in:
Carter Anderson 2020-03-09 23:43:40 -07:00
parent aa09e93980
commit 99cdf56e7d
12 changed files with 100 additions and 49 deletions

View File

@ -4,7 +4,7 @@ use bevy_derive::Uniforms;
#[derive(Uniforms, Default)] #[derive(Uniforms, Default)]
struct MyMaterial { struct MyMaterial {
pub color: Vec4, pub color: Color,
#[uniform(ignore, shader_def)] #[uniform(ignore, shader_def)]
pub always_red: bool, pub always_red: bool,
} }
@ -79,7 +79,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
..Default::default() ..Default::default()
}, },
material: MyMaterial { 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, always_red: false,
}, },
translation: Translation::new(-2.0, 0.0, 0.0), translation: Translation::new(-2.0, 0.0, 0.0),
@ -93,7 +93,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
..Default::default() ..Default::default()
}, },
material: MyMaterial { 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, always_red: true,
}, },
translation: Translation::new(2.0, 0.0, 0.0), translation: Translation::new(2.0, 0.0, 0.0),

View File

@ -16,7 +16,7 @@ fn create_entities_insert_vec(
vec![( vec![(
plane_handle, plane_handle,
StandardMaterial { 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(), LocalToWorld::identity(),
Translation::new(0.0, 0.0, 0.0), Translation::new(0.0, 0.0, 0.0),
@ -29,7 +29,7 @@ fn create_entities_insert_vec(
vec![( vec![(
cube_handle, cube_handle,
StandardMaterial { 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(), LocalToWorld::identity(),
Translation::new(0.0, 0.0, 1.0), Translation::new(0.0, 0.0, 1.0),
@ -79,7 +79,7 @@ fn create_entities_builder_add_component(
.build_entity() .build_entity()
.add(plane_handle) .add(plane_handle)
.add(StandardMaterial { .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(LocalToWorld::identity())
.add(Translation::new(0.0, 0.0, 0.0)) .add(Translation::new(0.0, 0.0, 0.0))
@ -87,7 +87,7 @@ fn create_entities_builder_add_component(
.build_entity() .build_entity()
.add(cube_handle) .add(cube_handle)
.add(StandardMaterial { .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(LocalToWorld::identity())
.add(Translation::new(0.0, 0.0, 1.0)) .add(Translation::new(0.0, 0.0, 1.0))
@ -125,7 +125,7 @@ fn create_entities_builder_archetype(
.add_entity(MeshEntity { .add_entity(MeshEntity {
mesh: plane_handle, mesh: plane_handle,
material: StandardMaterial { 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() ..Default::default()
}) })
@ -133,7 +133,7 @@ fn create_entities_builder_archetype(
.add_entity(MeshEntity { .add_entity(MeshEntity {
mesh: cube_handle, mesh: cube_handle,
material: StandardMaterial { 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() ..Default::default()
}) })

View File

@ -32,7 +32,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
.add_entity(MeshEntity { .add_entity(MeshEntity {
mesh: cube_handle, mesh: cube_handle,
material: StandardMaterial { 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), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()
@ -43,7 +43,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
builder.add_entity(MeshEntity { builder.add_entity(MeshEntity {
mesh: cube_handle, mesh: cube_handle,
material: StandardMaterial { 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), translation: Translation::new(0.0, 0.0, 3.0),
..Default::default() ..Default::default()

View File

@ -24,7 +24,7 @@ pub fn setup(world: &mut World, resources: &mut Resources) {
.add_archetype(MeshEntity { .add_archetype(MeshEntity {
mesh: plane_handle, mesh: plane_handle,
material: StandardMaterial { 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() ..Default::default()
}) })
@ -32,7 +32,7 @@ pub fn setup(world: &mut World, resources: &mut Resources) {
.add_archetype(MeshEntity { .add_archetype(MeshEntity {
mesh: cube_handle, mesh: cube_handle,
material: StandardMaterial { 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), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()

View File

@ -15,7 +15,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
.add_entity(MeshEntity { .add_entity(MeshEntity {
mesh: plane_handle, mesh: plane_handle,
material: StandardMaterial { 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() ..Default::default()
}) })
@ -23,7 +23,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
.add_entity(MeshEntity { .add_entity(MeshEntity {
mesh: cube_handle, mesh: cube_handle,
material: StandardMaterial { 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), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()

View File

@ -20,12 +20,7 @@ fn build_move_system() -> Box<dyn Schedulable> {
translation.0 += math::vec3(1.0, 0.0, 0.0) * time.delta_seconds; translation.0 += math::vec3(1.0, 0.0, 0.0) * time.delta_seconds;
if let ColorSource::Color(color) = material.albedo { if let ColorSource::Color(color) = material.albedo {
material.albedo = (color material.albedo = (color
+ math::vec4( + Color::rgb(-time.delta_seconds, -time.delta_seconds, time.delta_seconds))
-time.delta_seconds,
-time.delta_seconds,
time.delta_seconds,
0.0,
))
.into(); .into();
} }
} }
@ -72,7 +67,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
.add_entity(MeshEntity { .add_entity(MeshEntity {
mesh: plane_handle, mesh: plane_handle,
material: StandardMaterial { 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() ..Default::default()
}) })
@ -80,7 +75,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
.add_entity(MeshEntity { .add_entity(MeshEntity {
mesh: cube_handle, mesh: cube_handle,
material: StandardMaterial { 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), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()
@ -88,7 +83,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
.add_entity(MeshEntity { .add_entity(MeshEntity {
mesh: cube_handle, mesh: cube_handle,
material: StandardMaterial { 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), translation: Translation::new(-2.0, 0.0, 1.0),
..Default::default() ..Default::default()
@ -119,11 +114,10 @@ fn setup(world: &mut World, resources: &mut Resources) {
builder = builder.add_entity(MeshEntity { builder = builder.add_entity(MeshEntity {
mesh: cube_handle, mesh: cube_handle,
material: StandardMaterial { 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), rng.gen_range(0.0, 1.0),
rng.gen_range(0.0, 1.0), rng.gen_range(0.0, 1.0),
1.0,
) )
.into(), .into(),
}, },

View File

@ -14,7 +14,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
.add_entity(MeshEntity { .add_entity(MeshEntity {
mesh: cube_handle, mesh: cube_handle,
material: StandardMaterial { 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), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()
@ -57,7 +57,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
math::vec2(0.0, 0.0), math::vec2(0.0, 0.0),
Anchors::new(0.0, 0.0, 0.0, 1.0), Anchors::new(0.0, 0.0, 0.0, 1.0),
Margins::new(10.0, 200.0, 10.0, 10.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 // top right anchor with vertical fill
@ -66,7 +66,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
math::vec2(0.0, 0.0), math::vec2(0.0, 0.0),
Anchors::new(1.0, 1.0, 0.0, 1.0), Anchors::new(1.0, 1.0, 0.0, 1.0),
Margins::new(10.0, 100.0, 50.0, 100.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 // 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), math::vec2(75.0, 75.0),
Anchors::new(0.5, 0.5, 0.5, 0.5), Anchors::new(0.5, 0.5, 0.5, 0.5),
Margins::new(0.0, 100.0, 0.0, 100.0), 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 { .add_entity(UiEntity {
@ -83,7 +83,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
math::vec2(50.0, 50.0), math::vec2(50.0, 50.0),
Anchors::new(0.5, 0.5, 0.5, 0.5), Anchors::new(0.5, 0.5, 0.5, 0.5),
Margins::new(0.0, 100.0, 0.0, 100.0), 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 { .add_entity(UiEntity {
@ -91,7 +91,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
math::vec2(100.0, 100.0), math::vec2(100.0, 100.0),
Anchors::new(0.5, 0.5, 0.5, 0.5), Anchors::new(0.5, 0.5, 0.5, 0.5),
Margins::new(0.0, 100.0, 0.0, 100.0), 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 { .add_entity(UiEntity {
@ -99,7 +99,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
math::vec2(150.0, 150.0), math::vec2(150.0, 150.0),
Anchors::new(0.5, 0.5, 0.5, 0.5), Anchors::new(0.5, 0.5, 0.5, 0.5),
Margins::new(0.0, 100.0, 0.0, 100.0), 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 // parenting
@ -108,7 +108,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
math::vec2(300.0, 300.0), math::vec2(300.0, 300.0),
Anchors::new(0.0, 0.0, 0.0, 0.0), Anchors::new(0.0, 0.0, 0.0, 0.0),
Margins::new(0.0, 200.0, 0.0, 200.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| { .add_children(|builder| {
@ -117,7 +117,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
math::vec2(0.0, 0.0), math::vec2(0.0, 0.0),
Anchors::new(0.0, 1.0, 0.0, 1.0), Anchors::new(0.0, 1.0, 0.0, 1.0),
Margins::new(20.0, 20.0, 20.0, 20.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), math::vec2(200.0, 200.0),
Anchors::new(0.5, 0.5, 0.5, 0.5), Anchors::new(0.5, 0.5, 0.5, 0.5),
Margins::new(0.0, 100.0, 0.0, 100.0), 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(); .build();

View File

@ -8,7 +8,7 @@ pub use crate::{
pipeline::PipelineDescriptor, pipeline::PipelineDescriptor,
render_resource::{resource_name, resource_providers::UniformResourceProvider}, render_resource::{resource_name, resource_providers::UniformResourceProvider},
shader::{uniforms::StandardMaterial, Shader, ShaderDefSuffixProvider, ShaderStage}, shader::{uniforms::StandardMaterial, Shader, ShaderDefSuffixProvider, ShaderStage},
ActiveCamera, ActiveCamera2d, Camera, CameraType, ColorSource, Instanced, Light, ActiveCamera, ActiveCamera2d, Camera, CameraType, Color, ColorSource, Instanced, Light,
Renderable, Renderable,
}, },
ui::{Anchors, Margins, Node}, ui::{Anchors, Margins, Node},

View File

@ -4,15 +4,70 @@ use crate::{
math::Vec4, math::Vec4,
render::shader::ShaderDefSuffixProvider, 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<Color> for Color {
type Output = Color;
fn add(self, rhs: Color) -> Self::Output {
Color(self.0 + rhs.0)
}
}
impl Add<Vec4> for Color {
type Output = Color;
fn add(self, rhs: Vec4) -> Self::Output {
Color(self.0 + rhs)
}
}
impl From<Vec4> 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<u8> {
self.0.get_bytes()
}
fn get_bytes_ref(&self) -> Option<&[u8]> {
self.0.get_bytes_ref()
}
}
pub enum ColorSource { pub enum ColorSource {
Color(Vec4), Color(Color),
Texture(Handle<Texture>), Texture(Handle<Texture>),
} }
impl From<Vec4> for ColorSource { impl From<Vec4> for ColorSource {
fn from(vec4: Vec4) -> Self { fn from(vec4: Vec4) -> Self {
ColorSource::Color(vec4) ColorSource::Color(vec4.into())
}
}
impl From<Color> for ColorSource {
fn from(color: Color) -> Self {
ColorSource::Color(color)
} }
} }
@ -34,7 +89,7 @@ impl ShaderDefSuffixProvider for ColorSource {
impl GetBytes for ColorSource { impl GetBytes for ColorSource {
fn get_bytes(&self) -> Vec<u8> { fn get_bytes(&self) -> Vec<u8> {
match *self { match *self {
ColorSource::Color(color) => color.get_bytes(), ColorSource::Color(ref color) => color.get_bytes(),
ColorSource::Texture(_) => Vec::new(), // Texture is not a uniform ColorSource::Texture(_) => Vec::new(), // Texture is not a uniform
} }
} }

View File

@ -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 std::ops::Range;
use zerocopy::{AsBytes, FromBytes}; use zerocopy::{AsBytes, FromBytes};
pub struct Light { pub struct Light {
pub color: Vec4, pub color: Color,
pub fov: f32, pub fov: f32,
pub depth: Range<f32>, pub depth: Range<f32>,
} }
@ -11,7 +12,7 @@ pub struct Light {
impl Default for Light { impl Default for Light {
fn default() -> Self { fn default() -> Self {
Light { 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, depth: 0.1..50.0,
fov: f32::to_radians(60.0), fov: f32::to_radians(60.0),
} }

View File

@ -1,4 +1,4 @@
use crate::{math::Vec4, render::ColorSource}; use crate::render::{Color, ColorSource};
use crate as bevy; // for macro imports use crate as bevy; // for macro imports
use bevy_derive::Uniforms; use bevy_derive::Uniforms;
@ -12,7 +12,7 @@ pub struct StandardMaterial {
impl Default for StandardMaterial { impl Default for StandardMaterial {
fn default() -> Self { fn default() -> Self {
StandardMaterial { StandardMaterial {
albedo: ColorSource::Color(Vec4::new(0.3, 0.3, 0.3, 1.0)), albedo: Color::rgb(0.3, 0.3, 0.3).into(),
} }
} }
} }

View File

@ -1,6 +1,7 @@
use crate::{ use crate::{
math, math,
math::{Vec2, Vec4}, math::Vec2,
prelude::Color,
ui::{Anchors, Margins}, ui::{Anchors, Margins},
}; };
@ -16,7 +17,7 @@ pub struct Node {
pub parent_dimensions: Vec2, pub parent_dimensions: Vec2,
pub anchors: Anchors, pub anchors: Anchors,
pub margins: Margins, pub margins: Margins,
pub color: Vec4, pub color: Color,
} }
impl Default for Node { impl Default for Node {
@ -28,13 +29,13 @@ impl Default for Node {
parent_dimensions: Vec2::default(), parent_dimensions: Vec2::default(),
anchors: Anchors::default(), anchors: Anchors::default(),
margins: Margins::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 { 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 { Node {
position, position,
global_position: Vec2::default(), global_position: Vec2::default(),