use crate::render::asset_id::MaterialId;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, enum2schema::Schema)]
pub struct MaterialRef {
pub name: String,
#[serde(skip)]
#[schema(skip)]
pub id: Option<MaterialId>,
}
impl MaterialRef {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
id: None,
}
}
pub fn with_id(name: impl Into<String>, id: MaterialId) -> Self {
Self {
name: name.into(),
id: Some(id),
}
}
}
impl Default for MaterialRef {
fn default() -> Self {
Self {
name: "Default".to_string(),
id: None,
}
}
}
impl From<String> for MaterialRef {
fn from(name: String) -> Self {
Self { name, id: None }
}
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct MaterialVariants {
pub default_material_name: String,
pub mappings: Vec<MaterialVariantMapping>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MaterialVariantMapping {
pub variant_names: Vec<String>,
pub material_name: String,
}
impl MaterialVariants {
pub fn material_for_variant(&self, variant_name: &str) -> &str {
for mapping in &self.mappings {
if mapping.variant_names.iter().any(|n| n == variant_name) {
return &mapping.material_name;
}
}
&self.default_material_name
}
}
impl From<&str> for MaterialRef {
fn from(name: &str) -> Self {
Self {
name: name.to_string(),
id: None,
}
}
}