1use crate::ngui::{Object, V, st, vector};
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum MeshVersion {
10 V2019,
12 V2022,
14 V6000,
16}
17impl MeshVersion {
18 #[must_use]
20 pub fn version(self) -> &'static str {
21 match self {
22 Self::V2019 => "2019.4.0f1",
23 Self::V2022 => "2022.3.0f1",
24 Self::V6000 => "6000.3.0f1",
25 }
26 }
27}
28#[must_use]
30pub fn vec3(v: [f32; 3]) -> V {
31 st(
32 "Vector3f",
33 vec![
34 ("x", V::Float(v[0])),
35 ("y", V::Float(v[1])),
36 ("z", V::Float(v[2])),
37 ],
38 )
39}
40#[must_use]
42pub fn aabb(center: [f32; 3], extent: [f32; 3]) -> V {
43 st(
44 "AABB",
45 vec![("m_Center", vec3(center)), ("m_Extent", vec3(extent))],
46 )
47}
48#[must_use]
50pub fn matrix(values: [f32; 16]) -> V {
51 let names = [
52 "e00", "e01", "e02", "e03", "e10", "e11", "e12", "e13", "e20", "e21", "e22", "e23", "e30",
53 "e31", "e32", "e33",
54 ];
55 V::Struct(
56 "Matrix4x4f".to_owned(),
57 names
58 .into_iter()
59 .zip(values)
60 .map(|(n, v)| (n.to_owned(), V::Float(v)))
61 .collect(),
62 )
63}
64#[must_use]
66pub fn blend_vertex(index: u32, vertex: [f32; 3], normal: [f32; 3], tangent: [f32; 3]) -> V {
67 st(
68 "BlendShapeVertex",
69 vec![
70 ("vertex", vec3(vertex)),
71 ("normal", vec3(normal)),
72 ("tangent", vec3(tangent)),
73 ("index", V::UInt(index)),
74 ],
75 )
76}
77#[must_use]
79pub fn blend_frame(first: u32, count: u32, normals: bool, tangents: bool) -> V {
80 st(
81 "MeshBlendShape",
82 vec![
83 ("firstVertex", V::UInt(first)),
84 ("vertexCount", V::UInt(count)),
85 ("hasNormals", V::Bool(normals)),
86 ("hasTangents", V::Bool(tangents)),
87 ],
88 )
89}
90#[must_use]
92pub fn blend_channel(name: &[u8], hash: u32, first: i32, count: i32) -> V {
93 st(
94 "MeshBlendShapeChannel",
95 vec![
96 ("name", V::Str(name.to_vec())),
97 ("nameHash", V::UInt(hash)),
98 ("frameIndex", V::Int(first)),
99 ("frameCount", V::Int(count)),
100 ],
101 )
102}
103#[must_use]
105pub fn blend_shapes(vertices: Vec<V>, frames: Vec<V>, channels: Vec<V>, weights: Vec<f32>) -> V {
106 let frame_shape = frames
108 .first()
109 .cloned()
110 .unwrap_or_else(|| blend_frame(0, 0, false, false));
111 st(
112 "BlendShapeData",
113 vec![
114 (
115 "vertices",
116 vector(blend_vertex(0, [0.0; 3], [0.0; 3], [0.0; 3]), vertices),
117 ),
118 ("shapes", vector(frame_shape, frames)),
119 ("channels", vector(blend_channel(b"", 0, 0, 0), channels)),
120 (
121 "fullWeights",
122 vector(V::Float(0.0), weights.into_iter().map(V::Float).collect()),
123 ),
124 ],
125 )
126}
127#[must_use]
129pub fn lod_info(levels: u32, bias: f32, slope: f32, subs: &[Vec<(u32, u32)>]) -> V {
130 let range = |start, count| {
131 st(
132 "MeshLodRange",
133 vec![
134 ("m_IndexStart", V::UInt(start)),
135 ("m_IndexCount", V::UInt(count)),
136 ],
137 )
138 };
139 let sub = |ranges: &[(u32, u32)]| {
140 st(
141 "MeshLodSubMesh",
142 vec![(
143 "m_Levels",
144 vector(
145 range(0, 0),
146 ranges.iter().map(|&(s, c)| range(s, c)).collect(),
147 ),
148 )],
149 )
150 };
151 st(
152 "MeshLodInfo",
153 vec![
154 ("m_NumLevels", V::UInt(levels)),
155 (
156 "m_SubMeshes",
157 vector(sub(&[]), subs.iter().map(|v| sub(v)).collect()),
158 ),
159 (
160 "m_LodSelectionCurve",
161 st(
162 "LodSelectionCurve",
163 vec![
164 ("m_LodBias", V::Float(bias)),
165 ("m_LodSlope", V::Float(slope)),
166 ],
167 ),
168 ),
169 ],
170 )
171}
172#[must_use]
174pub fn submesh(
175 first_byte: u32,
176 index_count: u32,
177 topology: i32,
178 base_vertex: u32,
179 first_vertex: u32,
180 vertex_count: u32,
181) -> V {
182 st(
183 "SubMesh",
184 vec![
185 ("firstByte", V::UInt(first_byte)),
186 ("indexCount", V::UInt(index_count)),
187 ("topology", V::Int(topology)),
188 ("baseVertex", V::UInt(base_vertex)),
189 ("firstVertex", V::UInt(first_vertex)),
190 ("vertexCount", V::UInt(vertex_count)),
191 ("localAABB", aabb([0.0; 3], [1.0; 3])),
192 ],
193 )
194}
195fn packed(float: bool) -> V {
196 let mut fields = vec![("m_NumItems", V::UInt(0))];
197 if float {
198 fields.extend([("m_Range", V::Float(0.0)), ("m_Start", V::Float(0.0))]);
199 }
200 fields.extend([
201 ("m_Data", vector(V::UInt8(0), vec![])),
202 ("m_BitSize", V::UInt8(0)),
203 ]);
204 st("PackedBitVector", fields)
205}
206#[must_use]
208pub fn compressed() -> V {
209 st(
210 "CompressedMesh",
211 vec![
212 ("m_Vertices", packed(true)),
213 ("m_UV", packed(true)),
214 ("m_Normals", packed(true)),
215 ("m_Tangents", packed(true)),
216 ("m_Weights", packed(false)),
217 ("m_NormalSigns", packed(false)),
218 ("m_TangentSigns", packed(false)),
219 ("m_FloatColors", packed(true)),
220 ("m_BoneIndices", packed(false)),
221 ("m_Triangles", packed(false)),
222 ("m_UVInfo", V::UInt(0)),
223 ],
224 )
225}
226#[must_use]
229pub fn mesh(
230 path_id: i64,
231 name: &[u8],
232 vertices: &[[f32; 3]],
233 indices: &[u32],
234 version: MeshVersion,
235) -> Object {
236 let mut data: Vec<u8> = vertices
237 .iter()
238 .flatten()
239 .flat_map(|v| v.to_le_bytes())
240 .collect();
241 data.resize(data.len().next_multiple_of(16), 0);
242 let channel = |dimension| {
243 st(
244 "ChannelInfo",
245 vec![
246 ("stream", V::UInt8(0)),
247 ("offset", V::UInt8(0)),
248 ("format", V::UInt8(0)),
249 ("dimension", V::UInt8(dimension)),
250 ],
251 )
252 };
253 let mut channels = vec![channel(3)];
254 channels.extend((1..14).map(|_| channel(0)));
255 let count = u32::try_from(vertices.len()).unwrap_or(u32::MAX);
256 let mut fields = vec![
257 ("m_Name", V::Str(name.to_vec())),
258 (
259 "m_SubMeshes",
260 vector(
261 submesh(0, 0, 0, 0, 0, 0),
262 vec![submesh(
263 0,
264 u32::try_from(indices.len()).unwrap_or(u32::MAX),
265 0,
266 0,
267 0,
268 count,
269 )],
270 ),
271 ),
272 ("m_Shapes", blend_shapes(vec![], vec![], vec![], vec![])),
273 ("m_BindPose", vector(matrix([0.0; 16]), vec![])),
274 ("m_BoneNameHashes", vector(V::UInt(0), vec![])),
275 ("m_RootBoneNameHash", V::UInt(0)),
276 ("m_MeshCompression", V::UInt8(0)),
277 ("m_IndexFormat", V::Int(1)),
278 (
279 "m_IndexBuffer",
280 vector(
281 V::UInt8(0),
282 indices
283 .iter()
284 .flat_map(|v| v.to_le_bytes())
285 .map(V::UInt8)
286 .collect(),
287 ),
288 ),
289 (
290 "m_VertexData",
291 st(
292 "VertexData",
293 vec![
294 ("m_VertexCount", V::UInt(count)),
295 ("m_Channels", vector(channel(0), channels)),
296 ("m_DataSize", V::Bytes(data)),
297 ],
298 ),
299 ),
300 ("m_CompressedMesh", compressed()),
301 ("m_LocalAABB", aabb([0.0; 3], [1.0; 3])),
302 ("m_MeshUsageFlags", V::Int(0)),
303 ];
304 if version == MeshVersion::V6000 {
305 fields.push(("m_MeshLodInfo", lod_info(0, 0.0, 0.0, &[])));
306 }
307 Object::new(path_id, 43, st("Mesh", fields))
308}