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
use super::{LoadOptions, MeshDescriptor};
use crate::{
    load::Loader,
    mat::Flip,
    mat::{MaterialList, Texture, TextureDescriptor, TextureSource},
    LoadError, LoadResult,
};
use glam::*;
use std::{convert::TryFrom, fs::File, path::PathBuf};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone)]
pub struct ObjLoader {}

impl std::fmt::Display for ObjLoader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "obj-loader")
    }
}

impl Default for ObjLoader {
    fn default() -> Self {
        Self {}
    }
}

impl Loader for ObjLoader {
    fn name(&self) -> &'static str {
        "obj loader"
    }

    fn file_extensions(&self) -> Vec<String> {
        vec![String::from("obj")]
    }

    fn load(&self, options: LoadOptions) -> LoadResult {
        let obj_options = tobj::LoadOptions {
            single_index: true,
            triangulate: true,
            ignore_points: false,
            ignore_lines: false,
        };

        let parent = match &options.source {
            crate::load::LoadSource::Path(p) => {
                p.parent().map(|f| f.to_path_buf()).unwrap_or_default()
            }
            crate::load::LoadSource::String { basedir, .. } => {
                PathBuf::try_from(basedir).unwrap_or_default()
            }
        };

        let object = match &options.source {
            crate::load::LoadSource::Path(p) => tobj::load_obj(&p, &obj_options),
            crate::load::LoadSource::String { source, .. } => {
                use std::io::BufReader;
                tobj::load_obj_buf(&mut BufReader::new(*source), &obj_options, |p| {
                    if let Some(f) = p.file_name().and_then(|f| f.to_str()) {
                        let f = if let Ok(f) = File::open(parent.join(PathBuf::from(f))) {
                            f
                        } else {
                            return tobj::MTLLoadResult::Err(tobj::LoadError::ReadError);
                        };
                        tobj::load_mtl_buf(&mut BufReader::new(f))
                    } else {
                        tobj::MTLLoadResult::Err(tobj::LoadError::ReadError)
                    }
                })
            }
        };

        if object.is_err() {
            match &options.source {
                crate::load::LoadSource::Path(p) => {
                    return LoadResult::None(LoadError::FileDoesNotExist(p.clone()))
                }
                crate::load::LoadSource::String { .. } => {
                    return LoadResult::None(LoadError::CouldNotParseSource)
                }
            }
        }

        let (models, materials) = object.unwrap();
        let materials = materials.unwrap_or_default();
        let mut material_indices: Vec<i32> = vec![-1; materials.len()];
        let mut mat_manager = MaterialList::new();

        for (i, material) in materials.iter().enumerate() {
            let mut color = Vec3::from(material.diffuse);
            let specular = Vec3::from(material.specular);

            let roughness = (1.0_f32 - material.shininess.log10() / 1000.0_f32).clamp(0.0, 1.0);
            let opacity = 1.0 - material.dissolve;
            let eta = material.optical_density;

            let d_path = if material.diffuse_texture.is_empty() {
                None
            } else {
                Some(parent.join(material.diffuse_texture.as_str()).to_path_buf())
            };
            let mut n_path = if material.normal_texture.is_empty() {
                None
            } else {
                Some(parent.join(material.normal_texture.as_str()).to_path_buf())
            };

            let mut roughness_map: Option<PathBuf> = None;
            let mut metallic_map: Option<PathBuf> = None;
            let mut emissive_map: Option<PathBuf> = None;
            let mut sheen_map: Option<PathBuf> = None;

            // TODO: Alpha and specular maps
            material.unknown_param.iter().for_each(|(name, value)| {
                let key = name.to_lowercase();
                match key.as_str() {
                    "ke" => {
                        // Emissive
                        let values = value.split_ascii_whitespace();
                        let mut f_values = [0.0_f32; 3];

                        for (i, value) in values.take(3).enumerate() {
                            let value: f32 = value.parse().unwrap_or(0.0);
                            f_values[i] = value;
                        }

                        let mut value: Vec3A = Vec3A::from(f_values);
                        if !value.cmpeq(Vec3A::ZERO).all() && value.cmple(Vec3A::ONE).all() {
                            value *= Vec3A::splat(10.0);
                        }

                        color = value.max(color.into()).into();
                    }
                    "map_pr" => {
                        roughness_map = Some(parent.join(value.as_str()));
                    }
                    "map_ke" => {
                        emissive_map = Some(parent.join(value.as_str()));
                    }
                    "ps" | "map_ps" => {
                        sheen_map = Some(parent.join(value.as_str()));
                    }
                    "pm" | "map_pm" => {
                        metallic_map = Some(parent.join(value.as_str()));
                    }
                    "norm" | "map_ns" | "map_bump" => {
                        n_path = Some(parent.join(value.as_str()));
                    }
                    _ => {}
                }
            });

            let metallic_roughness = match (roughness_map, metallic_map) {
                (Some(r), Some(m)) => {
                    let r = match Texture::load(&r, Flip::FlipV) {
                        Ok(t) => t,
                        Err(_) => return LoadResult::None(LoadError::TextureDoesNotExist(r)),
                    };
                    let m = match Texture::load(&m, Flip::FlipV) {
                        Ok(t) => t,
                        Err(_) => return LoadResult::None(LoadError::TextureDoesNotExist(m)),
                    };

                    let (r, m) = if r.width != m.width || r.height != m.height {
                        let width = r.width.max(m.width);
                        let height = r.height.max(m.height);
                        (r.resized(width, height), m.resized(width, height))
                    } else {
                        (r, m)
                    };

                    let combined = Texture::merge(Some(&r), Some(&m), None, None);
                    Some(TextureSource::Loaded(combined))
                }
                (Some(r), None) => {
                    let r = match Texture::load(&r, Flip::FlipV) {
                        Ok(t) => t,
                        Err(_) => return LoadResult::None(LoadError::TextureDoesNotExist(r)),
                    };

                    let combined = Texture::merge(Some(&r), None, None, None);
                    Some(TextureSource::Loaded(combined))
                }
                (None, Some(m)) => {
                    let m = match Texture::load(&m, Flip::FlipV) {
                        Ok(t) => t,
                        Err(_) => return LoadResult::None(LoadError::TextureDoesNotExist(m)),
                    };
                    let combined = Texture::merge(None, Some(&m), None, None);
                    Some(TextureSource::Loaded(combined))
                }
                _ => None,
            };

            let mat_index = mat_manager.add_with_maps(
                color,
                roughness,
                specular,
                opacity,
                TextureDescriptor {
                    albedo: d_path.map(|path| TextureSource::Filesystem(path, Flip::FlipV)),
                    normal: n_path.map(|path| TextureSource::Filesystem(path, Flip::FlipV)),
                    metallic_roughness_map: metallic_roughness,
                    emissive_map: emissive_map
                        .map(|path| TextureSource::Filesystem(path, Flip::FlipV)),
                    sheen_map: sheen_map.map(|path| TextureSource::Filesystem(path, Flip::FlipV)),
                },
            );

            mat_manager[mat_index].eta = eta;
            material_indices[i] = mat_index as i32;
        }

        if material_indices.is_empty() {
            material_indices.push(-1);
        }

        let num_vertices: usize = models.iter().map(|m| m.mesh.indices.len()).sum();

        let mut vertices: Vec<[f32; 4]> = Vec::with_capacity(num_vertices);
        let mut normals: Vec<[f32; 3]> = Vec::with_capacity(num_vertices);
        let mut uvs: Vec<[f32; 2]> = Vec::with_capacity(num_vertices);
        let mut material_ids: Vec<i32> = Vec::with_capacity(num_vertices);

        for m in models.iter() {
            let mesh = &m.mesh;

            for idx in &mesh.indices {
                let idx = *idx as usize;
                let i0 = 3 * idx;
                let i1 = i0 + 1;
                let i2 = i0 + 2;

                let pos = [
                    mesh.positions[i0],
                    mesh.positions[i1],
                    mesh.positions[i2],
                    1.0,
                ];

                let normal = if !mesh.normals.is_empty() {
                    [mesh.normals[i0], mesh.normals[i1], mesh.normals[i2]]
                } else {
                    [0.0; 3]
                };

                let uv = if !mesh.texcoords.is_empty() {
                    [mesh.texcoords[idx * 2], mesh.texcoords[idx * 2 + 1]]
                } else {
                    [0.0; 2]
                };

                vertices.push(pos);
                normals.push(normal);
                uvs.push(uv);

                let material_id = if mesh.material_id.is_some() {
                    (*material_indices
                        .get(mesh.material_id.unwrap())
                        .unwrap_or(&0)) as i32
                } else {
                    material_indices[0] as i32
                };

                material_ids.push(material_id);
            }
        }

        let descriptor = MeshDescriptor::new(
            vertices,
            normals,
            uvs,
            None,
            material_ids,
            Some(mat_manager),
            None,
        );

        LoadResult::Mesh(descriptor)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::load::*;
    use core::panic;
    use rtbvh::Aabb;
    use std::path::PathBuf;

    #[test]
    fn load_obj_works() {
        let loader = ObjLoader::default();
        let sphere = loader.load(LoadOptions {
            source: LoadSource::Path(PathBuf::from("assets/sphere.obj")),
            ..Default::default()
        });

        let m = match sphere {
            crate::LoadResult::Mesh(m) => m,
            crate::LoadResult::Scene(_) => panic!("Obj loader should only return meshes"),
            crate::LoadResult::None(_) => panic!("Obj loader should successfully load meshes"),
        };

        // Bounds should be correct
        let mut aabb: Aabb<()> = Aabb::default();
        for v in m.vertices.iter() {
            aabb.grow(vec3(v[0], v[1], v[2]));
        }
        for i in 0..3 {
            assert!((aabb.min[i] - m.bounds.min[i]).abs() < f32::EPSILON);
            assert!((aabb.max[i] - m.bounds.max[i]).abs() < f32::EPSILON);
        }

        assert_eq!(960 * 3, m.vertices.len(), "The sphere object has 960 faces");
        assert_eq!(
            m.vertices.len(),
            m.normals.len(),
            "Number of vertices and normals should be equal"
        );
        assert_eq!(
            m.vertices.len(),
            m.uvs.len(),
            "Number of vertices and uvs should be equal"
        );
        assert_eq!(
            m.vertices.len(),
            m.tangents.len(),
            "Number of vertices and tangents should be equal"
        );
        assert_eq!(
            m.vertices.len(),
            m.material_ids.len(),
            "Number of vertices and material ids should be equal"
        );
    }

    #[test]
    fn load_obj_source_works() {
        let loader = ObjLoader::default();
        let sphere = loader.load(LoadOptions {
            source: LoadSource::String {
                source: include_bytes!("../../assets/sphere.obj"),
                extension: "obj",
                basedir: "assets",
            },
            ..Default::default()
        });

        let m = match sphere {
            crate::LoadResult::Mesh(m) => m,
            crate::LoadResult::Scene(_) => panic!("Obj loader should only return meshes"),
            crate::LoadResult::None(_) => panic!("Obj loader should successfully load meshes"),
        };

        // Bounds should be correct
        let mut aabb: Aabb<()> = Aabb::default();
        for v in m.vertices.iter() {
            aabb.grow(vec3(v[0], v[1], v[2]));
        }
        for i in 0..3 {
            assert!((aabb.min[i] - m.bounds.min[i]).abs() < f32::EPSILON);
            assert!((aabb.max[i] - m.bounds.max[i]).abs() < f32::EPSILON);
        }

        assert_eq!(960 * 3, m.vertices.len(), "The sphere object has 960 faces");
        assert_eq!(
            m.vertices.len(),
            m.normals.len(),
            "Number of vertices and normals should be equal"
        );
        assert_eq!(
            m.vertices.len(),
            m.uvs.len(),
            "Number of vertices and uvs should be equal"
        );
        assert_eq!(
            m.vertices.len(),
            m.tangents.len(),
            "Number of vertices and tangents should be equal"
        );
        assert_eq!(
            m.vertices.len(),
            m.material_ids.len(),
            "Number of vertices and material ids should be equal"
        );
    }

    #[test]
    fn load_obj_with_options_works() {
        // TODO: This does not work yet
        let loader = ObjLoader::default();
        let sphere = loader.load(LoadOptions {
            source: LoadSource::Path(PathBuf::from("assets/sphere.obj")),
            with_normals: false,
            with_tangents: false,
            with_materials: false,
        });

        let m = match sphere {
            crate::LoadResult::Mesh(m) => m,
            crate::LoadResult::Scene(_) => panic!("Obj loader should only return meshes"),
            crate::LoadResult::None(_) => panic!("Obj loader should succesfully load meshes"),
        };

        // Bounds should be correct
        let mut aabb: Aabb<()> = Aabb::new();
        for v in m.vertices.iter() {
            aabb.grow(vec3(v[0], v[1], v[2]));
        }
        for i in 0..3 {
            assert!((aabb.min[i] - m.bounds.min[i]).abs() < f32::EPSILON);
            assert!((aabb.max[i] - m.bounds.max[i]).abs() < f32::EPSILON);
        }

        assert_eq!(960 * 3, m.vertices.len(), "The sphere object has 960 faces");
        assert_eq!(
            m.vertices.len(),
            m.normals.len(),
            "Number of vertices and normals should be equal"
        );
        assert_eq!(
            m.vertices.len(),
            m.uvs.len(),
            "Number of vertices and uvs should be equal"
        );
        assert_eq!(
            m.vertices.len(),
            m.tangents.len(),
            "Number of vertices and tangents should be equal"
        );
        assert_eq!(
            m.vertices.len(),
            m.material_ids.len(),
            "Number of vertices and material ids should be equal"
        );
    }
}