wde_wgpu/utils/vertex.rs
1//! Vertex structure and layout for a mesh.
2
3/// Describe the vertex structure of a mesh.
4///
5/// # Fields
6///
7/// * `position` - The position of the vertex (location 0).
8/// * `uv` - The texture UV of the vertex (location 1).
9/// * `normal` - The normal of the vertex (location 2).
10#[repr(C)]
11#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable, Debug, Default)]
12pub struct Vertex {
13 /// The position of the vertex.
14 pub position: [f32; 3],
15 /// The texture UV of the vertex (must be between 0.0 and 1.0).
16 pub uv: [f32; 2],
17 /// The normal of the vertex (must be normalized).
18 pub normal: [f32; 3],
19 /// The tangent of the vertex (xyz: tangent, w: handedness).
20 pub tangent: [f32; 4]
21}
22
23impl Vertex {
24 /// Describe the layout of the vertex.
25 ///
26 /// # Returns
27 ///
28 /// * `wgpu::VertexBufferLayout` - The layout of the vertex.
29 ///
30 /// # Example
31 /// ```rust
32 /// use wde_wgpu::vertex::Vertex;
33 ///
34 /// let layout = Vertex::describe();
35 /// assert_eq!(layout.array_stride, std::mem::size_of::<Vertex>() as u64);
36 /// ```
37 pub fn describe<'a>() -> wgpu::VertexBufferLayout<'a> {
38 wgpu::VertexBufferLayout {
39 array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
40 step_mode: wgpu::VertexStepMode::Vertex,
41 attributes: &[
42 wgpu::VertexAttribute {
43 // Position
44 offset: 0,
45 shader_location: 0,
46 format: wgpu::VertexFormat::Float32x3
47 },
48 wgpu::VertexAttribute {
49 // UV
50 offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
51 shader_location: 1,
52 format: wgpu::VertexFormat::Float32x2
53 },
54 wgpu::VertexAttribute {
55 // Normal
56 offset: std::mem::size_of::<[f32; 5]>() as wgpu::BufferAddress,
57 shader_location: 2,
58 format: wgpu::VertexFormat::Float32x3
59 },
60 wgpu::VertexAttribute {
61 // Tangent
62 offset: std::mem::size_of::<[f32; 8]>() as wgpu::BufferAddress,
63 shader_location: 3,
64 format: wgpu::VertexFormat::Float32x4
65 }
66 ]
67 }
68 }
69}