1use crate::{crs, Result};
5
6#[derive(Debug, Clone)]
8pub struct Triangle {
9 pub vertices: [u16; 3],
11 pub normals: [u16; 3],
13}
14
15#[derive(Debug, Clone)]
17pub struct SubMesh {
18 pub name: String,
20 pub node: i32,
22 pub rotation: [f32; 4],
24 pub translation: [f32; 3],
26 pub bounds_min: [f32; 3],
28 pub bounds_max: [f32; 3],
30 pub vertices: Vec<[f32; 3]>,
32 pub normals: Vec<[f32; 3]>,
34 pub triangles: Vec<Triangle>,
36 pub edges: Vec<[u16; 2]>,
38}
39
40#[derive(Debug, Clone)]
42pub struct PickingMesh {
43 pub source: String,
45 pub name: String,
47 pub skinned: bool,
49 pub meshes: Vec<SubMesh>,
51}
52
53fn shorts<const N: usize>(r: &mut crs::Reader<'_>) -> Result<[u16; N]> {
54 let mut out = [0; N];
55 for n in &mut out {
56 *n = u16::from_le_bytes(r.bytes(2)?.try_into().unwrap());
57 }
58 Ok(out)
59}
60
61pub fn picking_mesh(data: &[u8]) -> Result<PickingMesh> {
63 crs::parse(data, |r, _| {
64 r.block(|r, _| {
65 let source = r.string()?;
66 let name = r.string()?;
67 let skinned = r.bool()?;
68 let mut meshes = Vec::new();
69 for _ in 0..r.count()? {
70 let name = r.string()?;
71 let node = r.i32()?;
72 let rotation = r.f32s()?;
73 let translation = r.f32s()?;
74 let bounds_min = r.f32s()?;
75 let bounds_max = r.f32s()?;
76 let mut vertices = Vec::new();
77 for _ in 0..r.count()? {
78 vertices.push(r.f32s()?);
79 }
80 let mut normals = Vec::new();
81 for _ in 0..r.count()? {
82 normals.push(r.f32s()?);
83 }
84 let mut triangles = Vec::new();
85 for _ in 0..r.count()? {
86 triangles.push(Triangle {
87 vertices: shorts(r)?,
88 normals: shorts(r)?,
89 });
90 }
91 let mut edges = Vec::new();
92 for _ in 0..r.count()? {
93 edges.push(shorts(r)?);
94 }
95 meshes.push(SubMesh {
96 name,
97 node,
98 rotation,
99 translation,
100 bounds_min,
101 bounds_max,
102 vertices,
103 normals,
104 triangles,
105 edges,
106 });
107 }
108 Ok(PickingMesh {
109 source,
110 name,
111 skinned,
112 meshes,
113 })
114 })
115 })
116 .map(|(_, v)| v)
117}
118
119#[derive(Debug, Clone)]
124pub struct PickingDescription {
125 pub meshes: Vec<Vec<Option<String>>>,
127 pub material: Option<String>,
129 pub flag_38: bool,
131}
132
133pub fn picking_description(data: &[u8]) -> Result<PickingDescription> {
135 crs::parse(data, |r, _| {
136 r.block(|r, _| {
137 let mut meshes = Vec::new();
138 for _ in 0..r.count()? {
139 let mut stages = Vec::new();
140 for _ in 0..r.count()? {
141 stages.push(r.external_ref()?);
142 }
143 meshes.push(stages);
144 }
145 Ok(PickingDescription {
146 meshes,
147 material: r.external_ref()?,
148 flag_38: r.bool()?,
149 })
150 })
151 })
152 .map(|(_, v)| v)
153}
154
155#[derive(Debug, Clone)]
160pub struct HighlightDescription {
161 pub meshes: Vec<Vec<Option<String>>>,
163 pub mask_material: Option<String>,
165 pub edge_material: Option<String>,
167}
168
169pub fn highlight_description(data: &[u8]) -> Result<HighlightDescription> {
171 crs::parse(data, |r, _| {
172 r.block(|r, _| {
173 let mut meshes = Vec::new();
174 for _ in 0..r.count()? {
175 let mut stages = Vec::new();
176 for _ in 0..r.count()? {
177 stages.push(r.external_ref()?);
178 }
179 meshes.push(stages);
180 }
181 Ok(HighlightDescription {
182 meshes,
183 mask_material: r.external_ref()?,
184 edge_material: r.external_ref()?,
185 })
186 })
187 })
188 .map(|(_, v)| v)
189}
190
191#[derive(Debug, Clone)]
196pub struct EdgeMesh {
197 pub references: [Option<String>; 5],
199 pub ranges: Vec<[i32; 6]>,
201 pub edge_ranges: Vec<[i32; 6]>,
203 pub buffers: [Vec<u8>; 8],
205 pub bounds_min: [f32; 3],
207 pub bounds_max: [f32; 3],
209}
210
211pub fn edge_mesh(data: &[u8]) -> Result<EdgeMesh> {
213 crs::parse(data, |r, _| {
214 r.block(|r, _| {
215 let mut references = [const { None }; 5];
216 for v in &mut references {
217 *v = r.external_ref()?;
218 }
219 let mut ranges = Vec::new();
220 for _ in 0..r.count()? {
221 ranges.push([r.i32()?, r.i32()?, r.i32()?, r.i32()?, r.i32()?, r.i32()?]);
222 }
223 let mut edge_ranges = Vec::new();
224 for _ in 0..r.count()? {
225 edge_ranges.push([r.i32()?, r.i32()?, r.i32()?, r.i32()?, r.i32()?, r.i32()?]);
226 }
227 let mut buffers = std::array::from_fn(|_| Vec::new());
228 for b in &mut buffers {
229 r.u8()?;
230 let count = r.count()?;
231 *b = r.bytes(count)?.to_vec();
232 }
233 Ok(EdgeMesh {
234 references,
235 ranges,
236 edge_ranges,
237 buffers,
238 bounds_min: r.f32s()?,
239 bounds_max: r.f32s()?,
240 })
241 })
242 })
243 .map(|(_, v)| v)
244}