bones3_remesh/ecs/
resources.rs

1//! This module contains the resources that may be used to generate chunk meshes
2//! and interact with the remesh systems.
3
4use bevy::prelude::*;
5use bevy::utils::HashMap;
6
7/// This resource contains an indexed list of material handles that are used by
8/// blocks when generating chunk meshes.
9#[derive(Resource, Default)]
10pub struct ChunkMaterialList {
11    /// The indexed list of material handles.
12    materials: Vec<Handle<StandardMaterial>>,
13
14    /// Material names and their corresponding index values within the material
15    /// list.
16    material_keys: HashMap<String, u16>,
17}
18
19impl ChunkMaterialList {
20    /// Adds a new material to the chunk material list.
21    ///
22    /// This function returns the index of the newly added material.
23    pub fn add_material(
24        &mut self,
25        material: Handle<StandardMaterial>,
26        name: Option<String>,
27    ) -> u16 {
28        self.materials.push(material);
29        let index = (self.materials.len() - 1) as u16;
30
31        if let Some(material_name) = name {
32            self.material_keys.insert(material_name, index);
33        }
34
35        index
36    }
37
38    /// Gets a copy of the material handle at the given material index.
39    pub fn get_material(&self, index: u16) -> Handle<StandardMaterial> {
40        self.materials[index as usize].clone()
41    }
42
43    /// Tries to find a material within this material list with the given name.
44    ///
45    /// Returns the index of the material, or `None` if the material could not
46    /// be found.
47    pub fn find_material(&self, name: &str) -> Option<u16> {
48        self.material_keys.get(name).copied()
49    }
50}