add texture types

This commit is contained in:
Carter Anderson 2020-01-13 18:06:06 -08:00
parent 7a6c938409
commit c867c021c9
11 changed files with 150 additions and 16 deletions

View file

@ -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<Mesh>, 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::<f32>()),
Material::new(Albedo::Color(math::vec4(0.5, 0.3, 0.3, 1.0) * random::<f32>())),
mesh_handle,
LocalToWorld::identity(),
translation,

View file

@ -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),

View file

@ -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),
)],

75
examples/texture.rs Normal file
View file

@ -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::<AssetStorage<Mesh>>().unwrap();
let cube = Mesh::load(MeshType::Cube);
(mesh_storage.add(cube))
};
let texture_handle = {
let mut texture_storage = world.resources.get_mut::<AssetStorage<Texture>>().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),
)),
)],
);
}

View file

@ -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))),
)],
);

View file

@ -79,6 +79,7 @@ impl AppBuilder {
let resources = &mut self.world.resources;
resources.insert(Time::new());
resources.insert(AssetStorage::<Mesh>::new());
resources.insert(AssetStorage::<Texture>::new());
self
}

View file

@ -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<Texture>),
}
pub struct Material {
pub color: math::Vec4,
pub albedo: Albedo,
pub bind_group: Option<wgpu::BindGroup>,
pub uniform_buf: Option<wgpu::Buffer>,
}
@ -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)]

View file

@ -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::*;

View file

@ -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(),
});
}

View file

@ -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(),
);

44
src/render/texture.rs Normal file
View file

@ -0,0 +1,44 @@
use crate::asset::Asset;
pub enum TextureType {
Data(Vec<u8>),
}
pub struct Texture {
pub data: Vec<u8>,
}
impl Asset<TextureType> 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<u8> {
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()
}