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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
mod parser;
pub use parser::Error;
use parser::{MaterialFragment, MeshFragment, MeshFragmentPolygonEntry, TextureFragment, WldDoc};
pub struct Wld<'a>(WldDoc<'a>);
pub fn load(data: &[u8]) -> Result<Wld, Error> {
Wld::load(data)
}
impl<'a> Wld<'a> {
fn load(data: &[u8]) -> Result<Wld, Error> {
WldDoc::parse(&data[..]).map(|(_, wld_doc)| Ok(Wld(wld_doc)))?
}
pub fn meshes(&self) -> impl Iterator<Item = Mesh> + '_ {
self.0.meshes().map(move |(name, fragment)| Mesh {
doc: &self.0,
name,
fragment,
})
}
pub fn materials(&self) -> impl Iterator<Item = Material> + '_ {
self.0.materials().map(move |(name, fragment)| Material {
doc: &self.0,
name,
fragment,
})
}
}
#[derive(Debug)]
pub struct Mesh<'a> {
doc: &'a WldDoc<'a>,
fragment: MeshFragment,
name: Option<&'a str>,
}
impl<'a> Mesh<'a> {
pub fn name(&self) -> Option<&str> {
self.name
}
pub fn center(&self) -> (f32, f32, f32) {
let (x, z, y) = self.fragment.center;
(x, y, z)
}
pub fn positions(&self) -> Vec<[f32; 3]> {
let scale = 1.0 / (1 << self.fragment.scale) as f32;
self.fragment
.positions
.iter()
.map(|v| [v.0 as f32 * scale, v.2 as f32 * scale, v.1 as f32 * scale])
.collect()
}
pub fn normals(&self) -> Vec<[f32; 3]> {
self.fragment
.vertex_normals
.iter()
.map(|v| {
[
(v.0 as f32) / 127.0,
(v.2 as f32) / 127.0,
(v.1 as f32) / 127.0,
]
})
.collect()
}
pub fn texture_coordinates(&self) -> Vec<[f32; 2]> {
self.fragment
.texture_coordinates
.iter()
.map(|v| [(v.0 as f32) / 256.0, (v.1 as f32) / 256.0])
.collect()
}
pub fn indices(&self) -> Vec<u32> {
self.fragment
.polygons
.iter()
.flat_map(|v| {
vec![
v.vertex_indexes.0 as u32,
v.vertex_indexes.1 as u32,
v.vertex_indexes.2 as u32,
]
.into_iter()
})
.collect()
}
pub fn materials(&self) -> Vec<Material> {
let (_, material_list) = self
.doc
.get(&self.fragment.material_list_ref)
.expect("Invalid material list reference");
material_list
.fragments
.iter()
.map(|fragment_ref| {
self.doc
.get(&fragment_ref)
.expect("Invalid material reference")
})
.map(|(name, fragment)| Material {
doc: &self.doc,
name,
fragment,
})
.collect()
}
pub fn primitives(&self) -> Vec<Primitive> {
let mut pos = 0;
self.fragment
.polygon_materials
.iter()
.enumerate()
.map(|(index, (poly_count, ref material_idx))| {
let count = *poly_count as usize;
let next_pos = pos + count;
let batch = pos..next_pos;
pos = next_pos;
Primitive {
mesh: self,
index,
fragments: &self
.fragment
.polygons
.get(batch)
.expect("Primitive fragments out of range"),
material_idx: *material_idx as usize,
}
})
.collect()
}
}
#[derive(Debug)]
pub struct Primitive<'a> {
mesh: &'a Mesh<'a>,
index: usize,
fragments: &'a [MeshFragmentPolygonEntry],
material_idx: usize,
}
impl<'a> Primitive<'a> {
pub fn indices(&self) -> Vec<u32> {
self.fragments
.iter()
.flat_map(|v| {
vec![
v.vertex_indexes.0 as u32,
v.vertex_indexes.1 as u32,
v.vertex_indexes.2 as u32,
]
})
.collect()
}
pub fn positions(&self) -> Vec<[f32; 3]> {
self.mesh.positions()
}
pub fn normals(&self) -> Vec<[f32; 3]> {
self.mesh.normals()
}
pub fn texture_coordinates(&self) -> Vec<[f32; 2]> {
self.mesh.texture_coordinates()
}
pub fn material(&self) -> Material {
self.mesh.materials().remove(self.material_idx)
}
pub fn index(&self) -> usize {
self.index
}
}
#[derive(Debug)]
pub struct Material<'a> {
doc: &'a WldDoc<'a>,
fragment: MaterialFragment,
name: Option<&'a str>,
}
impl<'a> Material<'a> {
pub fn name(&self) -> Option<&str> {
self.name
}
pub fn base_color_texture(&self) -> Option<Texture> {
self.doc
.get(&self.fragment.reference)
.and_then(|(_, texture_ref)| self.doc.get(&texture_ref.reference))
.map(|(name, fragment)| Texture {
doc: self.doc,
fragment,
name,
})
}
}
#[derive(Debug)]
pub struct Texture<'a> {
doc: &'a WldDoc<'a>,
fragment: TextureFragment,
name: Option<&'a str>,
}
impl<'a> Texture<'a> {
pub fn name(&self) -> Option<&str> {
self.name
}
pub fn source(&self) -> Option<String> {
self.fragment
.references
.iter()
.map(|r| self.doc.get(&r))
.flat_map(|image| match image {
Some((_, i)) => i
.entries
.iter()
.map(|e| e.file_name.to_lowercase())
.collect::<Vec<_>>(),
None => vec![],
})
.nth(0)
}
}