use wgpu::vertex_attr_array; #[repr(C)] #[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)] pub struct Vertex { position: [f32; 3], color: [f32; 3], uv: [f32; 2], } impl Vertex{ const ATTRIB : [wgpu::VertexAttribute; 3]= vertex_attr_array![0 => Float32x3, 1 => Float32x3, 2 => Float32x2]; pub fn desc() -> wgpu::VertexBufferLayout<'static>{ wgpu::VertexBufferLayout{ array_stride: std::mem::size_of::() as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, attributes: &Self::ATTRIB } } } #[derive(Default, Debug, Clone)] pub struct Mesh { vertices: Vec, } impl Mesh{ pub fn len(&self) -> u32{ return self.vertices.len().try_into().unwrap() } pub fn vertices(self)-> Vec{ self.vertices } pub fn positions_list(&self) -> Vec<[f32; 3]> { self.vertices.iter().map(|vertex| vertex.position).collect() } } pub fn generate_plane(num_segx: u32, num_segy: u32, width: f32, height: f32) -> Mesh { let mut result = Mesh::default(); let normal: [f32; 3] = [0.0, 0.0, 1.0]; // Ensure num_segx and num_segy are not zero to avoid division by zero if num_segx == 0 || num_segy == 0 { panic!("Number of segments cannot be zero."); } let xstep = width / num_segx as f32; let ystep = height / num_segy as f32; let width_half = width / 2.0; let height_half = height / 2.0; let mut x = -width_half; let mut y = -height_half; let mut seg_y = 0; while seg_y < num_segy { let mut seg_x = 0; x= -width_half; while seg_x < num_segx { //println!("Boucle interne numéro {}, avec y = {}",seg_x,seg_y); let x0 = x; let y0 = y; let x1 = x + xstep; let y1 = y + ystep; // Add first triangle result.vertices.push(Vertex { position: [x0, y0, 0.0], color: [1.0, 0.0, 0.0], // Replace with desired color uv: [1.0 - (x0 + width_half) / width, (y0 + height_half) / height], }); result.vertices.push(Vertex { position: [x1, y0, 0.0], color: [0.0, 1.0, 0.0], // Replace with desired color uv: [1.0 - (x1 + width_half) / width, (y0 + height_half) / height], }); result.vertices.push(Vertex { position: [x0, y1, 0.0], color: [0.0, 0.0, 1.0], // Replace with desired color uv: [1.0 - (x0 + width_half) / width, (y1 + height_half) / height], }); // Add second triangle (degenerates into a line if numSegX or numSegY is 1) result.vertices.push(Vertex { position: [x0, y1, 0.0], color: [0.0, 0.0, 1.0], // Replace with desired color (same as previous) uv: [1.0 - (x0 + width_half) / width, (y1 + height_half) / height], }); result.vertices.push(Vertex { position: [x1, y0, 0.0], color: [0.0, 1.0, 0.0], // Replace with desired color (same as previous) uv: [1.0 - (x1 + width_half) / width, (y0 + height_half) / height], }); result.vertices.push(Vertex { position: [x1, y1, 0.0], color: [1.0, 0.0, 0.0], // Replace with desired color (same as previous) uv: [1.0 - (x1 + width_half) / width, (y1 + height_half) / height], }); x += xstep; seg_x += 1; } y += ystep; seg_y += 1; } result } pub fn change_plane(buff: wgpu::Buffer){ println!("{:?}", buff.usage()) }