From 7a6c9384093e88ad6764f3a6afd4cc545b0d10c3 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Mon, 13 Jan 2020 17:35:30 -0800 Subject: [PATCH] add uvs to vertex layout --- src/render/mesh.rs | 56 +++++++++---------- src/render/mod.rs | 5 ++ src/render/passes/forward/forward.vert | 1 + .../forward_instanced/forward_instanced.vert | 5 +- src/render/passes/forward_instanced/mod.rs | 4 +- .../passes/forward_shadow/forward_shadow.vert | 1 + src/render/passes/ui/mod.rs | 8 +-- src/render/passes/ui/ui.vert | 9 +-- src/render/vertex.rs | 32 +++++++---- 9 files changed, 69 insertions(+), 52 deletions(-) diff --git a/src/render/mesh.rs b/src/render/mesh.rs index 62c5684214..b873f1c730 100644 --- a/src/render/mesh.rs +++ b/src/render/mesh.rs @@ -67,10 +67,10 @@ pub fn create_quad( south_east: Vec2, ) -> (Vec, Vec) { let vertex_data = [ - Vertex::from(([south_west.x(), south_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])), - Vertex::from(([north_east.x(), north_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])), + 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], [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], [1.0, 0.0])), ]; let index_data: &[u16] = &[0, 2, 1, 0, 3, 2]; @@ -80,35 +80,35 @@ pub fn create_quad( pub fn create_cube() -> (Vec, Vec) { let vertex_data = [ // top (0, 0, 1) - Vertex::from(([-1, -1, 1], [0, 0, 1])), - Vertex::from(([1, -1, 1], [0, 0, 1])), - Vertex::from(([1, 1, 1], [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], [1, 0])), + Vertex::from(([1, 1, 1], [0, 0, 1], [1, 1])), + Vertex::from(([-1, 1, 1], [0, 0, 1], [0, 1])), // bottom (0, 0, -1) - Vertex::from(([-1, 1, -1], [0, 0, -1])), - Vertex::from(([1, 1, -1], [0, 0, -1])), - Vertex::from(([1, -1, -1], [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], [0, 0])), + Vertex::from(([1, -1, -1], [0, 0, -1], [0, 1])), + Vertex::from(([-1, -1, -1], [0, 0, -1], [1, 1])), // right (1, 0, 0) - Vertex::from(([1, -1, -1], [1, 0, 0])), - Vertex::from(([1, 1, -1], [1, 0, 0])), - Vertex::from(([1, 1, 1], [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], [1, 0])), + Vertex::from(([1, 1, 1], [1, 0, 0], [1, 1])), + Vertex::from(([1, -1, 1], [1, 0, 0], [0, 1])), // left (-1, 0, 0) - Vertex::from(([-1, -1, 1], [-1, 0, 0])), - Vertex::from(([-1, 1, 1], [-1, 0, 0])), - Vertex::from(([-1, 1, -1], [-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], [0, 0])), + Vertex::from(([-1, 1, -1], [-1, 0, 0], [0, 1])), + Vertex::from(([-1, -1, -1], [-1, 0, 0], [1, 1])), // front (0, 1, 0) - Vertex::from(([1, 1, -1], [0, 1, 0])), - Vertex::from(([-1, 1, -1], [0, 1, 0])), - Vertex::from(([-1, 1, 1], [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], [0, 0])), + Vertex::from(([-1, 1, 1], [0, 1, 0], [0, 1])), + Vertex::from(([1, 1, 1], [0, 1, 0], [1, 1])), // back (0, -1, 0) - Vertex::from(([1, -1, 1], [0, -1, 0])), - Vertex::from(([-1, -1, 1], [0, -1, 0])), - Vertex::from(([-1, -1, -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], [1, 0])), + Vertex::from(([-1, -1, -1], [0, -1, 0], [1, 1])), + Vertex::from(([1, -1, -1], [0, -1, 0], [0, 1])), ]; let index_data: &[u16] = &[ diff --git a/src/render/mod.rs b/src/render/mod.rs index 188debcc16..1bc47b31ed 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -50,6 +50,11 @@ pub fn get_vertex_buffer_descriptor<'a>() -> wgpu::VertexBufferDescriptor<'a> { offset: 4 * 4, shader_location: 1, }, + wgpu::VertexAttributeDescriptor { + format: wgpu::VertexFormat::Float2, + offset: 8 * 4, + shader_location: 2, + }, ], } } diff --git a/src/render/passes/forward/forward.vert b/src/render/passes/forward/forward.vert index 4cada407c1..e68bb9bed9 100644 --- a/src/render/passes/forward/forward.vert +++ b/src/render/passes/forward/forward.vert @@ -2,6 +2,7 @@ layout(location = 0) in vec4 a_Pos; layout(location = 1) in vec4 a_Normal; +layout(location = 2) in vec4 a_Uv; layout(location = 0) out vec3 v_Normal; layout(location = 1) out vec4 v_Position; diff --git a/src/render/passes/forward_instanced/forward_instanced.vert b/src/render/passes/forward_instanced/forward_instanced.vert index d59429dbee..291cfbc0e2 100644 --- a/src/render/passes/forward_instanced/forward_instanced.vert +++ b/src/render/passes/forward_instanced/forward_instanced.vert @@ -3,10 +3,11 @@ // vertex attributes layout(location = 0) in vec4 a_Pos; layout(location = 1) in vec4 a_Normal; +layout(location = 2) in vec2 a_Uv; // Instanced attributes -layout (location = 2) in vec3 a_instancePos; -layout (location = 3) in vec4 a_instanceColor; +layout (location = 3) in vec3 a_instancePos; +layout (location = 4) in vec4 a_instanceColor; layout(location = 0) out vec3 v_Normal; diff --git a/src/render/passes/forward_instanced/mod.rs b/src/render/passes/forward_instanced/mod.rs index 4d9824b26b..e98c228d41 100644 --- a/src/render/passes/forward_instanced/mod.rs +++ b/src/render/passes/forward_instanced/mod.rs @@ -199,12 +199,12 @@ impl Pipeline for ForwardInstancedPipeline { wgpu::VertexAttributeDescriptor { format: wgpu::VertexFormat::Float3, offset: 0, - shader_location: 2, + shader_location: 3, }, wgpu::VertexAttributeDescriptor { format: wgpu::VertexFormat::Float4, offset: 3 * 4, - shader_location: 3, + shader_location: 4, }, ], }; diff --git a/src/render/passes/forward_shadow/forward_shadow.vert b/src/render/passes/forward_shadow/forward_shadow.vert index 4cada407c1..5690d877d7 100644 --- a/src/render/passes/forward_shadow/forward_shadow.vert +++ b/src/render/passes/forward_shadow/forward_shadow.vert @@ -2,6 +2,7 @@ layout(location = 0) in vec4 a_Pos; layout(location = 1) in vec4 a_Normal; +layout(location = 2) in vec2 a_Uv; layout(location = 0) out vec3 v_Normal; layout(location = 1) out vec4 v_Position; diff --git a/src/render/passes/ui/mod.rs b/src/render/passes/ui/mod.rs index 7d13658414..42c3289a14 100644 --- a/src/render/passes/ui/mod.rs +++ b/src/render/passes/ui/mod.rs @@ -155,22 +155,22 @@ impl Pipeline for UiPipeline { wgpu::VertexAttributeDescriptor { format: wgpu::VertexFormat::Float2, offset: 0, - shader_location: 2, + shader_location: 3, }, wgpu::VertexAttributeDescriptor { format: wgpu::VertexFormat::Float2, offset: 2 * 4, - shader_location: 3, + shader_location: 4, }, wgpu::VertexAttributeDescriptor { format: wgpu::VertexFormat::Float4, offset: 4 * 4, - shader_location: 4, + shader_location: 5, }, wgpu::VertexAttributeDescriptor { format: wgpu::VertexFormat::Float, offset: 8 * 4, - shader_location: 5, + shader_location: 6, }, ], }; diff --git a/src/render/passes/ui/ui.vert b/src/render/passes/ui/ui.vert index 13b25a9795..5138b9f626 100644 --- a/src/render/passes/ui/ui.vert +++ b/src/render/passes/ui/ui.vert @@ -3,12 +3,13 @@ // vertex attributes layout(location = 0) in vec4 a_Pos; layout(location = 1) in vec4 a_Normal; +layout(location = 2) in vec2 a_Uv; // instanced attributes (RectData) -layout (location = 2) in vec2 a_RectPosition; -layout (location = 3) in vec2 a_RectSize; -layout (location = 4) in vec4 a_RectColor; -layout (location = 5) in float a_RectZIndex; +layout (location = 3) in vec2 a_RectPosition; +layout (location = 4) in vec2 a_RectSize; +layout (location = 5) in vec4 a_RectColor; +layout (location = 6) in float a_RectZIndex; layout(location = 0) out vec4 v_Color; diff --git a/src/render/vertex.rs b/src/render/vertex.rs index b6e4247469..92762b9dd2 100644 --- a/src/render/vertex.rs +++ b/src/render/vertex.rs @@ -6,33 +6,36 @@ use zerocopy::{AsBytes, FromBytes}; pub struct Vertex { pub position: [f32; 4], pub normal: [f32; 4], + pub uv: [f32; 2], } -impl From<([f32; 4], [f32; 4])> for Vertex { - fn from((position, normal): ([f32; 4], [f32; 4])) -> Self { +impl From<([f32; 4], [f32; 4], [f32; 2])> for Vertex { + fn from((position, normal, uv): ([f32; 4], [f32; 4], [f32; 2])) -> Self { Vertex { position: position, normal: normal, + uv: uv, } } } -impl From<([f32; 3], [f32; 3])> for Vertex { - fn from((position, normal): ([f32; 3], [f32; 3])) -> Self { +impl From<([f32; 3], [f32; 3], [f32; 2])> for Vertex { + fn from((position, normal, uv): ([f32; 3], [f32; 3], [f32; 2])) -> Self { Vertex { position: [ - position[0] as f32, - position[1] as f32, - position[2] as f32, + position[0], + position[1], + position[2], 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 { - fn from((position, normal): ([i8; 4], [i8; 4])) -> Self { +impl From<([i8; 4], [i8; 4], [i8; 2])> for Vertex { + fn from((position, normal, uv): ([i8; 4], [i8; 4], [i8; 2])) -> Self { Vertex { position: [ position[0] as f32, @@ -46,12 +49,16 @@ impl From<([i8; 4], [i8; 4])> for Vertex { normal[2] as f32, normal[3] as f32, ], + uv: [ + uv[0] as f32, + uv[1] as f32, + ], } } } -impl From<([i8; 3], [i8; 3])> for Vertex { - fn from((position, normal): ([i8; 3], [i8; 3])) -> Self { +impl From<([i8; 3], [i8; 3], [i8; 2])> for Vertex { + fn from((position, normal, uv): ([i8; 3], [i8; 3], [i8; 2])) -> Self { Vertex { position: [ position[0] as f32, @@ -60,6 +67,7 @@ impl From<([i8; 3], [i8; 3])> for Vertex { 1.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], } } }