mesh.rs (3473B)
1 use eframe::egui_wgpu::wgpu; 2 3 #[repr(C)] 4 #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] 5 pub struct Vertex { 6 pos: [f32; 3], 7 normal: [f32; 3], 8 } 9 10 impl Vertex { 11 pub const ATTRS: [wgpu::VertexAttribute; 2] = wgpu::vertex_attr_array![ 12 0 => Float32x3, // position 13 1 => Float32x3 // normal 14 ]; 15 pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout { 16 array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress, 17 step_mode: wgpu::VertexStepMode::Vertex, 18 attributes: &Self::ATTRS, 19 }; 20 } 21 22 #[repr(C)] 23 #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] 24 pub struct Instance { 25 pub base_pos: [f32; 3], 26 pub scale: f32, 27 pub seed: f32, 28 pub color: [f32; 3], 29 } 30 31 impl Instance { 32 pub const ATTRS: [wgpu::VertexAttribute; 4] = wgpu::vertex_attr_array![ 33 2 => Float32x3, // base_pos 34 3 => Float32, // scale 35 4 => Float32, // seed 36 5 => Float32x3 // color 37 ]; 38 pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout { 39 array_stride: std::mem::size_of::<Instance>() as wgpu::BufferAddress, 40 step_mode: wgpu::VertexStepMode::Instance, 41 attributes: &Self::ATTRS, 42 }; 43 } 44 45 // 6 faces * 4 verts. Each face has a constant normal. 46 #[rustfmt::skip] 47 pub const CUBE_VERTICES: [Vertex; 24] = [ 48 // -Z (back) 49 Vertex { pos: [-0.5,-0.5,-0.5], normal: [0.0, 0.0,-1.0] }, 50 Vertex { pos: [ 0.5,-0.5,-0.5], normal: [0.0, 0.0,-1.0] }, 51 Vertex { pos: [ 0.5, 0.5,-0.5], normal: [0.0, 0.0,-1.0] }, 52 Vertex { pos: [-0.5, 0.5,-0.5], normal: [0.0, 0.0,-1.0] }, 53 54 // +Z (front) 55 Vertex { pos: [-0.5,-0.5, 0.5], normal: [0.0, 0.0, 1.0] }, 56 Vertex { pos: [ 0.5,-0.5, 0.5], normal: [0.0, 0.0, 1.0] }, 57 Vertex { pos: [ 0.5, 0.5, 0.5], normal: [0.0, 0.0, 1.0] }, 58 Vertex { pos: [-0.5, 0.5, 0.5], normal: [0.0, 0.0, 1.0] }, 59 60 // -X (left) 61 Vertex { pos: [-0.5,-0.5,-0.5], normal: [-1.0, 0.0, 0.0] }, 62 Vertex { pos: [-0.5, 0.5,-0.5], normal: [-1.0, 0.0, 0.0] }, 63 Vertex { pos: [-0.5, 0.5, 0.5], normal: [-1.0, 0.0, 0.0] }, 64 Vertex { pos: [-0.5,-0.5, 0.5], normal: [-1.0, 0.0, 0.0] }, 65 66 // +X (right) 67 Vertex { pos: [ 0.5,-0.5,-0.5], normal: [1.0, 0.0, 0.0] }, 68 Vertex { pos: [ 0.5, 0.5,-0.5], normal: [1.0, 0.0, 0.0] }, 69 Vertex { pos: [ 0.5, 0.5, 0.5], normal: [1.0, 0.0, 0.0] }, 70 Vertex { pos: [ 0.5,-0.5, 0.5], normal: [1.0, 0.0, 0.0] }, 71 72 // -Y (bottom) 73 Vertex { pos: [-0.5,-0.5,-0.5], normal: [0.0,-1.0, 0.0] }, 74 Vertex { pos: [-0.5,-0.5, 0.5], normal: [0.0,-1.0, 0.0] }, 75 Vertex { pos: [ 0.5,-0.5, 0.5], normal: [0.0,-1.0, 0.0] }, 76 Vertex { pos: [ 0.5,-0.5,-0.5], normal: [0.0,-1.0, 0.0] }, 77 78 // +Y (top) 79 Vertex { pos: [-0.5, 0.5,-0.5], normal: [0.0, 1.0, 0.0] }, 80 Vertex { pos: [-0.5, 0.5, 0.5], normal: [0.0, 1.0, 0.0] }, 81 Vertex { pos: [ 0.5, 0.5, 0.5], normal: [0.0, 1.0, 0.0] }, 82 Vertex { pos: [ 0.5, 0.5,-0.5], normal: [0.0, 1.0, 0.0] }, 83 ]; 84 85 // 6 faces * 2 triangles * 3 indices — all CCW when viewed from the outside 86 pub const CUBE_INDICES: [u16; 36] = [ 87 // -Z (back) normal (0, 0,-1) 88 0, 3, 2, 0, 2, 1, // +Z (front) normal (0, 0, 1) 89 4, 5, 6, 4, 6, 7, // -X (left) normal (-1,0, 0) 90 8, 11, 10, 8, 10, 9, // +X (right) normal ( 1,0, 0) 91 12, 13, 14, 12, 14, 15, // -Y (bottom) normal (0,-1, 0) 92 16, 18, 17, 16, 19, 18, // +Y (top) normal (0, 1, 0) 93 20, 21, 22, 20, 22, 23, 94 ];