use super::{pptr::PPtr, shader::Shader, texture2d::Texture2D, vectors::ColorRGBA}; use crate::object::{Value, parser::FromValue}; use glam::Vec2; use serde::Serialize; use std::collections::BTreeMap; #[derive(Debug, Serialize)] pub struct Material { pub name: String, pub shader: PPtr, pub properties: UnityPropertySheet, pub lightmap_flags: u32, pub double_sided_gi: bool, pub custom_render_queue: i32, pub enable_instancing_variants: bool, } #[derive(Debug, Serialize)] pub struct UnityPropertySheet { pub colors: BTreeMap, pub floats: BTreeMap, pub textures: BTreeMap, } #[derive(Debug, Serialize)] pub struct UnityTexEnv { pub offset: Vec2, pub scale: Vec2, pub texture: PPtr, } impl UnityPropertySheet { pub fn textures(&self) -> impl Iterator { self.textures.iter().filter(|(_, v)| !v.texture.is_null()) } } impl FromValue for Material { fn from_value(v: Value) -> anyhow::Result { let mut fields = v.as_class("Material")?; Ok(Self { custom_render_queue: fields.field("m_CustomRenderQueue")?, double_sided_gi: fields.field("m_DoubleSidedGI")?, properties: fields.field("m_SavedProperties")?, enable_instancing_variants: fields.field("m_EnableInstancingVariants")?, lightmap_flags: fields.field("m_LightmapFlags")?, name: fields.field("m_Name")?, shader: fields.field("m_Shader")?, }) } } impl FromValue for UnityPropertySheet { fn from_value(v: Value) -> anyhow::Result { let mut fields = v.as_class("UnityPropertySheet")?; Ok(Self { colors: fields.field("m_Colors")?, floats: fields.field("m_Floats")?, textures: fields.field("m_TexEnvs")?, }) } } impl FromValue for UnityTexEnv { fn from_value(v: Value) -> anyhow::Result { let mut fields = v.as_class("UnityTexEnv")?; Ok(Self { offset: fields.field("m_Offset")?, scale: fields.field("m_Scale")?, texture: fields.field("m_Texture")?, }) } }