add ColorSource and remove test macro from StandardMaterial

This commit is contained in:
Carter Anderson 2020-02-18 09:16:34 -08:00
parent f820e4207c
commit b809b22589
12 changed files with 72 additions and 65 deletions

View File

@ -19,8 +19,7 @@ fn create_entities_insert_vec(
vec![( vec![(
plane_handle.clone(), plane_handle.clone(),
StandardMaterial { StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0), albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(),
everything_is_red: false,
}, },
LocalToWorld::identity(), LocalToWorld::identity(),
Translation::new(0.0, 0.0, 0.0), Translation::new(0.0, 0.0, 0.0),
@ -33,8 +32,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), albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(),
everything_is_red: false,
}, },
LocalToWorld::identity(), LocalToWorld::identity(),
Translation::new(0.0, 0.0, 1.0), Translation::new(0.0, 0.0, 1.0),
@ -84,8 +82,7 @@ fn create_entities_builder_add_component(
.build_entity() .build_entity()
.add(plane_handle.clone()) .add(plane_handle.clone())
.add(StandardMaterial { .add(StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0), albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(),
everything_is_red: false,
}) })
.add(LocalToWorld::identity()) .add(LocalToWorld::identity())
.add(Translation::new(0.0, 0.0, 0.0)) .add(Translation::new(0.0, 0.0, 0.0))
@ -93,8 +90,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), albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(),
everything_is_red: false,
}) })
.add(LocalToWorld::identity()) .add(LocalToWorld::identity())
.add(Translation::new(0.0, 0.0, 1.0)) .add(Translation::new(0.0, 0.0, 1.0))
@ -132,8 +128,7 @@ fn create_entities_builder_archetype(
.add_archetype(MeshEntity { .add_archetype(MeshEntity {
mesh: plane_handle.clone(), mesh: plane_handle.clone(),
material: StandardMaterial { material: StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0), albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(),
everything_is_red: false
}, },
..Default::default() ..Default::default()
}) })
@ -141,8 +136,7 @@ fn create_entities_builder_archetype(
.add_archetype(MeshEntity { .add_archetype(MeshEntity {
mesh: cube_handle, mesh: cube_handle,
material: StandardMaterial { material: StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0), albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(),
everything_is_red: false
}, },
..Default::default() ..Default::default()
}) })

View File

@ -194,8 +194,7 @@ fn create_person(world: &mut World, mesh_handle: Handle<Mesh>, translation: Tran
}, },
Instanced, Instanced,
StandardMaterial { StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0) * random::<f32>(), albedo: (math::vec4(0.5, 0.3, 0.3, 1.0) * random::<f32>()).into(),
everything_is_red: false,
}, },
mesh_handle, mesh_handle,
LocalToWorld::identity(), LocalToWorld::identity(),

View File

@ -36,8 +36,7 @@ fn setup(world: &mut World) {
vec![( vec![(
plane_handle.clone(), plane_handle.clone(),
StandardMaterial { StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0), albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(),
everything_is_red: false,
}, },
LocalToWorld::identity(), LocalToWorld::identity(),
Translation::new(0.0, 0.0, -5.0), Translation::new(0.0, 0.0, -5.0),
@ -51,8 +50,7 @@ fn setup(world: &mut World) {
vec![( vec![(
cube_handle.clone(), cube_handle.clone(),
StandardMaterial { StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0), albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(),
everything_is_red: false,
}, },
LocalToWorld::identity(), LocalToWorld::identity(),
Translation::new(0.0, 0.0, 1.0), Translation::new(0.0, 0.0, 1.0),
@ -69,8 +67,7 @@ fn setup(world: &mut World) {
vec![( vec![(
cube_handle, cube_handle,
StandardMaterial { StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0), albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(),
everything_is_red: false,
}, },
LocalToWorld::identity(), LocalToWorld::identity(),
Translation::new(0.0, 0.0, 3.0), Translation::new(0.0, 0.0, 3.0),

View File

@ -19,31 +19,19 @@ fn setup(world: &mut World) {
.add_archetype(MeshEntity { .add_archetype(MeshEntity {
mesh: plane_handle.clone(), mesh: plane_handle.clone(),
material: StandardMaterial { material: StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0), albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(),
everything_is_red: false,
}, },
..Default::default() ..Default::default()
}) })
// tan cube // cube
.add_archetype(MeshEntity { .add_archetype(MeshEntity {
mesh: cube_handle.clone(), mesh: cube_handle.clone(),
material: StandardMaterial { material: StandardMaterial {
albedo: math::vec4(0.5, 0.4, 0.3, 1.0), albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(),
everything_is_red: false,
}, },
translation: Translation::new(0.0, 0.0, 1.0), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()
}) })
// red cube
.add_archetype(MeshEntity {
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.5, 0.4, 0.3, 1.0),
everything_is_red: true,
},
translation: Translation::new(3.0, 0.0, 1.0),
..Default::default()
})
// light // light
.add_archetype(LightEntity { .add_archetype(LightEntity {
translation: Translation::new(4.0, -4.0, 5.0), translation: Translation::new(4.0, -4.0, 5.0),

View File

@ -18,13 +18,15 @@ fn build_move_system() -> Box<dyn Schedulable> {
.build(move |_, world, time, person_query| { .build(move |_, world, time, person_query| {
for (mut translation, mut material) in person_query.iter_mut(world) { for (mut translation, mut material) in person_query.iter_mut(world) {
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;
material.albedo = material.albedo if let ColorSource::Color(color) = material.albedo {
+ math::vec4( material.albedo = (color
-time.delta_seconds, + math::vec4(
-time.delta_seconds, -time.delta_seconds,
time.delta_seconds, -time.delta_seconds,
0.0, time.delta_seconds,
); 0.0,
)).into();
}
} }
}) })
} }
@ -73,8 +75,7 @@ fn setup(world: &mut World) {
.add_archetype(MeshEntity { .add_archetype(MeshEntity {
mesh: plane_handle.clone(), mesh: plane_handle.clone(),
material: StandardMaterial { material: StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0), albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(),
everything_is_red: false,
}, },
..Default::default() ..Default::default()
}) })
@ -82,8 +83,7 @@ fn setup(world: &mut World) {
.add_archetype(MeshEntity { .add_archetype(MeshEntity {
mesh: cube_handle.clone(), mesh: cube_handle.clone(),
material: StandardMaterial { material: StandardMaterial {
albedo: math::vec4(1.0, 1.0, 1.0, 1.0), albedo: math::vec4(1.0, 1.0, 1.0, 1.0).into(),
everything_is_red: false,
}, },
translation: Translation::new(0.0, 0.0, 1.0), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()
@ -91,8 +91,7 @@ fn setup(world: &mut World) {
.add_archetype(MeshEntity { .add_archetype(MeshEntity {
mesh: cube_handle.clone(), mesh: cube_handle.clone(),
material: StandardMaterial { material: StandardMaterial {
albedo: math::vec4(0.0, 1.0, 0.0, 1.0), albedo: math::vec4(0.0, 1.0, 0.0, 1.0).into(),
everything_is_red: true,
}, },
translation: Translation::new(-2.0, 0.0, 1.0), translation: Translation::new(-2.0, 0.0, 1.0),
..Default::default() ..Default::default()
@ -128,8 +127,7 @@ fn setup(world: &mut World) {
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, 1.0,
), ).into(),
everything_is_red: false,
}, },
translation: Translation::new( translation: Translation::new(
rng.gen_range(-50.0, 50.0), rng.gen_range(-50.0, 50.0),

View File

@ -22,8 +22,7 @@ fn setup(world: &mut World) {
.add_archetype(MeshEntity { .add_archetype(MeshEntity {
mesh: cube_handle.clone(), mesh: cube_handle.clone(),
material: StandardMaterial { material: StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0), albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(),
everything_is_red: false,
}, },
translation: Translation::new(0.0, 0.0, 1.0), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()

View File

@ -17,8 +17,7 @@ fn setup(world: &mut World) {
.add_archetype(MeshEntity { .add_archetype(MeshEntity {
mesh: cube_handle.clone(), mesh: cube_handle.clone(),
material: StandardMaterial { material: StandardMaterial {
albedo: math::vec4(0.5, 0.3, 0.3, 1.0), albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(),
everything_is_red: false,
}, },
translation: Translation::new(0.0, 0.0, 1.0), translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()

View File

@ -6,7 +6,7 @@ pub use crate::{
ecs::{default_archetypes::*, EntityArchetype, WorldBuilder, WorldBuilderSource}, ecs::{default_archetypes::*, EntityArchetype, WorldBuilder, WorldBuilderSource},
render::{ render::{
ActiveCamera, ActiveCamera2d, Camera, CameraType, Instanced, Light, ActiveCamera, ActiveCamera2d, Camera, CameraType, Instanced, Light,
render_graph::{StandardMaterial, Renderable, ShaderDefSuffixProvider}, render_graph::{StandardMaterial, Renderable, ShaderDefSuffixProvider}, ColorSource
}, },
ui::{Anchors, Margins, Node}, ui::{Anchors, Margins, Node},
}; };

38
src/render/color.rs Normal file
View File

@ -0,0 +1,38 @@
use crate::{
asset::{Handle, Texture},
math::Vec4,
render::render_graph::ShaderDefSuffixProvider, core::GetBytes,
};
pub enum ColorSource {
Color(Vec4),
Texture(Handle<Texture>),
}
impl From<Vec4> for ColorSource {
fn from(vec4: Vec4) -> Self {
ColorSource::Color(vec4)
}
}
impl ShaderDefSuffixProvider for ColorSource {
fn get_shader_def(&self) -> Option<&'static str> {
match *self {
ColorSource::Color(_) => Some("_color"),
ColorSource::Texture(_) => Some("_texture"),
}
}
}
impl GetBytes for ColorSource {
fn get_bytes(&self) -> Vec<u8> {
match *self {
ColorSource::Color(color) => color.get_bytes(),
ColorSource::Texture(_) => Vec::new(), // Texture is not a uniform
}
}
fn get_bytes_ref(&self) -> Option<&[u8]> {
None
}
}

View File

@ -3,12 +3,14 @@ pub mod render_graph;
pub mod shader; pub mod shader;
pub mod shader_reflect; pub mod shader_reflect;
mod color;
mod light; mod light;
mod vertex; mod vertex;
pub use camera::*; pub use camera::*;
pub use light::*; pub use light::*;
pub use shader::*; pub use shader::*;
pub use color::*;
pub use vertex::Vertex; pub use vertex::Vertex;

View File

@ -43,8 +43,4 @@ void main() {
} }
// multiply the light by material color // multiply the light by material color
o_Target = vec4(color, 1.0) * Albedo; o_Target = vec4(color, 1.0) * Albedo;
# ifdef everything_is_red
o_Target = vec4(1.0, 0.0, 0.0, 1.0);
# endif
} }

View File

@ -1,20 +1,17 @@
use crate::{math, math::Vec4, render::render_graph::ShaderDefSuffixProvider}; use crate::{math::Vec4, render::ColorSource};
use crate as bevy; // for macro imports use crate as bevy; // for macro imports
use bevy_derive::Uniforms; use bevy_derive::Uniforms;
#[derive(Uniforms)] #[derive(Uniforms)]
pub struct StandardMaterial { pub struct StandardMaterial {
pub albedo: Vec4, pub albedo: ColorSource,
#[uniform(ignore, shader_def)]
pub everything_is_red: bool,
} }
impl Default for StandardMaterial { impl Default for StandardMaterial {
fn default() -> Self { fn default() -> Self {
StandardMaterial { StandardMaterial {
albedo: math::vec4(0.3, 0.3, 0.3, 1.0), albedo: ColorSource::Color(Vec4::new(0.3, 0.3, 0.3, 1.0)),
everything_is_red: false,
} }
} }
} }