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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
use crate::*;
#[derive(Clone, Debug)]
pub struct Controller {
pub id: Option<String>,
pub name: Option<String>,
pub asset: Option<Box<Asset>>,
pub element: ControlElement,
pub extra: Vec<Extra>,
}
impl Controller {
pub fn new(element: ControlElement) -> Self {
Self {
id: None,
name: None,
asset: None,
element,
extra: vec![],
}
}
}
impl XNode for Controller {
const NAME: &'static str = "controller";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
Ok(Controller {
id: element.attr("id").map(Into::into),
name: element.attr("name").map(Into::into),
asset: Asset::parse_opt_box(&mut it)?,
element: parse_one_many(&mut it, ControlElement::parse)?,
extra: Extra::parse_many(it)?,
})
}
}
impl XNodeWrite for Controller {
fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
let mut e = Self::elem();
e.opt_attr("id", &self.id);
e.opt_attr("name", &self.name);
let e = e.start(w)?;
self.asset.write_to(w)?;
self.element.write_to(w)?;
self.extra.write_to(w)?;
e.end(w)
}
}
#[derive(Clone, Debug, Default)]
pub struct InstanceControllerData {
pub skeleton: Vec<Url>,
pub bind_material: Option<BindMaterial>,
}
impl XNodeWrite for InstanceControllerData {
fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
many(&self.skeleton, |e| ElemBuilder::print("skeleton", e, w))?;
self.bind_material.write_to(w)
}
}
impl Instantiate for Controller {
const INSTANCE: &'static str = "instance_controller";
type Data = InstanceControllerData;
fn parse_data(_: &Element, it: &mut ElementIter<'_>) -> Result<Self::Data> {
Ok(InstanceControllerData {
skeleton: parse_list("skeleton", it, parse_elem)?,
bind_material: BindMaterial::parse_opt(it)?,
})
}
fn is_empty(data: &Self::Data) -> bool {
data.skeleton.is_empty() && data.bind_material.is_none()
}
}
#[derive(Clone, Debug)]
pub enum ControlElement {
Skin(Skin),
Morph(Morph),
}
impl ControlElement {
pub fn parse(e: &Element) -> Result<Option<Self>> {
match e.name() {
Skin::NAME => Ok(Some(Self::Skin(Skin::parse(e)?))),
Morph::NAME => Ok(Some(Self::Morph(Morph::parse(e)?))),
_ => Ok(None),
}
}
pub fn source(&self) -> &Url {
match self {
ControlElement::Skin(skin) => &skin.source,
ControlElement::Morph(morph) => &morph.source,
}
}
pub fn sources(&self) -> &[Source] {
match self {
ControlElement::Skin(skin) => &skin.sources,
ControlElement::Morph(morph) => &morph.sources,
}
}
}
impl XNodeWrite for ControlElement {
fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
match self {
Self::Skin(e) => e.write_to(w),
Self::Morph(e) => e.write_to(w),
}
}
}
#[derive(Clone, Debug)]
pub struct Joints {
pub inputs: Vec<Input>,
pub joint: usize,
pub extra: Vec<Extra>,
}
impl Joints {
pub fn new(inputs: Vec<Input>) -> Self {
Self {
joint: inputs
.iter()
.position(|i| i.semantic == Semantic::Joint)
.expect("joints: missing JOINT input"),
inputs,
extra: vec![],
}
}
}
impl XNode for Joints {
const NAME: &'static str = "joints";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
let inputs = Input::parse_list_n::<2>(&mut it)?;
Ok(Joints {
joint: inputs
.iter()
.position(|i| i.semantic == Semantic::Joint)
.ok_or("joints: missing JOINT input")?,
inputs,
extra: Extra::parse_many(it)?,
})
}
}
impl XNodeWrite for Joints {
fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
let e = Self::elem().start(w)?;
self.inputs.write_to(w)?;
self.extra.write_to(w)?;
e.end(w)
}
}
impl Joints {
pub fn joint_input(&self) -> &Input {
&self.inputs[self.joint]
}
}
#[derive(Clone, Debug)]
pub struct Morph {
pub source: UrlRef<Geometry>,
pub method: MorphMethod,
pub sources: Vec<Source>,
pub targets: Targets,
pub extra: Vec<Extra>,
}
impl Morph {
pub fn new(source: Url, sources: Vec<Source>, targets: Vec<Input>) -> Self {
assert!(sources.len() >= 2);
Self {
source: Ref::new(source),
method: Default::default(),
sources,
targets: Targets::new(targets),
extra: vec![],
}
}
}
impl XNode for Morph {
const NAME: &'static str = "morph";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
Ok(Morph {
source: parse_attr(element.attr("source"))?.ok_or("missing source attr")?,
method: parse_attr(element.attr("method"))?.unwrap_or_default(),
sources: Source::parse_list_n::<2>(&mut it)?,
targets: Targets::parse_one(&mut it)?,
extra: Extra::parse_many(it)?,
})
}
}
impl XNodeWrite for Morph {
fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
let mut e = Self::elem();
e.print_attr("source", &self.source);
e.print_attr("method", &self.method);
let e = e.start(w)?;
self.sources.write_to(w)?;
self.targets.write_to(w)?;
self.extra.write_to(w)?;
e.end(w)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MorphMethod {
Normalized,
Relative,
}
impl Default for MorphMethod {
fn default() -> Self {
Self::Normalized
}
}
impl FromStr for MorphMethod {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"NORMALIZED" => Ok(Self::Normalized),
"RELATIVE" => Ok(Self::Relative),
_ => Err(()),
}
}
}
impl MorphMethod {
pub fn to_str(self) -> &'static str {
match self {
Self::Normalized => "NORMALIZED",
Self::Relative => "RELATIVE",
}
}
}
impl Display for MorphMethod {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(self.to_str(), f)
}
}
#[derive(Clone, Debug)]
pub struct Skin {
pub source: UrlRef<Mesh>,
pub bind_shape_matrix: Option<Box<[f32; 16]>>,
pub sources: Vec<Source>,
pub joints: Joints,
pub weights: VertexWeights,
pub extra: Vec<Extra>,
}
impl Skin {
pub fn new(
source: Url,
sources: Vec<Source>,
joints: Vec<Input>,
weights: VertexWeights,
) -> Self {
assert!(sources.len() >= 3);
Self {
source: Ref::new(source),
bind_shape_matrix: None,
sources,
joints: Joints::new(joints),
weights,
extra: vec![],
}
}
}
impl XNode for Skin {
const NAME: &'static str = "skin";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
Ok(Skin {
source: parse_attr(element.attr("source"))?.ok_or("missing source attr")?,
bind_shape_matrix: parse_opt("bind_shape_matrix", &mut it, parse_array_n)?,
sources: Source::parse_list_n::<3>(&mut it)?,
joints: Joints::parse_one(&mut it)?,
weights: VertexWeights::parse_one(&mut it)?,
extra: Extra::parse_many(it)?,
})
}
}
impl XNodeWrite for Skin {
fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
let mut e = Self::elem();
e.print_attr("source", &self.source);
let e = e.start(w)?;
opt(&self.bind_shape_matrix, |e| {
ElemBuilder::print_arr("bind_shape_matrix", &**e, w)
})?;
self.sources.write_to(w)?;
self.joints.write_to(w)?;
self.weights.write_to(w)?;
self.extra.write_to(w)?;
e.end(w)
}
}
#[derive(Clone, Debug)]
pub struct Targets {
pub inputs: Vec<Input>,
pub morph_target: usize,
pub morph_weight: usize,
pub extra: Vec<Extra>,
}
impl Targets {
pub fn new(inputs: Vec<Input>) -> Self {
Self {
morph_target: inputs
.iter()
.position(|i| i.semantic == Semantic::MorphTarget)
.expect("targets: missing MORPH_TARGET input"),
morph_weight: inputs
.iter()
.position(|i| i.semantic == Semantic::MorphWeight)
.expect("targets: missing MORPH_WEIGHT input"),
inputs,
extra: vec![],
}
}
}
impl XNode for Targets {
const NAME: &'static str = "targets";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
let inputs = Input::parse_list(&mut it)?;
Ok(Targets {
morph_target: inputs
.iter()
.position(|i| i.semantic == Semantic::MorphTarget)
.ok_or("targets: missing MORPH_TARGET input")?,
morph_weight: inputs
.iter()
.position(|i| i.semantic == Semantic::MorphWeight)
.ok_or("targets: missing MORPH_WEIGHT input")?,
inputs,
extra: Extra::parse_many(it)?,
})
}
}
impl XNodeWrite for Targets {
fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
let e = Self::elem().start(w)?;
self.inputs.write_to(w)?;
self.extra.write_to(w)?;
e.end(w)
}
}
impl Targets {
pub fn morph_target_input(&self) -> &Input {
&self.inputs[self.morph_target]
}
pub fn morph_weight_input(&self) -> &Input {
&self.inputs[self.morph_weight]
}
}
#[derive(Clone, Debug)]
pub struct VertexWeights {
pub count: usize,
pub inputs: InputList,
pub joint: usize,
pub vcount: Box<[u32]>,
pub prim: Box<[i32]>,
pub extra: Vec<Extra>,
}
impl XNode for VertexWeights {
const NAME: &'static str = "vertex_weights";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
let inputs = InputList::parse::<2>(&mut it)?;
let res = VertexWeights {
count: parse_attr(element.attr("count"))?.ok_or("expected 'count' attr")?,
joint: inputs
.iter()
.position(|i| i.semantic == Semantic::Joint)
.ok_or("vertex_weights: missing JOINT input")?,
inputs,
vcount: parse_opt("vcount", &mut it, parse_array)?.unwrap_or_default(),
prim: parse_opt("v", &mut it, parse_array)?.unwrap_or_default(),
extra: Extra::parse_many(it)?,
};
validate_vcount(res.count, res.inputs.stride, &res.vcount, &res.prim)?;
Ok(res)
}
}
impl XNodeWrite for VertexWeights {
fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
let mut e = Self::elem();
e.print_attr("count", &self.count);
let e = e.start(w)?;
self.inputs.write_to(w)?;
ElemBuilder::print_arr("vcount", &self.vcount, w)?;
ElemBuilder::print_arr("v", &self.prim, w)?;
self.extra.write_to(w)?;
e.end(w)
}
}
impl VertexWeights {
pub fn new(inputs: Vec<InputS>, vcount: Box<[u32]>, prim: Box<[i32]>) -> Self {
let inputs = InputList::new(inputs);
assert!(
inputs.stride * vcount.iter().sum::<u32>() as usize == prim.len(),
"vcount does not match prim"
);
Self {
count: vcount.len(),
joint: inputs
.iter()
.position(|i| i.semantic == Semantic::Joint)
.expect("vertex_weights: missing JOINT input"),
inputs,
vcount,
prim,
extra: vec![],
}
}
pub fn joint_input(&self) -> &Input {
&self.inputs[self.joint]
}
}