Blacksite/crates/shared/src/material_asset.rs
Rbanh f29712d158
Some checks are pending
CI / Format, lint, test, build (push) Waiting to run
Initial project import
2026-06-05 21:44:45 -04:00

67 lines
2.0 KiB
Rust

//! Serializable material asset files under `assets/materials/`.
use serde::{Deserialize, Serialize};
use crate::{MaterialDesc, MaterialParameter, MaterialShaderKind, MaterialTextureBinding};
/// RON material asset: label plus authoring [`MaterialDesc`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MaterialAsset {
pub label: String,
#[serde(default)]
pub shader: Option<String>,
pub material: MaterialDesc,
}
impl MaterialAsset {
pub fn load_from_path(catalog_path: &str) -> Result<Self, String> {
let text = std::fs::read_to_string(catalog_path)
.map_err(|err| format!("could not read {catalog_path}: {err}"))?;
ron::from_str(&text).map_err(|err| format!("invalid material RON in {catalog_path}: {err}"))
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ShaderPropertyType {
Bool,
Float { min: Option<f32>, max: Option<f32> },
Vec2,
Vec3,
Color,
Enum { options: Vec<String> },
Texture,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ShaderPropertyDesc {
pub name: String,
pub display_name: String,
#[serde(default)]
pub group: String,
pub property_type: ShaderPropertyType,
}
/// RON shader schema describing material inspector parameters.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ShaderSchemaAsset {
pub label: String,
pub kind: MaterialShaderKind,
#[serde(default)]
pub wgsl_path: Option<String>,
#[serde(default)]
pub parameters: Vec<ShaderPropertyDesc>,
#[serde(default)]
pub default_values: Vec<MaterialParameter>,
#[serde(default)]
pub default_textures: Vec<MaterialTextureBinding>,
}
impl ShaderSchemaAsset {
pub fn load_from_path(catalog_path: &str) -> Result<Self, String> {
let text = std::fs::read_to_string(catalog_path)
.map_err(|err| format!("could not read {catalog_path}: {err}"))?;
ron::from_str(&text)
.map_err(|err| format!("invalid shader schema RON in {catalog_path}: {err}"))
}
}