add uvs to vertex layout

This commit is contained in:
Carter Anderson 2020-01-13 17:35:30 -08:00
parent 5ab026d943
commit 7a6c938409
9 changed files with 69 additions and 52 deletions

View File

@ -67,10 +67,10 @@ pub fn create_quad(
south_east: Vec2, south_east: Vec2,
) -> (Vec<Vertex>, Vec<u16>) { ) -> (Vec<Vertex>, Vec<u16>) {
let vertex_data = [ let vertex_data = [
Vertex::from(([south_west.x(), south_west.y(), 0.0], [0.0, 0.0, 1.0])), Vertex::from(([south_west.x(), south_west.y(), 0.0], [0.0, 0.0, 1.0], [0.0, 0.0])),
Vertex::from(([north_west.x(), north_west.y(), 0.0], [0.0, 0.0, 1.0])), Vertex::from(([north_west.x(), north_west.y(), 0.0], [0.0, 0.0, 1.0], [0.0, 1.0])),
Vertex::from(([north_east.x(), north_east.y(), 0.0], [0.0, 0.0, 1.0])), Vertex::from(([north_east.x(), north_east.y(), 0.0], [0.0, 0.0, 1.0], [1.0, 1.0])),
Vertex::from(([south_east.x(), south_east.y(), 0.0], [0.0, 0.0, 1.0])), Vertex::from(([south_east.x(), south_east.y(), 0.0], [0.0, 0.0, 1.0], [1.0, 0.0])),
]; ];
let index_data: &[u16] = &[0, 2, 1, 0, 3, 2]; let index_data: &[u16] = &[0, 2, 1, 0, 3, 2];
@ -80,35 +80,35 @@ pub fn create_quad(
pub fn create_cube() -> (Vec<Vertex>, Vec<u16>) { pub fn create_cube() -> (Vec<Vertex>, Vec<u16>) {
let vertex_data = [ let vertex_data = [
// top (0, 0, 1) // top (0, 0, 1)
Vertex::from(([-1, -1, 1], [0, 0, 1])), Vertex::from(([-1, -1, 1], [0, 0, 1], [0, 0])),
Vertex::from(([1, -1, 1], [0, 0, 1])), Vertex::from(([1, -1, 1], [0, 0, 1], [1, 0])),
Vertex::from(([1, 1, 1], [0, 0, 1])), Vertex::from(([1, 1, 1], [0, 0, 1], [1, 1])),
Vertex::from(([-1, 1, 1], [0, 0, 1])), Vertex::from(([-1, 1, 1], [0, 0, 1], [0, 1])),
// bottom (0, 0, -1) // bottom (0, 0, -1)
Vertex::from(([-1, 1, -1], [0, 0, -1])), Vertex::from(([-1, 1, -1], [0, 0, -1], [1, 0])),
Vertex::from(([1, 1, -1], [0, 0, -1])), Vertex::from(([1, 1, -1], [0, 0, -1], [0, 0])),
Vertex::from(([1, -1, -1], [0, 0, -1])), Vertex::from(([1, -1, -1], [0, 0, -1], [0, 1])),
Vertex::from(([-1, -1, -1], [0, 0, -1])), Vertex::from(([-1, -1, -1], [0, 0, -1], [1, 1])),
// right (1, 0, 0) // right (1, 0, 0)
Vertex::from(([1, -1, -1], [1, 0, 0])), Vertex::from(([1, -1, -1], [1, 0, 0], [0, 0])),
Vertex::from(([1, 1, -1], [1, 0, 0])), Vertex::from(([1, 1, -1], [1, 0, 0], [1, 0])),
Vertex::from(([1, 1, 1], [1, 0, 0])), Vertex::from(([1, 1, 1], [1, 0, 0], [1, 1])),
Vertex::from(([1, -1, 1], [1, 0, 0])), Vertex::from(([1, -1, 1], [1, 0, 0], [0, 1])),
// left (-1, 0, 0) // left (-1, 0, 0)
Vertex::from(([-1, -1, 1], [-1, 0, 0])), Vertex::from(([-1, -1, 1], [-1, 0, 0], [1, 0])),
Vertex::from(([-1, 1, 1], [-1, 0, 0])), Vertex::from(([-1, 1, 1], [-1, 0, 0], [0, 0])),
Vertex::from(([-1, 1, -1], [-1, 0, 0])), Vertex::from(([-1, 1, -1], [-1, 0, 0], [0, 1])),
Vertex::from(([-1, -1, -1], [-1, 0, 0])), Vertex::from(([-1, -1, -1], [-1, 0, 0], [1, 1])),
// front (0, 1, 0) // front (0, 1, 0)
Vertex::from(([1, 1, -1], [0, 1, 0])), Vertex::from(([1, 1, -1], [0, 1, 0], [1, 0])),
Vertex::from(([-1, 1, -1], [0, 1, 0])), Vertex::from(([-1, 1, -1], [0, 1, 0], [0, 0])),
Vertex::from(([-1, 1, 1], [0, 1, 0])), Vertex::from(([-1, 1, 1], [0, 1, 0], [0, 1])),
Vertex::from(([1, 1, 1], [0, 1, 0])), Vertex::from(([1, 1, 1], [0, 1, 0], [1, 1])),
// back (0, -1, 0) // back (0, -1, 0)
Vertex::from(([1, -1, 1], [0, -1, 0])), Vertex::from(([1, -1, 1], [0, -1, 0], [0, 0])),
Vertex::from(([-1, -1, 1], [0, -1, 0])), Vertex::from(([-1, -1, 1], [0, -1, 0], [1, 0])),
Vertex::from(([-1, -1, -1], [0, -1, 0])), Vertex::from(([-1, -1, -1], [0, -1, 0], [1, 1])),
Vertex::from(([1, -1, -1], [0, -1, 0])), Vertex::from(([1, -1, -1], [0, -1, 0], [0, 1])),
]; ];
let index_data: &[u16] = &[ let index_data: &[u16] = &[

View File

@ -50,6 +50,11 @@ pub fn get_vertex_buffer_descriptor<'a>() -> wgpu::VertexBufferDescriptor<'a> {
offset: 4 * 4, offset: 4 * 4,
shader_location: 1, shader_location: 1,
}, },
wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float2,
offset: 8 * 4,
shader_location: 2,
},
], ],
} }
} }

View File

@ -2,6 +2,7 @@
layout(location = 0) in vec4 a_Pos; layout(location = 0) in vec4 a_Pos;
layout(location = 1) in vec4 a_Normal; layout(location = 1) in vec4 a_Normal;
layout(location = 2) in vec4 a_Uv;
layout(location = 0) out vec3 v_Normal; layout(location = 0) out vec3 v_Normal;
layout(location = 1) out vec4 v_Position; layout(location = 1) out vec4 v_Position;

View File

@ -3,10 +3,11 @@
// vertex attributes // vertex attributes
layout(location = 0) in vec4 a_Pos; layout(location = 0) in vec4 a_Pos;
layout(location = 1) in vec4 a_Normal; layout(location = 1) in vec4 a_Normal;
layout(location = 2) in vec2 a_Uv;
// Instanced attributes // Instanced attributes
layout (location = 2) in vec3 a_instancePos; layout (location = 3) in vec3 a_instancePos;
layout (location = 3) in vec4 a_instanceColor; layout (location = 4) in vec4 a_instanceColor;
layout(location = 0) out vec3 v_Normal; layout(location = 0) out vec3 v_Normal;

View File

@ -199,12 +199,12 @@ impl Pipeline for ForwardInstancedPipeline {
wgpu::VertexAttributeDescriptor { wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float3, format: wgpu::VertexFormat::Float3,
offset: 0, offset: 0,
shader_location: 2, shader_location: 3,
}, },
wgpu::VertexAttributeDescriptor { wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float4, format: wgpu::VertexFormat::Float4,
offset: 3 * 4, offset: 3 * 4,
shader_location: 3, shader_location: 4,
}, },
], ],
}; };

View File

@ -2,6 +2,7 @@
layout(location = 0) in vec4 a_Pos; layout(location = 0) in vec4 a_Pos;
layout(location = 1) in vec4 a_Normal; layout(location = 1) in vec4 a_Normal;
layout(location = 2) in vec2 a_Uv;
layout(location = 0) out vec3 v_Normal; layout(location = 0) out vec3 v_Normal;
layout(location = 1) out vec4 v_Position; layout(location = 1) out vec4 v_Position;

View File

@ -155,22 +155,22 @@ impl Pipeline for UiPipeline {
wgpu::VertexAttributeDescriptor { wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float2, format: wgpu::VertexFormat::Float2,
offset: 0, offset: 0,
shader_location: 2, shader_location: 3,
}, },
wgpu::VertexAttributeDescriptor { wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float2, format: wgpu::VertexFormat::Float2,
offset: 2 * 4, offset: 2 * 4,
shader_location: 3, shader_location: 4,
}, },
wgpu::VertexAttributeDescriptor { wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float4, format: wgpu::VertexFormat::Float4,
offset: 4 * 4, offset: 4 * 4,
shader_location: 4, shader_location: 5,
}, },
wgpu::VertexAttributeDescriptor { wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float, format: wgpu::VertexFormat::Float,
offset: 8 * 4, offset: 8 * 4,
shader_location: 5, shader_location: 6,
}, },
], ],
}; };

View File

@ -3,12 +3,13 @@
// vertex attributes // vertex attributes
layout(location = 0) in vec4 a_Pos; layout(location = 0) in vec4 a_Pos;
layout(location = 1) in vec4 a_Normal; layout(location = 1) in vec4 a_Normal;
layout(location = 2) in vec2 a_Uv;
// instanced attributes (RectData) // instanced attributes (RectData)
layout (location = 2) in vec2 a_RectPosition; layout (location = 3) in vec2 a_RectPosition;
layout (location = 3) in vec2 a_RectSize; layout (location = 4) in vec2 a_RectSize;
layout (location = 4) in vec4 a_RectColor; layout (location = 5) in vec4 a_RectColor;
layout (location = 5) in float a_RectZIndex; layout (location = 6) in float a_RectZIndex;
layout(location = 0) out vec4 v_Color; layout(location = 0) out vec4 v_Color;

View File

@ -6,33 +6,36 @@ use zerocopy::{AsBytes, FromBytes};
pub struct Vertex { pub struct Vertex {
pub position: [f32; 4], pub position: [f32; 4],
pub normal: [f32; 4], pub normal: [f32; 4],
pub uv: [f32; 2],
} }
impl From<([f32; 4], [f32; 4])> for Vertex { impl From<([f32; 4], [f32; 4], [f32; 2])> for Vertex {
fn from((position, normal): ([f32; 4], [f32; 4])) -> Self { fn from((position, normal, uv): ([f32; 4], [f32; 4], [f32; 2])) -> Self {
Vertex { Vertex {
position: position, position: position,
normal: normal, normal: normal,
uv: uv,
} }
} }
} }
impl From<([f32; 3], [f32; 3])> for Vertex { impl From<([f32; 3], [f32; 3], [f32; 2])> for Vertex {
fn from((position, normal): ([f32; 3], [f32; 3])) -> Self { fn from((position, normal, uv): ([f32; 3], [f32; 3], [f32; 2])) -> Self {
Vertex { Vertex {
position: [ position: [
position[0] as f32, position[0],
position[1] as f32, position[1],
position[2] as f32, position[2],
1.0, 1.0,
], ],
normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, 0.0], normal: [normal[0], normal[1], normal[2], 0.0],
uv: uv
} }
} }
} }
impl From<([i8; 4], [i8; 4])> for Vertex { impl From<([i8; 4], [i8; 4], [i8; 2])> for Vertex {
fn from((position, normal): ([i8; 4], [i8; 4])) -> Self { fn from((position, normal, uv): ([i8; 4], [i8; 4], [i8; 2])) -> Self {
Vertex { Vertex {
position: [ position: [
position[0] as f32, position[0] as f32,
@ -46,12 +49,16 @@ impl From<([i8; 4], [i8; 4])> for Vertex {
normal[2] as f32, normal[2] as f32,
normal[3] as f32, normal[3] as f32,
], ],
uv: [
uv[0] as f32,
uv[1] as f32,
],
} }
} }
} }
impl From<([i8; 3], [i8; 3])> for Vertex { impl From<([i8; 3], [i8; 3], [i8; 2])> for Vertex {
fn from((position, normal): ([i8; 3], [i8; 3])) -> Self { fn from((position, normal, uv): ([i8; 3], [i8; 3], [i8; 2])) -> Self {
Vertex { Vertex {
position: [ position: [
position[0] as f32, position[0] as f32,
@ -60,6 +67,7 @@ impl From<([i8; 3], [i8; 3])> for Vertex {
1.0, 1.0,
], ],
normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, 0.0], normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, 0.0],
uv: [uv[0] as f32, uv[1] as f32],
} }
} }
} }