aboutsummaryrefslogtreecommitdiff
path: root/src/classes/material.rs
blob: 9124b0bdc83c6c80fafad9a75d932266c168af19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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<Shader>,
    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<String, ColorRGBA>,
    pub floats: BTreeMap<String, f32>,
    pub textures: BTreeMap<String, UnityTexEnv>,
}

#[derive(Debug, Serialize)]
pub struct UnityTexEnv {
    pub offset: Vec2,
    pub scale: Vec2,
    pub texture: PPtr<Texture2D>,
}

impl UnityPropertySheet {
    pub fn textures(&self) -> impl Iterator<Item = (&String, &UnityTexEnv)> {
        self.textures.iter().filter(|(_, v)| !v.texture.is_null())
    }
}

impl FromValue for Material {
    fn from_value(v: Value) -> anyhow::Result<Self> {
        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<Self> {
        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<Self> {
        let mut fields = v.as_class("UnityTexEnv")?;
        Ok(Self {
            offset: fields.field("m_Offset")?,
            scale: fields.field("m_Scale")?,
            texture: fields.field("m_Texture")?,
        })
    }
}