mesh_loader/collada/
instance.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
use std::path::PathBuf;

use super::*;
use crate::ShadingModel;

pub(super) fn build(doc: &mut Document<'_>, dir: Option<&Path>) -> common::Scene {
    let mut meshes = Vec::with_capacity(doc.library_geometries.geometries.len());
    let mut materials = Vec::with_capacity(doc.library_geometries.geometries.len());
    let mut instance_geometry_map = HashMap::new();
    let mut instance_material_map = HashMap::new();

    if let Some(root) = &doc.scene.instance_visual_scene {
        if let Some(root) = doc
            .library_visual_scenes
            .nodes
            .iter_mut()
            .find(|n| n.id == Some(root.url.as_str()))
        {
            root.transform *= Matrix4x4::new(
                doc.asset.unit,
                0.,
                0.,
                0.,
                0.,
                doc.asset.unit,
                0.,
                0.,
                0.,
                0.,
                doc.asset.unit,
                0.,
                0.,
                0.,
                0.,
                1.,
            );
        }
    }

    for node in &doc.library_visual_scenes.nodes {
        for instance_geometry in &node.instance_geometry {
            let mut transform = node.transform;
            let mut parent = node.parent;
            while let Some(p) = parent {
                let node = &doc.library_visual_scenes.nodes[p];
                transform *= node.transform;
                parent = node.parent;
            }
            let transform = if transform.is_identity() {
                None
            } else {
                Some(transform)
            };
            instance_geometry_map.insert(instance_geometry.url.as_str(), transform);
            for (symbol, instance_material) in &instance_geometry.materials {
                match doc.get(&instance_material.target) {
                    Some(material) => {
                        match doc.get(&material.instance_effect.url) {
                            Some(effect) => {
                                instance_material_map.insert(*symbol, (material.name, effect));
                            }
                            None => {
                                // debug!(
                                //     "not found effect instance '{}'",
                                //     material.instance_effect.url.as_str()
                                // );
                            }
                        }
                    }
                    None => {
                        // debug!(
                        //     "not found material instance '{}'",
                        //     instance_material.target.as_str()
                        // );
                    }
                }
            }
        }
    }
    for geometry in doc.library_geometries.geometries.values() {
        if let Some(&transform) = instance_geometry_map.get(geometry.id) {
            meshes.push(build_mesh(doc, geometry, transform));
        } else {
            meshes.push(build_mesh(doc, geometry, None));
        }
        let mut material = None;
        for mat in geometry.mesh.primitives.iter().filter_map(|m| m.material) {
            if let Some(&(name, effect)) = instance_material_map.get(mat) {
                material = Some(build_material(doc, name, effect, dir));
                // TODO: multiple materials from geometry.mesh.primitives?
                break;
            }
        }
        match material {
            Some(material) => materials.push(material),
            None => materials.push(common::Material::default()),
        }
    }

    common::Scene { materials, meshes }
}

fn build_mesh(
    doc: &Document<'_>,
    geometry: &Geometry<'_>,
    transform: Option<Matrix4x4>,
) -> common::Mesh {
    let mut mesh = common::Mesh {
        name: geometry.id.to_owned(),
        ..Default::default()
    };

    for prim in (iter::Mesh { doc, xml: geometry }).primitives() {
        #[allow(clippy::cast_possible_truncation)]
        let prev_positions_len = mesh.vertices.len() as u32;
        let p: Vec<_> = prim.positions().collect();
        let n: Vec<_> = prim.normals().collect();
        let t: Vec<_> = prim.texcoords(0).collect();
        let c: Vec<_> = prim.colors().collect();
        let positions_indices = prim.vertex_indices();
        let mut normal_indices = prim.normal_indices();
        let mut texcoord_indices = prim.texcoord_indices(0);
        let mut color_indices = prim.color_indices();
        let mut idx = 0;

        for vertex_idx in positions_indices {
            for vertex_idx in vertex_idx {
                let mut v = [
                    p[vertex_idx as usize][0],
                    p[vertex_idx as usize][1],
                    p[vertex_idx as usize][2],
                ];
                if let Some(transform) = transform {
                    v *= transform;
                }
                mesh.vertices.push(v);
            }
            if !n.is_empty() {
                if let Some(normal_idx) = normal_indices.next() {
                    for normal_idx in normal_idx {
                        mesh.normals.push([
                            n[normal_idx as usize][0],
                            n[normal_idx as usize][1],
                            n[normal_idx as usize][2],
                        ]);
                    }
                } else {
                    panic!()
                }
            }
            if !t.is_empty() {
                if let Some(texcoord_idx) = texcoord_indices.next() {
                    for texcoord_idx in texcoord_idx {
                        mesh.texcoords[0]
                            .push([t[texcoord_idx as usize][0], t[texcoord_idx as usize][1]]);
                    }
                } else {
                    panic!()
                }
            }
            if !c.is_empty() {
                if let Some(rgb_idx) = color_indices.next() {
                    for rgb_idx in rgb_idx {
                        mesh.colors[0].push([
                            c[rgb_idx as usize][0],
                            c[rgb_idx as usize][1],
                            c[rgb_idx as usize][2],
                            1.,
                        ]);
                    }
                } else {
                    panic!()
                }
            }
            mesh.faces.push([
                prev_positions_len + idx,
                prev_positions_len + (idx + 1),
                prev_positions_len + (idx + 2),
            ]);
            idx += 3;
        }
    }

    mesh
}

fn build_material(
    doc: &Document<'_>,
    name: Option<&str>,
    effect: &Effect<'_>,
    dir: Option<&Path>,
) -> common::Material {
    fn texture(
        doc: &Document<'_>,
        effect: &Effect<'_>,
        texture: &Texture<'_>,
        dir: Option<&Path>,
    ) -> Option<PathBuf> {
        if texture.texture.is_empty() {
            return None;
        }
        let mut image = None;
        if let Some(sampler) = effect.profile.samplers.get(texture.texture) {
            if let Some(surface) = effect.profile.surfaces.get(sampler.source) {
                if let Some(i) = doc.get(&surface.init_from) {
                    image = Some(i);
                }
            }
        }
        if image.is_none() {
            if let Some(i) = doc.library_images.images.get(&texture.texture) {
                image = Some(i);
            }
        }
        if let Some(image) = image {
            match &image.source {
                ImageSource::Data(_data) => {} // TODO
                ImageSource::InitFrom(mut p) => {
                    // There is an exporter writes empty <init_from/> tag
                    if p.is_empty() {
                        return None;
                    }
                    match dir {
                        Some(dir) => {
                            // TODO
                            p = p.strip_prefix("file://").unwrap_or(p);
                            let tmp: String;
                            if p.contains('\\') {
                                tmp = p.replace('\\', "/");
                                p = &*tmp;
                            }
                            if p.starts_with("/..") {
                                p = p.strip_prefix('/').unwrap_or(p);
                            }
                            let p = dir.join(p);
                            if p.exists() {
                                return Some(p);
                            }
                        }
                        None => return Some(p.into()),
                    }
                }
                ImageSource::Skip => {}
            }
        }
        None
    }

    let mut mat = common::Material::default();

    if let Some(name) = name {
        mat.name = name.to_owned();
    }

    mat.shading_model = match effect.profile.technique.ty {
        _ if effect.profile.technique.faceted => Some(ShadingModel::Flat),
        ShadeType::Constant => Some(ShadingModel::NoShading),
        ShadeType::Lambert => Some(ShadingModel::Gouraud),
        ShadeType::Blinn => Some(ShadingModel::Blinn),
        ShadeType::Phong => Some(ShadingModel::Phong),
    };

    // mat.two_sided = Some(effect.profile.technique.double_sided);
    // mat.wireframe = Some(effect.profile.technique.wireframe);

    mat.color.ambient = Some(effect.profile.technique.ambient.color);
    mat.color.diffuse = Some(effect.profile.technique.diffuse.color);
    mat.color.specular = Some(effect.profile.technique.specular.color);
    mat.color.emissive = Some(effect.profile.technique.emission.color);
    mat.color.reflective = Some(effect.profile.technique.reflective.color);

    mat.shininess = Some(effect.profile.technique.shininess);
    mat.reflectivity = Some(effect.profile.technique.reflectivity);
    mat.index_of_refraction = Some(effect.profile.technique.index_of_refraction);

    // Refs: https://github.com/assimp/assimp/blob/v5.3.1/code/AssetLib/Collada/ColladaLoader.cpp#L1588
    let mut transparency = effect.profile.technique.transparency;
    let mut transparent = effect.profile.technique.transparent.color;
    if transparency >= 0. && transparency <= 1. {
        if effect.profile.technique.rgb_transparency {
            transparency *=
                0.212671 * transparent[0] + 0.715160 * transparent[1] + 0.072169 * transparent[2];
            transparent[3] = 1.;
            mat.color.transparent = Some(transparent);
        } else {
            transparency *= transparent[3];
        }
        if effect.profile.technique.invert_transparency {
            transparency = 1. - transparency;
        }
        if effect.profile.technique.has_transparency || transparency < 1. {
            mat.opacity = Some(transparency);
        }
    }

    if let Some(p) = texture(doc, effect, &effect.profile.technique.ambient.texture, dir) {
        // Refs: https://github.com/assimp/assimp/blob/v5.3.1/code/AssetLib/Collada/ColladaLoader.cpp#L1619
        mat.texture.lightmap = Some(p);
    }
    if let Some(p) = texture(doc, effect, &effect.profile.technique.emission.texture, dir) {
        mat.texture.emissive = Some(p);
    }
    if let Some(p) = texture(doc, effect, &effect.profile.technique.specular.texture, dir) {
        mat.texture.specular = Some(p);
    }
    if let Some(p) = texture(doc, effect, &effect.profile.technique.diffuse.texture, dir) {
        mat.texture.diffuse = Some(p);
    }
    if let Some(p) = texture(doc, effect, &effect.profile.technique.bump, dir) {
        mat.texture.normal = Some(p);
    }
    if let Some(p) = texture(
        doc,
        effect,
        &effect.profile.technique.transparent.texture,
        dir,
    ) {
        mat.texture.opacity = Some(p);
    }
    if let Some(p) = texture(
        doc,
        effect,
        &effect.profile.technique.reflective.texture,
        dir,
    ) {
        mat.texture.reflection = Some(p);
    }

    mat
}