Skip to main content

unityrs_testkit/scene3d/
material.rs

1// Ported from UnityPy 1.25.2 (MIT): generated Material and Shader field lists. See THIRD_PARTY_LICENSES.
2//! Small public-format material and shader facts on hand-built embedded trees.
3//! These are synthetic field subsets, not complete Editor layouts.
4use crate::ngui::{Object, V, pair, pptr, st};
5
6/// A texture environment in Unity's bottom-left UV space.
7#[must_use]
8pub fn tex_env(texture: (i32, i64), scale: [f32; 2], offset: [f32; 2]) -> V {
9    st(
10        "UnityTexEnv",
11        vec![
12            ("m_Texture", pptr("Texture", texture)),
13            ("m_Scale", super::xyz("Vector2f", &scale)),
14            ("m_Offset", super::xyz("Vector2f", &offset)),
15        ],
16    )
17}
18
19fn color(value: [f32; 4]) -> V {
20    st(
21        "ColorRGBA",
22        ["r", "g", "b", "a"]
23            .iter()
24            .zip(value)
25            .map(|(name, x)| (*name, V::Float(x)))
26            .collect(),
27    )
28}
29fn map(shape: V, entries: Vec<V>) -> V {
30    V::Vector("map".to_owned(), Box::new(shape), entries)
31}
32fn name(value: &str) -> V {
33    V::Str(value.as_bytes().to_vec())
34}
35
36/// A Material with explicitly supplied properties and default render queue.
37/// Entries are kept in caller order; duplicate names can exercise hostile input.
38#[must_use]
39pub fn material(
40    path_id: i64,
41    material_name: &[u8],
42    shader: (i32, i64),
43    floats: &[(&str, f32)],
44    colors: &[(&str, [f32; 4])],
45    textures: &[(&str, V)],
46) -> Object {
47    Object::new(
48        path_id,
49        21,
50        st(
51            "Material",
52            vec![
53                ("m_Name", V::Str(material_name.to_vec())),
54                ("m_Shader", pptr("Shader", shader)),
55                ("m_CustomRenderQueue", V::Int(-1)),
56                (
57                    "m_SavedProperties",
58                    st(
59                        "UnityPropertySheet",
60                        vec![
61                            (
62                                "m_Floats",
63                                map(
64                                    pair(name(""), V::Float(0.)),
65                                    floats
66                                        .iter()
67                                        .map(|(n, v)| pair(name(n), V::Float(*v)))
68                                        .collect(),
69                                ),
70                            ),
71                            (
72                                "m_Colors",
73                                map(
74                                    pair(name(""), color([0.; 4])),
75                                    colors
76                                        .iter()
77                                        .map(|(n, v)| pair(name(n), color(*v)))
78                                        .collect(),
79                                ),
80                            ),
81                            (
82                                "m_TexEnvs",
83                                map(
84                                    pair(name(""), tex_env((0, 0), [1.; 2], [0.; 2])),
85                                    textures
86                                        .iter()
87                                        .map(|(n, v)| pair(name(n), v.clone()))
88                                        .collect(),
89                                ),
90                            ),
91                        ],
92                    ),
93                ),
94            ],
95        ),
96    )
97}
98
99/// A Shader's legacy name and modern parsed-form name, read by full parse.
100#[must_use]
101pub fn shader(path_id: i64, shader_name: &[u8]) -> Object {
102    Object::new(
103        path_id,
104        48,
105        st(
106            "Shader",
107            vec![
108                ("m_Name", V::Str(shader_name.to_vec())),
109                (
110                    "m_ParsedForm",
111                    st(
112                        "SerializedShader",
113                        vec![("m_Name", V::Str(shader_name.to_vec()))],
114                    ),
115                ),
116            ],
117        ),
118    )
119}