From c867c021c9e8a04f237398af23fa3630a0587da2 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Mon, 13 Jan 2020 18:06:06 -0800 Subject: [PATCH] add texture types --- examples/instancing.rs | 4 +- examples/parenting.rs | 8 +- examples/simple.rs | 4 +- examples/texture.rs | 75 +++++++++++++++++++ examples/ui.rs | 2 +- src/app/app_builder.rs | 1 + src/render/material.rs | 20 ++++- src/render/mod.rs | 2 + src/render/passes/forward_instanced/mod.rs | 4 +- .../material_resource_manager.rs | 2 +- src/render/texture.rs | 44 +++++++++++ 11 files changed, 150 insertions(+), 16 deletions(-) create mode 100644 examples/texture.rs create mode 100644 src/render/texture.rs diff --git a/examples/instancing.rs b/examples/instancing.rs index 2a8ed269eb..8569f0ac69 100644 --- a/examples/instancing.rs +++ b/examples/instancing.rs @@ -61,7 +61,7 @@ fn setup(world: &mut World) { depth: 0.1..50.0, target_view: None, }, - Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)), + Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))), LocalToWorld::identity(), Translation::new(4.0, -4.0, 5.0), Rotation::from_euler_angles(0.0, 0.0, 0.0), @@ -213,7 +213,7 @@ fn create_person(world: &mut World, mesh_handle: Handle, translation: Tran value: math::vec3(0.0, 0.0, 0.0), }, Instanced, - Material::new(math::vec4(0.5, 0.3, 0.3, 1.0) * random::()), + Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0) * random::())), mesh_handle, LocalToWorld::identity(), translation, diff --git a/examples/parenting.rs b/examples/parenting.rs index 2ae429d252..16b61cc044 100644 --- a/examples/parenting.rs +++ b/examples/parenting.rs @@ -40,7 +40,7 @@ fn setup(world: &mut World) { (), vec![( plane_handle.clone(), - Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)), + Material::new(Albedo::Color(math::vec4(0.1, 0.2, 0.1, 1.0))), LocalToWorld::identity(), Translation::new(0.0, 0.0, -5.0), )], @@ -52,7 +52,7 @@ fn setup(world: &mut World) { (), vec![( cube_handle.clone(), - Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)), + Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))), LocalToWorld::identity(), Translation::new(0.0, 0.0, 1.0), Rotation::from_euler_angles(0.0, 0.0, 0.0), @@ -67,7 +67,7 @@ fn setup(world: &mut World) { (), vec![( cube_handle, - Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)), + Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))), LocalToWorld::identity(), Translation::new(0.0, 0.0, 3.0), Parent(parent_cube), @@ -90,7 +90,7 @@ fn setup(world: &mut World) { depth: 0.1..50.0, target_view: None, }, - Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)), + Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))), LocalToWorld::identity(), Translation::new(4.0, -4.0, 5.0), Rotation::from_euler_angles(0.0, 0.0, 0.0), diff --git a/examples/simple.rs b/examples/simple.rs index f43833ed5a..5dde43643c 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -23,7 +23,7 @@ fn setup(world: &mut World) { (), vec![( plane_handle.clone(), - Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)), + Material::new(Albedo::Color(math::vec4(0.1, 0.2, 0.1, 1.0))), LocalToWorld::identity(), Translation::new(0.0, 0.0, 0.0), )], @@ -34,7 +34,7 @@ fn setup(world: &mut World) { (), vec![( cube_handle, - Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)), + Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))), LocalToWorld::identity(), Translation::new(0.0, 0.0, 1.0), )], diff --git a/examples/texture.rs b/examples/texture.rs new file mode 100644 index 0000000000..eb6951e4a4 --- /dev/null +++ b/examples/texture.rs @@ -0,0 +1,75 @@ +use bevy::{ + asset::{Asset, AssetStorage}, + math::{Mat4, Vec3}, + render::*, + *, +}; + +fn main() { + AppBuilder::new().add_defaults().setup_world(setup).run(); +} + +fn setup(world: &mut World) { + let cube_handle = { + let mut mesh_storage = world.resources.get_mut::>().unwrap(); + let cube = Mesh::load(MeshType::Cube); + (mesh_storage.add(cube)) + }; + + let texture_handle = { + let mut texture_storage = world.resources.get_mut::>().unwrap(); + let texture = Texture::load(TextureType::Data(create_texels(256))); + (texture_storage.add(texture)) + }; + + // cube + world.insert( + (), + vec![( + cube_handle, + Material::new(Albedo::Texture(texture_handle)), + LocalToWorld::identity(), + Translation::new(0.0, 0.0, 1.0), + )], + ); + + // light + world.insert( + (), + vec![( + Light { + color: wgpu::Color { + r: 0.8, + g: 0.8, + b: 0.5, + a: 1.0, + }, + fov: f32::to_radians(60.0), + depth: 0.1..50.0, + target_view: None, + }, + LocalToWorld::identity(), + Translation::new(4.0, -4.0, 5.0), + Rotation::from_euler_angles(0.0, 0.0, 0.0), + )], + ); + + // camera + world.insert( + (), + vec![( + Camera::new(CameraType::Projection { + fov: std::f32::consts::PI / 4.0, + near: 1.0, + far: 1000.0, + aspect_ratio: 1.0, + }), + ActiveCamera, + LocalToWorld(Mat4::look_at_rh( + Vec3::new(3.0, 8.0, 5.0), + Vec3::new(0.0, 0.0, 0.0), + Vec3::new(0.0, 0.0, 1.0), + )), + )], + ); +} diff --git a/examples/ui.rs b/examples/ui.rs index fe3f4d1caf..468ae56ef7 100644 --- a/examples/ui.rs +++ b/examples/ui.rs @@ -23,7 +23,7 @@ fn setup(world: &mut World) { vec![( cube_handle, LocalToWorld::identity(), - Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)), + Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0))), )], ); diff --git a/src/app/app_builder.rs b/src/app/app_builder.rs index 1dad4b32f5..04f8abf00f 100644 --- a/src/app/app_builder.rs +++ b/src/app/app_builder.rs @@ -79,6 +79,7 @@ impl AppBuilder { let resources = &mut self.world.resources; resources.insert(Time::new()); resources.insert(AssetStorage::::new()); + resources.insert(AssetStorage::::new()); self } diff --git a/src/render/material.rs b/src/render/material.rs index 5d21a7fcb1..99bad2e138 100644 --- a/src/render/material.rs +++ b/src/render/material.rs @@ -1,8 +1,13 @@ -use crate::math; +use crate::{asset::Handle, math, render::Texture}; use zerocopy::{AsBytes, FromBytes}; +pub enum Albedo { + Color(math::Vec4), + Texture(Handle), +} + pub struct Material { - pub color: math::Vec4, + pub albedo: Albedo, pub bind_group: Option, pub uniform_buf: Option, } @@ -10,13 +15,20 @@ pub struct Material { pub struct Instanced; impl Material { - pub fn new(color: math::Vec4) -> Self { + pub fn new(albedo: Albedo) -> Self { Material { - color, + albedo, bind_group: None, uniform_buf: None, } } + + pub fn get_color(&self) -> math::Vec4 { + match self.albedo { + Albedo::Color(color) => color, + _ => math::vec4(1.0, 0.0, 1.0, 1.0), + } + } } #[repr(C)] diff --git a/src/render/mod.rs b/src/render/mod.rs index 1bc47b31ed..e9bc55ce1c 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -1,6 +1,7 @@ pub mod camera; pub mod instancing; pub mod mesh; +pub mod texture; pub mod passes; pub mod render_resources; pub mod shader; @@ -14,6 +15,7 @@ pub use camera::*; pub use light::*; pub use material::*; pub use mesh::*; +pub use texture::*; pub use render_graph::*; pub use shader::*; diff --git a/src/render/passes/forward_instanced/mod.rs b/src/render/passes/forward_instanced/mod.rs index e98c228d41..376a9014eb 100644 --- a/src/render/passes/forward_instanced/mod.rs +++ b/src/render/passes/forward_instanced/mod.rs @@ -78,7 +78,7 @@ impl ForwardInstancedPipeline { slot.copy_from_slice( SimpleMaterialUniforms { position: translation.into(), - color: material.color.into(), + color: material.get_color().into(), } .as_bytes(), ); @@ -115,7 +115,7 @@ impl ForwardInstancedPipeline { data.push(SimpleMaterialUniforms { position: translation.into(), - color: material.color.into(), + color: material.get_color().into(), }); } diff --git a/src/render/render_resources/material_resource_manager.rs b/src/render/render_resources/material_resource_manager.rs index 261b7c4a2b..d91b71b361 100644 --- a/src/render/render_resources/material_resource_manager.rs +++ b/src/render/render_resources/material_resource_manager.rs @@ -50,7 +50,7 @@ impl RenderResourceManager for MaterialResourceManager { slot.copy_from_slice( MaterialUniforms { model: transform.0.to_cols_array_2d(), - color: material.color.into(), + color: material.get_color().into(), } .as_bytes(), ); diff --git a/src/render/texture.rs b/src/render/texture.rs new file mode 100644 index 0000000000..7a9eadaabe --- /dev/null +++ b/src/render/texture.rs @@ -0,0 +1,44 @@ +use crate::asset::Asset; + +pub enum TextureType { + Data(Vec), +} + +pub struct Texture { + pub data: Vec, +} + +impl Asset for Texture { + fn load(descriptor: TextureType) -> Self { + let data = match descriptor { + TextureType::Data(data) => data.clone(), + }; + + Texture { + data: data, + } + } +} + +pub fn create_texels(size: usize) -> Vec { + use std::iter; + + (0 .. size * size) + .flat_map(|id| { + // get high five for recognizing this ;) + let cx = 3.0 * (id % size) as f32 / (size - 1) as f32 - 2.0; + let cy = 2.0 * (id / size) as f32 / (size - 1) as f32 - 1.0; + let (mut x, mut y, mut count) = (cx, cy, 0); + while count < 0xFF && x * x + y * y < 4.0 { + let old_x = x; + x = x * x - y * y + cx; + y = 2.0 * old_x * y + cy; + count += 1; + } + iter::once(0xFF - (count * 5) as u8) + .chain(iter::once(0xFF - (count * 15) as u8)) + .chain(iter::once(0xFF - (count * 50) as u8)) + .chain(iter::once(1)) + }) + .collect() +} \ No newline at end of file