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

View file

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

View file

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

View file

@ -19,31 +19,19 @@ fn setup(world: &mut World) {
.add_archetype(MeshEntity {
mesh: plane_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0),
everything_is_red: false,
albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(),
},
..Default::default()
})
// tan cube
// cube
.add_archetype(MeshEntity {
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.5, 0.4, 0.3, 1.0),
everything_is_red: false,
albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(),
},
translation: Translation::new(0.0, 0.0, 1.0),
..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
.add_archetype(LightEntity {
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| {
for (mut translation, mut material) in person_query.iter_mut(world) {
translation.0 += math::vec3(1.0, 0.0, 0.0) * time.delta_seconds;
material.albedo = material.albedo
+ math::vec4(
-time.delta_seconds,
-time.delta_seconds,
time.delta_seconds,
0.0,
);
if let ColorSource::Color(color) = material.albedo {
material.albedo = (color
+ math::vec4(
-time.delta_seconds,
-time.delta_seconds,
time.delta_seconds,
0.0,
)).into();
}
}
})
}
@ -73,8 +75,7 @@ fn setup(world: &mut World) {
.add_archetype(MeshEntity {
mesh: plane_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.1, 0.2, 0.1, 1.0),
everything_is_red: false,
albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(),
},
..Default::default()
})
@ -82,8 +83,7 @@ fn setup(world: &mut World) {
.add_archetype(MeshEntity {
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(1.0, 1.0, 1.0, 1.0),
everything_is_red: false,
albedo: math::vec4(1.0, 1.0, 1.0, 1.0).into(),
},
translation: Translation::new(0.0, 0.0, 1.0),
..Default::default()
@ -91,8 +91,7 @@ fn setup(world: &mut World) {
.add_archetype(MeshEntity {
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: math::vec4(0.0, 1.0, 0.0, 1.0),
everything_is_red: true,
albedo: math::vec4(0.0, 1.0, 0.0, 1.0).into(),
},
translation: Translation::new(-2.0, 0.0, 1.0),
..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),
1.0,
),
everything_is_red: false,
).into(),
},
translation: Translation::new(
rng.gen_range(-50.0, 50.0),

View file

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

View file

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

View file

@ -6,7 +6,7 @@ pub use crate::{
ecs::{default_archetypes::*, EntityArchetype, WorldBuilder, WorldBuilderSource},
render::{
ActiveCamera, ActiveCamera2d, Camera, CameraType, Instanced, Light,
render_graph::{StandardMaterial, Renderable, ShaderDefSuffixProvider},
render_graph::{StandardMaterial, Renderable, ShaderDefSuffixProvider}, ColorSource
},
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_reflect;
mod color;
mod light;
mod vertex;
pub use camera::*;
pub use light::*;
pub use shader::*;
pub use color::*;
pub use vertex::Vertex;

View file

@ -43,8 +43,4 @@ void main() {
}
// multiply the light by material color
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 bevy_derive::Uniforms;
#[derive(Uniforms)]
pub struct StandardMaterial {
pub albedo: Vec4,
#[uniform(ignore, shader_def)]
pub everything_is_red: bool,
pub albedo: ColorSource,
}
impl Default for StandardMaterial {
fn default() -> Self {
StandardMaterial {
albedo: math::vec4(0.3, 0.3, 0.3, 1.0),
everything_is_red: false,
albedo: ColorSource::Color(Vec4::new(0.3, 0.3, 0.3, 1.0)),
}
}
}