1use crate::{
2 app::App,
3 assets::{plugin::AssetPlugin, upload::Asset},
4 ecs::plugin::Plugin,
5 wgpu::backend::WGPUBackend,
6};
7
8pub struct Mesh {
9 pub vertices: Vec<u8>,
10 pub indices: Vec<u32>,
11}
12
13pub struct GPUMesh {
14 pub vertex_buffer: wgpu::Buffer,
15 pub index_buffer: wgpu::Buffer,
16 pub index_count: u32,
17}
18
19impl Asset<WGPUBackend> for GPUMesh {
20 type Source = Mesh;
21 type Deps<'a> = ();
22
23 fn upload<'a>(source: &Mesh, backend: &WGPUBackend, _deps: &()) -> Option<Self> {
24 use wgpu::util::DeviceExt;
25 let vertex_buffer = backend
26 .device
27 .create_buffer_init(&wgpu::util::BufferInitDescriptor {
28 label: Some("Mesh Vertex Buffer"),
29 contents: &source.vertices,
30 usage: wgpu::BufferUsages::VERTEX,
31 });
32 let index_buffer = backend
33 .device
34 .create_buffer_init(&wgpu::util::BufferInitDescriptor {
35 label: Some("Mesh Index Buffer"),
36 contents: bytemuck::cast_slice(&source.indices),
37 usage: wgpu::BufferUsages::INDEX,
38 });
39 Some(Self {
40 vertex_buffer,
41 index_buffer,
42 index_count: source.indices.len() as u32,
43 })
44 }
45}
46
47pub struct MeshPlugin;
48impl MeshPlugin {
49 pub fn new() -> Self {
50 Self
51 }
52}
53impl Plugin for MeshPlugin {
54 fn build(&self, app: &mut App) {
55 app.add_plugin(AssetPlugin::<WGPUBackend, GPUMesh>::new());
56 }
57}