Skip to main content

oxide_asset/
mesh_cache.rs

1//! GPU mesh cache for sharing meshes across entities.
2
3use oxide_renderer::mesh::Mesh3D;
4
5use super::{Assets, Handle};
6
7/// Resource that caches GPU meshes by handle.
8pub struct MeshCache {
9    meshes: Assets<Mesh3D>,
10}
11
12impl MeshCache {
13    /// Creates an empty mesh cache.
14    pub fn new() -> Self {
15        Self {
16            meshes: Assets::new(),
17        }
18    }
19
20    /// Inserts a mesh with the given handle.
21    pub fn insert(&mut self, handle: Handle<Mesh3D>, mesh: Mesh3D) {
22        self.meshes.insert(handle, mesh);
23    }
24
25    /// Gets a mesh by handle.
26    pub fn get(&self, handle: Handle<Mesh3D>) -> Option<&Mesh3D> {
27        self.meshes.get(&handle)
28    }
29
30    /// Removes a mesh by handle.
31    pub fn remove(&mut self, handle: Handle<Mesh3D>) -> Option<Mesh3D> {
32        self.meshes.remove(&handle)
33    }
34
35    /// Returns the number of cached meshes.
36    pub fn len(&self) -> usize {
37        self.meshes.len()
38    }
39
40    /// Returns true if the cache is empty.
41    pub fn is_empty(&self) -> bool {
42        self.meshes.is_empty()
43    }
44}
45
46impl Default for MeshCache {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52/// Component that references a mesh and material for rendering.
53#[derive(Clone, Debug)]
54pub struct MeshFilter {
55    /// Handle to the cached mesh.
56    pub mesh: Handle<Mesh3D>,
57}
58
59impl MeshFilter {
60    /// Creates a new mesh filter.
61    pub fn new(mesh: Handle<Mesh3D>) -> Self {
62        Self { mesh }
63    }
64}