diff options
author | HampusM <hampus@hampusmat.com> | 2024-05-12 14:51:53 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-05-12 14:51:53 +0200 |
commit | 52171baf9101ff63be33f756c4cfe1046dd2bf88 (patch) | |
tree | 9d3b60bb8ed89e983e8bec7fa33541f83aa82eb4 /engine/src/file_format/wavefront | |
parent | f4431eee3680a5d4b8c5dc569d70c231b3a2fc1d (diff) |
feat(engine): add support for faces without texture/normal in obj
Diffstat (limited to 'engine/src/file_format/wavefront')
-rw-r--r-- | engine/src/file_format/wavefront/obj.rs | 59 |
1 files changed, 27 insertions, 32 deletions
diff --git a/engine/src/file_format/wavefront/obj.rs b/engine/src/file_format/wavefront/obj.rs index 7dab37c..4df5fe5 100644 --- a/engine/src/file_format/wavefront/obj.rs +++ b/engine/src/file_format/wavefront/obj.rs @@ -267,36 +267,37 @@ impl FaceVertex /// Tries to convert this face vertex into a [`Vertex`]. pub fn to_vertex(&self, obj: &Obj) -> Result<Vertex, Error> { + let mut vertex_builder = VertexBuilder::new(); + let vertex_pos = *obj.vertex_positions.get(self.position as usize - 1).ok_or( Error::FaceVertexPositionNotFound { vertex_pos_index: self.position }, )?; - let face_vertex_texture = - self.texture.ok_or(Error::NoFaceVertexTextureNotSupported)?; - - let texture_pos = *obj - .texture_positions - .get(face_vertex_texture as usize - 1) - .ok_or(Error::FaceTexturePositionNotFound { - texture_pos_index: face_vertex_texture, - })?; - - let face_vertex_normal = - self.normal.ok_or(Error::NoFaceVertexNormalNotSupported)?; - - let vertex_normal = *obj - .vertex_normals - .get(face_vertex_normal as usize - 1) - .ok_or(Error::FaceVertexNormalNotFound { - vertex_normal_index: face_vertex_normal, - })?; - - Ok(VertexBuilder::new() - .pos(vertex_pos) - .texture_coords(texture_pos) - .normal(vertex_normal) - .build() - .unwrap()) + vertex_builder = vertex_builder.pos(vertex_pos); + + if let Some(face_vertex_texture) = self.texture { + let texture_pos = obj + .texture_positions + .get(face_vertex_texture as usize - 1) + .ok_or(Error::FaceTexturePositionNotFound { + texture_pos_index: face_vertex_texture, + })?; + + vertex_builder = vertex_builder.texture_coords(texture_pos.clone()); + } + + if let Some(face_vertex_normal) = self.normal { + let vertex_normal = *obj + .vertex_normals + .get(face_vertex_normal as usize - 1) + .ok_or(Error::FaceVertexNormalNotFound { + vertex_normal_index: face_vertex_normal, + })?; + + vertex_builder = vertex_builder.normal(vertex_normal); + } + + Ok(vertex_builder.build().unwrap()) } } @@ -345,12 +346,6 @@ pub enum Error #[error("Face index {0} is too big to fit into a 32-bit integer")] FaceIndexTooBig(usize), - #[error("Face vertices without textures are not yet supported")] - NoFaceVertexTextureNotSupported, - - #[error("Face vertices without normals are not yet supported")] - NoFaceVertexNormalNotSupported, - #[error( "Unsupported number of arguments ({arg_count}) to {keyword} at line {line_no}" )] |