mesh_loader/collada/
effect.rs

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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use super::*;

/// The `<library_effects>` element.
///
/// See the [specification][1.4] for details.
///
/// [1.4]: https://www.khronos.org/files/collada_spec_1_4.pdf#page=277
#[derive(Default)]
pub(super) struct LibraryEffects<'a> {
    // /// The unique identifier of this element.
    // pub(super) id: Option<&'a str>,
    // /// The name of this element.
    // pub(super) name: Option<&'a str>,
    pub(super) effects: HashMap<&'a str, Effect<'a>>,
}

/// The `<effect>` element.
///
/// See the [specification][1.4] for details.
///
/// [1.4]: https://www.khronos.org/files/collada_spec_1_4.pdf#page=265
pub(super) struct Effect<'a> {
    /// The unique identifier of this element.
    pub(super) id: &'a str,
    // /// The name of this element.
    // pub(super) name: Option<&'a str>,
    pub(super) profile: ProfileCommon<'a>,
}

/// The `<profile_COMMON>` element.
///
/// See the [specification][1.4] for details.
///
/// [1.4]: https://www.khronos.org/files/collada_spec_1_4.pdf#page=301
pub(super) struct ProfileCommon<'a> {
    // /// The unique identifier of this element.
    // pub(super) id: Option<&'a str>,
    pub(super) surfaces: HashMap<&'a str, Surface<'a>>,
    pub(super) samplers: HashMap<&'a str, Sampler<'a>>,

    pub(super) technique: Technique<'a>,
}

pub(super) struct Technique<'a> {
    #[allow(dead_code)] // TODO
    pub(super) ty: ShadeType,

    // Colors/Textures
    pub(super) emission: ColorAndTexture<'a>,
    pub(super) ambient: ColorAndTexture<'a>,
    pub(super) diffuse: ColorAndTexture<'a>,
    pub(super) specular: ColorAndTexture<'a>,
    pub(super) reflective: ColorAndTexture<'a>,
    pub(super) transparent: ColorAndTexture<'a>,
    pub(super) has_transparency: bool,
    pub(super) rgb_transparency: bool,
    pub(super) invert_transparency: bool,

    pub(super) shininess: f32,
    pub(super) reflectivity: f32,
    pub(super) transparency: f32,
    pub(super) index_of_refraction: f32,

    // GOOGLEEARTH/OKINO extensions
    pub(super) double_sided: bool,

    // FCOLLADA extensions
    pub(super) bump: Texture<'a>,

    // MAX3D extensions
    pub(super) wireframe: bool,
    pub(super) faceted: bool,
}

/// The `<surface>` element.
///
/// See the [specification][1.4] for details.
///
/// [1.4]: https://www.khronos.org/files/collada_spec_1_4.pdf#page=332
pub(super) struct Surface<'a> {
    pub(super) init_from: Uri<'a, Image<'a>>,
}

/// The `<sampler2D>` element.
///
/// See the [specification][1.4] for details.
///
/// [1.4] https://www.khronos.org/files/collada_spec_1_4.pdf#page=312
pub(super) struct Sampler<'a> {
    // An xs:NCName, which is the sid of a <surface>. A
    // <sampler*> is a definition of how a shader will resolve a
    // color out of a <surface>. <source> identifies the
    // <surface> to read.
    pub(super) source: &'a str,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(super) enum ShadeType {
    Constant,
    Lambert,
    Phong,
    Blinn,
}

pub(super) struct ColorAndTexture<'a> {
    pub(super) color: Color4,
    pub(super) texture: Texture<'a>,
}

impl ColorAndTexture<'_> {
    fn new(color: Color4) -> Self {
        Self {
            color,
            texture: Texture {
                texture: "",
                // texcoord: "",
            },
        }
    }
}

pub(super) struct Texture<'a> {
    pub(super) texture: &'a str,
    // pub(super) texcoord: &'a str,
}

// -----------------------------------------------------------------------------
// Parsing

pub(super) fn parse_library_effects<'a>(
    cx: &mut Context<'a>,
    node: xml::Node<'a, '_>,
) -> io::Result<()> {
    debug_assert_eq!(node.tag_name().name(), "library_effects");
    // cx.library_effects.id = node.attribute("id");
    // cx.library_effects.name = node.attribute("name");

    for child in node.element_children() {
        match child.tag_name().name() {
            "effect" => {
                let effect = parse_effect(cx, child)?;
                cx.library_effects.effects.insert(effect.id, effect);
            }
            "asset" | "extra" => { /* skip */ }
            _ => return Err(error::unexpected_child_elem(child)),
        }
    }

    // The specification says <library_effects> has 1 or more <effect> elements,
    // but some exporters write empty <library_effects/> tags.

    Ok(())
}

/*
The `<effect>` element

Attributes:
- `id` (xs:ID, Required)
- `name` (xs:token, Optional)

Child elements must appear in the following order if present:
- `<asset>` (0 or 1)
- `<annotate>` (0 or more)
- `<newparam>` (0 or more)
- profile (1 or more)
    At least one profile must appear, but any number of any of
    the following profiles can be included:
    - <profile_BRIDGE>
    - <profile_CG>
    - <profile_GLES>
    - <profile_GLES2>
    - <profile_GLSL>
    - <profile_COMMON>
- `<extra>` (0 or more)
*/
fn parse_effect<'a>(cx: &mut Context<'a>, node: xml::Node<'a, '_>) -> io::Result<Effect<'a>> {
    debug_assert_eq!(node.tag_name().name(), "effect");
    let id = node.required_attribute("id")?;
    let mut profile = None;

    for child in node.element_children() {
        if child.tag_name().name() == "profile_COMMON" {
            profile = Some(parse_profile_common(cx, child)?);
        }
    }

    let profile = match profile {
        Some(profile) => profile,
        None => return Err(error::exactly_one_elem(node, "profile_COMMON")),
    };

    Ok(Effect {
        id,
        // name: node.attribute("name"),
        profile,
    })
}

/*
The `<profile_COMMON>` element

Attributes:
- `id` (xs:ID, Optional)

Child elements must appear in the following order if present:
- `<asset>` (0 or 1)
- `<newparam>` (0 or more)
- `<technique>` (1)
- `<extra>` (0 or more)

Child Elements for `<profile_COMMON>` / `<technique>`
Child elements must appear in the following order if present:
- `<asset>` (0 or 1)
- shader_element (0 or more)
    One of `<constant>` (FX), `<lambert>`, `<phong>`, or `<blinn>`.
- `<extra>` (0 or more)
*/
fn parse_profile_common<'a>(
    cx: &mut Context<'a>,
    node: xml::Node<'a, '_>,
) -> io::Result<ProfileCommon<'a>> {
    debug_assert_eq!(node.tag_name().name(), "profile_COMMON");
    let mut surfaces = HashMap::default();
    let mut samplers = HashMap::default();
    let mut technique = None;

    for child in node.element_children() {
        match child.tag_name().name() {
            "newparam" => {
                parse_newparam(cx, child, &mut surfaces, &mut samplers)?;
            }
            "technique" => {
                for t in child.element_children() {
                    let name = t.tag_name().name();
                    match name {
                        "constant" | "lambert" | "phong" | "blinn" => {
                            technique = Some(parse_technique(t, name.parse().unwrap())?);
                        }
                        "asset" | "extra" => { /* skip */ }
                        _ => {}
                    }
                }
            }
            "asset" | "extra" => { /* skip */ }
            _ => return Err(error::unexpected_child_elem(child)),
        }
    }

    let technique = match technique {
        Some(technique) => technique,
        // TODO: technique maybe flatten?
        None => return Err(error::exactly_one_elem(node, "technique")),
    };

    Ok(ProfileCommon {
        // id: node.attribute("id"),
        surfaces,
        samplers,
        technique,
    })
}

fn parse_newparam<'a>(
    _cx: &mut Context<'a>,
    node: xml::Node<'a, '_>,
    surfaces: &mut HashMap<&'a str, Surface<'a>>,
    samplers: &mut HashMap<&'a str, Sampler<'a>>,
) -> io::Result<()> {
    debug_assert_eq!(node.tag_name().name(), "newparam");
    let sid = node.required_attribute("sid")?;

    for child in node.element_children() {
        match child.tag_name().name() {
            "surface" => {
                // image ID given inside <init_from> tags
                if let Some(init) = child.child("init_from") {
                    surfaces.insert(
                        sid,
                        Surface {
                            init_from: Uri::from_id(init.trimmed_text()),
                        },
                    );
                }
            }
            "sampler2D" => {
                // surface ID is given inside <source> tags
                if let Some(source) = child.child("source") {
                    samplers.insert(
                        sid,
                        Sampler {
                            source: source.trimmed_text(),
                        },
                    );
                }
            }
            _ => return Err(error::unexpected_child_elem(child)),
        }
    }

    Ok(())
}

impl FromStr for ShadeType {
    type Err = io::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "constant" => Self::Constant,
            "lambert" => Self::Lambert,
            "phong" => Self::Phong,
            "blinn" => Self::Blinn,
            _ => bail!("unknown shade type {:?}", s),
        })
    }
}

/*
Child elements must appear in the following order if present:
- <emission> (0 or 1, fx_common_color_or_texture_type)
- <ambient> (FX) (0 or 1, fx_common_color_or_texture_type)
- <diffuse> (0 or 1, fx_common_color_or_texture_type)
- <specular> (0 or 1, fx_common_color_or_texture_type)
- <shininess> (0 or 1, fx_common_float_or_param_type)
- <reflective> (0 or 1, fx_common_color_or_texture_type)
- <reflectivity> (0 or 1, fx_common_float_or_param_type 0.0 ..= 1.0)
- <transparent> (0 or 1, fx_common_color_or_texture_type)
- <transparency> (0 or 1, fx_common_float_or_param_type 0.0 ..= 1.0)
- <index_of_refraction> (0 or 1, fx_common_float_or_param_type)
*/
fn parse_technique<'a>(node: xml::Node<'a, '_>, ty: ShadeType) -> io::Result<Technique<'a>> {
    debug_assert_eq!(node.tag_name().name().parse::<ShadeType>().unwrap(), ty);
    let mut effect = Technique::new(ty);

    for child in node.element_children() {
        let name = child.tag_name().name();
        match name {
            // fx_common_color_or_texture_type
            "emission" => {
                parse_effect_color(
                    child,
                    &mut effect.emission.color,
                    &mut effect.emission.texture,
                )?;
            }
            "ambient" => {
                parse_effect_color(
                    child,
                    &mut effect.ambient.color,
                    &mut effect.ambient.texture,
                )?;
            }
            "diffuse" => {
                parse_effect_color(
                    child,
                    &mut effect.diffuse.color,
                    &mut effect.diffuse.texture,
                )?;
            }
            "specular" => {
                parse_effect_color(
                    child,
                    &mut effect.specular.color,
                    &mut effect.specular.texture,
                )?;
            }
            "reflective" => {
                parse_effect_color(
                    child,
                    &mut effect.reflective.color,
                    &mut effect.reflective.texture,
                )?;
            }
            "transparent" => {
                effect.has_transparency = true;
                if let Some(opaque) = child.parse_attribute::<Opaque>("opaque")? {
                    effect.rgb_transparency = opaque.rgb_transparency();
                    effect.invert_transparency = opaque.invert_transparency();
                }
                parse_effect_color(
                    child,
                    &mut effect.transparent.color,
                    &mut effect.transparent.texture,
                )?;
            }

            // fx_common_float_or_param_type
            "shininess" => {
                if let Some(n) = parse_effect_float(child)? {
                    effect.shininess = n;
                }
            }
            "reflectivity" => {
                if let Some(n) = parse_effect_float(child)? {
                    effect.reflectivity = n;
                }
            }
            "transparency" => {
                if let Some(n) = parse_effect_float(child)? {
                    effect.transparency = n;
                }
            }
            "index_of_refraction" => {
                if let Some(n) = parse_effect_float(child)? {
                    effect.index_of_refraction = n;
                }
            }

            // GOOGLEEARTH/OKINO extensions
            "double_sided" => {
                effect.double_sided = node.parse_required_attribute(name)?;
            }

            // FCOLLADA extensions
            "bump" => {
                let mut dummy = [0.; 4];
                parse_effect_color(child, &mut dummy, &mut effect.bump)?;
            }

            // MAX3D extensions
            "wireframe" => {
                effect.wireframe = node.parse_required_attribute(name)?;
            }
            "faceted" => {
                effect.faceted = node.parse_required_attribute(name)?;
            }

            _ => {}
        }
    }

    Ok(effect)
}

impl Technique<'_> {
    fn new(ty: ShadeType) -> Self {
        Self {
            ty,
            emission: ColorAndTexture::new([0.0, 0.0, 0.0, 1.0]),
            ambient: ColorAndTexture::new([0.1, 0.1, 0.1, 1.0]),
            diffuse: ColorAndTexture::new([0.6, 0.6, 0.6, 1.0]),
            specular: ColorAndTexture::new([0.4, 0.4, 0.4, 1.0]),
            // refs: https://www.khronos.org/files/collada_spec_1_5.pdf#page=250
            transparent: ColorAndTexture::new([1.0, 1.0, 1.0, 1.0]),
            reflective: ColorAndTexture::new([0.0, 0.0, 0.0, 1.0]),
            shininess: 10.0,
            index_of_refraction: 1.0,
            reflectivity: 0.0,
            // refs: https://www.khronos.org/files/collada_spec_1_5.pdf#page=250
            transparency: 1.0,
            has_transparency: false,
            rgb_transparency: false,
            invert_transparency: false,
            double_sided: false,
            bump: Texture {
                texture: "",
                // texcoord: "",
            },
            wireframe: false,
            faceted: false,
        }
    }
}

// Attributes:
// Only <transparent> has an attribute
// - `opaque` (Enumeration, Optional)
//
// Child Elements:
// Note: Exactly one of the child elements `<color>`, `<param>`, or
// `<texture>` must appear. They are mutually exclusive.
// - `<color>`
// - `<param>` (reference)
// - `<texture>`
//
// See also fx_common_color_or_texture_type in specification.
fn parse_effect_color<'a>(
    node: xml::Node<'a, '_>,
    color: &mut Color4,
    texture: &mut Texture<'a>,
) -> io::Result<()> {
    for child in node.element_children() {
        match child.tag_name().name() {
            "color" => {
                let content = xml::comma_to_period(child.trimmed_text());
                let mut iter = xml::parse_float_array_exact(&content, 4);
                // TODO: include in parse_float_array_exact?
                let map_err = |e| {
                    format_err!(
                        "{e} in <{}> element ({})",
                        child.tag_name().name(),
                        child.text_location(),
                    )
                };
                let r = iter.next().unwrap().map_err(map_err)?;
                let g = iter.next().unwrap().map_err(map_err)?;
                let b = iter.next().unwrap().map_err(map_err)?;
                let a = iter.next().unwrap().map_err(map_err)?;
                *color = [r, g, b, a];
            }
            "texture" => {
                let _texcoord = child.required_attribute("texcoord")?;
                *texture = Texture {
                    texture: child.required_attribute("texture")?,
                    // texcoord,
                };
            }
            "param" => {
                // warn!(
                //     "<{}> child element in <{}> element is unsupported ({})",
                //     child.tag_name().name(),
                //     child.parent_element().unwrap().tag_name().name(),
                //     child.node_location()
                // );
            }
            _ => {}
        }
    }
    Ok(())
}

fn parse_effect_float(node: xml::Node<'_, '_>) -> io::Result<Option<f32>> {
    let mut float = None;

    for child in node.element_children() {
        match child.tag_name().name() {
            "float" => {
                let content = xml::comma_to_period(child.trimmed_text());
                float = Some(
                    float::parse(content.as_bytes())
                        .ok_or_else(|| format_err!("error while parsing a float"))?,
                );
            }
            "param" => {
                // warn!(
                //     "<{}> child element in <{}> element is unsupported ({})",
                //     child.tag_name().name(),
                //     child.parent_element().unwrap().tag_name().name(),
                //     child.node_location()
                // );
            }
            _ => return Err(error::unexpected_child_elem(child)),
        }
    }

    Ok(float)
}

#[allow(non_camel_case_types)]
#[derive(Clone, Copy, PartialEq, Eq)]
enum Opaque {
    A_ZERO,
    A_ONE,
    RGB_ZERO,
    RGB_ONE,
}

impl Opaque {
    fn rgb_transparency(self) -> bool {
        matches!(self, Self::RGB_ZERO | Self::RGB_ONE)
    }
    fn invert_transparency(self) -> bool {
        matches!(self, Self::RGB_ZERO | Self::A_ZERO)
    }
}

impl FromStr for Opaque {
    type Err = io::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "A_ZERO" => Self::A_ZERO,
            "A_ONE" => Self::A_ONE,
            "RGB_ZERO" => Self::RGB_ZERO,
            "RGB_ONE" => Self::RGB_ONE,
            _ => bail!("unknown opaque type {:?}", s),
        })
    }
}