Skip to main content

rustial_renderer_wgpu/gpu/
model_vertex.rs

1//! Vertex type for 3D model rendering.
2
3use bytemuck::{Pod, Zeroable};
4
5/// A vertex for 3D model geometry with position, normal, and UV.
6#[repr(C)]
7#[derive(Debug, Clone, Copy, Pod, Zeroable)]
8pub struct ModelVertex {
9    /// Position `[x, y, z]` in model space.
10    pub position: [f32; 3],
11    /// Normal vector `[nx, ny, nz]`.
12    pub normal: [f32; 3],
13    /// Texture coordinate `[u, v]`.
14    pub uv: [f32; 2],
15}
16
17impl ModelVertex {
18    /// Vertex buffer layout for the model pipeline.
19    pub fn layout() -> wgpu::VertexBufferLayout<'static> {
20        wgpu::VertexBufferLayout {
21            array_stride: std::mem::size_of::<ModelVertex>() as wgpu::BufferAddress,
22            step_mode: wgpu::VertexStepMode::Vertex,
23            attributes: &[
24                wgpu::VertexAttribute {
25                    offset: 0,
26                    shader_location: 0,
27                    format: wgpu::VertexFormat::Float32x3,
28                },
29                wgpu::VertexAttribute {
30                    offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
31                    shader_location: 1,
32                    format: wgpu::VertexFormat::Float32x3,
33                },
34                wgpu::VertexAttribute {
35                    offset: (std::mem::size_of::<[f32; 3]>() * 2) as wgpu::BufferAddress,
36                    shader_location: 2,
37                    format: wgpu::VertexFormat::Float32x2,
38                },
39            ],
40        }
41    }
42}