Skip to content

Commit

Permalink
Merge pull request Ezcha#4 from kb173/master
Browse files Browse the repository at this point in the history
Improve ease of integration into other projects
  • Loading branch information
Ezcha authored Jul 17, 2021
2 parents 5419dde + 38e8b1c commit e5ca305
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ As of Godot 3.2, Godot is unable to import `.obj` files outside of the `res://`
gd-obj allows either or both of these features. gd-obj supports uvs, faces, normals, and non triangulated meshes.

## How to use?
Create a new instance of `ObjParse`.
Call `.parse_file(path_to_file)` or `.parse_str(str_content)` from it. It will return a `Mesh` instance.
Call `ObjParse.parse_file(path_to_obj, path_to_mtl)`. This will return a `Mesh` which can, for example, be placed into the `mesh` field of a `MeshInstance`.
29 changes: 15 additions & 14 deletions ObjParse.gd → addons/obj-parse/ObjParse.gd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extends Node
class_name ObjParse

# Obj parser made by Ezcha
# Created on 7/11/2018
Expand All @@ -8,15 +9,15 @@ extends Node
# https://github.com/Ezcha/gd-obj/blob/master/LICENSE

# Returns an array of materials from a MTL file
func _parse_mtl_file(path):
static func _parse_mtl_file(path):
print("Parsing mtl file " + path)
var file = File.new()
file.open(path, File.READ)
var obj = file.get_as_text()

var mats = {}
var currentMat = null

var lines = obj.split("\n", false)
for line in lines:
var parts = line.split(" ", false)
Expand Down Expand Up @@ -48,10 +49,10 @@ func _parse_mtl_file(path):
"map_Ka":
# Texture file
currentMat.albedo_texture = _get_texture(path, parts[1])

return mats

func _get_texture(mtl_filepath, tex_filename):
static func _get_texture(mtl_filepath, tex_filename):
print(" Debug: Mapping texture file " + tex_filename)
var texfilepath = mtl_filepath.get_base_dir() + "/" + tex_filename
var filetype = texfilepath.get_extension()
Expand All @@ -63,12 +64,12 @@ func _get_texture(mtl_filepath, tex_filename):
print(" Debug: texture is " + str(tex))
return tex

func parse_obj(obj_path, mtl_path):
static func parse_obj(obj_path, mtl_path):
var file = File.new()
file.open(obj_path, File.READ)
var obj = file.get_as_text()
var mats = _parse_mtl_file(mtl_path)

# Setup
var mesh = Mesh.new()
var vertices = PoolVector3Array()
Expand All @@ -79,7 +80,7 @@ func parse_obj(obj_path, mtl_path):

var firstSurface = true
var mat_name = null

# Parse
var lines = obj.split("\n", false)
for line in lines:
Expand Down Expand Up @@ -145,15 +146,15 @@ func parse_obj(obj_path, mtl_path):
face["vn"].append(point2[2])
face["vn"].append(point1[2])
faces[mat_name].append(face)

# Make tri
for matgroup in faces.keys():
print("Creating surface for matgroup " + matgroup + " with " + str(faces[matgroup].size()) + " faces")

# Mesh Assembler
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)

st.set_material(mats[matgroup])
for face in faces[matgroup]:
if (face["v"].size() == 3):
Expand All @@ -162,21 +163,21 @@ func parse_obj(obj_path, mtl_path):
fan_v.append(vertices[face["v"][0]])
fan_v.append(vertices[face["v"][2]])
fan_v.append(vertices[face["v"][1]])

# Normals
var fan_vn = PoolVector3Array()
fan_vn.append(normals[face["vn"][0]])
fan_vn.append(normals[face["vn"][2]])
fan_vn.append(normals[face["vn"][1]])

# Textures
var fan_vt = PoolVector2Array()
fan_vt.append(uvs[face["vt"][0]])
fan_vt.append(uvs[face["vt"][2]])
fan_vt.append(uvs[face["vt"][1]])

st.add_triangle_fan(fan_v, fan_vt, PoolColorArray(), PoolVector2Array(), fan_vn, [])
mesh = st.commit(mesh)

# Finish
return mesh

0 comments on commit e5ca305

Please sign in to comment.