bevy/src/render/vertex.rs

94 lines
2.7 KiB
Rust
Raw Normal View History

2020-02-08 07:17:51 +00:00
use crate::render::render_graph_2::VertexBufferDescriptor;
2019-12-10 07:12:50 +00:00
use std::convert::From;
2020-01-11 10:11:27 +00:00
use zerocopy::{AsBytes, FromBytes};
2019-12-01 01:42:27 +00:00
#[repr(C)]
#[derive(Clone, Copy, AsBytes, FromBytes)]
pub struct Vertex {
2019-12-10 07:12:50 +00:00
pub position: [f32; 4],
pub normal: [f32; 4],
2020-01-14 01:35:30 +00:00
pub uv: [f32; 2],
2019-12-10 07:12:50 +00:00
}
2020-01-18 22:09:53 +00:00
impl Vertex {
// TODO: generate from macro
pub fn get_vertex_buffer_descriptor() -> VertexBufferDescriptor {
VertexBufferDescriptor {
stride: std::mem::size_of::<Vertex>() as u64,
step_mode: wgpu::InputStepMode::Vertex,
attributes: vec![
wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float4,
offset: 0,
shader_location: 0,
},
wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float4,
offset: 4 * 4,
shader_location: 1,
},
wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float2,
offset: 8 * 4,
shader_location: 2,
},
],
}
}
}
2020-01-14 01:35:30 +00:00
impl From<([f32; 4], [f32; 4], [f32; 2])> for Vertex {
fn from((position, normal, uv): ([f32; 4], [f32; 4], [f32; 2])) -> Self {
2019-12-10 07:12:50 +00:00
Vertex {
position: position,
normal: normal,
2020-01-14 01:35:30 +00:00
uv: uv,
2019-12-10 07:12:50 +00:00
}
}
}
2020-01-14 01:35:30 +00:00
impl From<([f32; 3], [f32; 3], [f32; 2])> for Vertex {
fn from((position, normal, uv): ([f32; 3], [f32; 3], [f32; 2])) -> Self {
2019-12-10 07:12:50 +00:00
Vertex {
2020-01-15 06:23:00 +00:00
position: [position[0], position[1], position[2], 1.0],
2020-01-14 01:35:30 +00:00
normal: [normal[0], normal[1], normal[2], 0.0],
2020-01-15 06:23:00 +00:00
uv: uv,
2019-12-10 07:12:50 +00:00
}
}
}
2020-01-14 01:35:30 +00:00
impl From<([i8; 4], [i8; 4], [i8; 2])> for Vertex {
fn from((position, normal, uv): ([i8; 4], [i8; 4], [i8; 2])) -> Self {
2019-12-10 07:12:50 +00:00
Vertex {
2020-01-11 10:11:27 +00:00
position: [
position[0] as f32,
position[1] as f32,
position[2] as f32,
position[3] as f32,
],
normal: [
normal[0] as f32,
normal[1] as f32,
normal[2] as f32,
normal[3] as f32,
],
2020-01-15 06:23:00 +00:00
uv: [uv[0] as f32, uv[1] as f32],
2019-12-10 07:12:50 +00:00
}
}
}
2020-01-14 01:35:30 +00:00
impl From<([i8; 3], [i8; 3], [i8; 2])> for Vertex {
fn from((position, normal, uv): ([i8; 3], [i8; 3], [i8; 2])) -> Self {
2019-12-10 07:12:50 +00:00
Vertex {
2020-01-11 10:11:27 +00:00
position: [
position[0] as f32,
position[1] as f32,
position[2] as f32,
1.0,
],
2019-12-10 07:12:50 +00:00
normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, 0.0],
2020-01-14 01:35:30 +00:00
uv: [uv[0] as f32, uv[1] as f32],
2019-12-10 07:12:50 +00:00
}
}
2020-01-11 10:11:27 +00:00
}