From 52171baf9101ff63be33f756c4cfe1046dd2bf88 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 12 May 2024 14:51:53 +0200 Subject: feat(engine): add support for faces without texture/normal in obj --- engine/src/file_format/wavefront/obj.rs | 59 +++++++++++++++------------------ 1 file changed, 27 insertions(+), 32 deletions(-) (limited to 'engine') 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 { + 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}" )] -- cgit v1.2.3-18-g5258