Bevy Mesh Obj
This crate provides utilities to parse Wavefront .obj files, handle 3D object data, and generate bevy meshes from them.
Features
- Parsing
.objfiles: Parse.objfiles into an internal representation of vertices, normals, texture coordinates, faces, and smoothing information. - Handle 3D object data: The crate includes types for representing vertices (
Vertex), normals (Normal), texture coordinates (UVTexture), smoothing groups (Smoothing), and faces (Face). - Generate meshes: Create Bevy
Meshobjects from parsed.objfiles, which can be used in 3D rendering applications.
Installation
Add the bevy_mesh_obj crate:
Limitations
To ensure proper functionality, all .obj files must NOT contain any shared vertecies, and all objects must be triangulated (all faces must be triangles). See assets/wall_with_door_gap.blend for an example how to achieve this in Blender before exporting to .obj format.
Usage
Parsing .obj files
The crate provides several methods to parse .obj files either from a string or a file. Below are examples of how to use the provided functions.
Parse from a file
let obj: Obj3D = parse_single.unwrap;
Parse multiple objects from a file
let objs: = parse.unwrap;
Parse the nth object
let obj: Obj3D = parse_n.unwrap;
Parse the first object
let obj: Obj3D = parse_first.unwrap;
Writing to a file
You can write the Obj3D object back to an .obj file:
let obj: Obj3D = parse_single.unwrap;
obj.write_to_file.unwrap;
Generating Bevy Mesh
You can use the mesh_from_obj!() macro to generate a Bevy Mesh directly from an .obj file, that you can use in your game or application:
use Mesh;
use mesh_from_obj;
File Format
This crate handles parsing the following tokens from an .obj file:
o: Object namev: Vertex position (x, y, z)vn: Vertex normal (x, y, z)vt: Texture coordinates (u, v)s: Smoothing groupf: Face definitions, which are made up of indices to vertices, normals, and texture coordinates
Here is an example of a basic .obj file:
# Name
o Cube
# Vertices
v 1.0 1.0 1.0
v -1.0 1.0 1.0
v -1.0 -1.0 1.0
v 1.0 -1.0 1.0
v 1.0 1.0 -1.0
v -1.0 1.0 -1.0
v -1.0 -1.0 -1.0
v 1.0 -1.0 -1.0
# Normals
vn 0.0 0.0 1.0
vn 0.0 0.0 -1.0
vn 1.0 0.0 0.0
vn -1.0 0.0 0.0
vn 0.0 1.0 0.0
vn 0.0 -1.0 0.0
# Texture Coordinates
vt 0.0 0.0
vt 1.0 0.0
vt 1.0 1.0
vt 0.0 1.0
# Faces (using 1-based index notation)
f 1/1/1 2/2/1 3/3/1 4/4/1 # Front face
f 5/1/2 6/2/2 7/3/2 8/4/2 # Back face
f 1/1/3 2/2/3 6/3/5 5/4/5 # Top face
f 4/1/5 3/2/5 7/3/5 8/4/5 # Bottom face
f 1/1/4 4/2/4 8/3/4 5/4/4 # Right face
f 2/1/6 3/2/6 7/3/6 6/4/6 # Left face
oCube defines the object name.vlines define vertices.vnlines define vertex normals.vtlines define texture coordinates.flines define faces using vertex/texture/normal indices.
License
Contributing
I would be more than happy to accept your contribution if you have a valuable addition to this project. Please fork this repository, create a branch, and submit a pull request.