diff options
Diffstat (limited to 'engine/src/file_format/wavefront/obj.rs')
| -rw-r--r-- | engine/src/file_format/wavefront/obj.rs | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/engine/src/file_format/wavefront/obj.rs b/engine/src/file_format/wavefront/obj.rs index bebe291..dbcee33 100644 --- a/engine/src/file_format/wavefront/obj.rs +++ b/engine/src/file_format/wavefront/obj.rs @@ -92,7 +92,7 @@ impl Obj /// - A face's texture position cannot be found /// - A face's vertex normal cannot be found /// - A face index does not fit in a [`u32`] - pub fn to_mesh(&self) -> Result<Mesh, Error> + pub fn to_mesh(&self, options: ToMeshOptions) -> Result<Mesh, Error> { let mut vertex_buf = MeshVertexBuffer::with_capacity( &[ @@ -101,7 +101,7 @@ impl Obj ty: VertexAttrType::Float32Array { length: 3 }, }, VertexAttrInfo { - label: VertexLabel::Uv, + label: VertexLabel::UvFromTopLeft, ty: VertexAttrType::Float32Array { length: 2 }, }, VertexAttrInfo { @@ -154,6 +154,11 @@ impl Obj }, )?; + let texture_pos = options + .y_flip_uvs + .then(|| Vec2 { x: texture_pos.x, y: -texture_pos.y }) + .unwrap_or(texture_pos); + let normal = face_vertex.normal.map_or_else( || { if !self.vertex_normals.is_empty() { @@ -181,7 +186,7 @@ impl Obj value: pos.into_array(), }, NamedVertexAttr { - label: VertexLabel::Uv, + label: VertexLabel::UvFromTopLeft, value: texture_pos.into_array(), }, NamedVertexAttr { @@ -437,6 +442,38 @@ impl Obj } } +#[derive(Debug, Clone)] +#[non_exhaustive] +pub struct ToMeshOptions +{ + /// Whether all UVs should be flipped on the Y axis. This is necessary if the UVs + /// have their origin at the bottom left corner of the image + /// + /// The default is `true`. + pub y_flip_uvs: bool, +} + +impl ToMeshOptions +{ + /// Whether all UVs should be flipped on the Y axis. This is necessary if the UVs have + /// their origin at the bottom left corner of the image + /// + /// The default is `true`. + pub fn y_flip_uvs(mut self, y_flip_uvs: bool) -> Self + { + self.y_flip_uvs = y_flip_uvs; + self + } +} + +impl Default for ToMeshOptions +{ + fn default() -> Self + { + Self { y_flip_uvs: true } + } +} + #[derive(Debug)] #[non_exhaustive] pub struct Face |
