Skip to content

Commit

Permalink
handle late naming of first object in OBJ loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy committed Apr 16, 2024
1 parent c59c4f3 commit 799eac3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions lib/mittsu/loaders/obj_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def parse_line(line)

when FACE_PATTERN then parse_face(line)

when OBJECT_PATTERN then new_object($1) and reset_vertices
when OBJECT_PATTERN then handle_object($1)
when GROUP_PATTERN # ignore
when SMOOTH_GROUP_PATTERN # ignore

Expand Down Expand Up @@ -105,9 +105,13 @@ def relevant_lines(raw_lines)
raw_lines.split("\n").map(&:strip).reject(&:empty?).reject{|l| l.start_with? '#'}
end

def new_object(object_name = '')
end_object
@object = Object3D.new
def handle_object(object_name = '')
unless @object&.name.nil? # Name the first object if it exists and hasn't been named yet
end_object
reset_vertices
@object = nil
end
@object ||= Object3D.new
@object.name = object_name
end

Expand All @@ -120,7 +124,7 @@ def end_object

def new_mesh
end_mesh
new_object if @object.nil?
handle_object if @object.nil?
@geometry = Geometry.new
@mesh = Mesh.new(@geometry, @material || MeshLambertMaterial.new)
@mesh.name = @object.name
Expand Down
4 changes: 2 additions & 2 deletions test/loaders/test_obj_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,13 @@ def test_parse_with_objects
loader = Mittsu::OBJLoader.new

object = loader.parse """
o square1
v 0.0 1.0 0.0
v 0.0 0.0 0.0
v 1.0 0.0 0.0
v 1.0 1.0 0.0
o square1
f 1 2 4
f 2 3 4
Expand Down

0 comments on commit 799eac3

Please sign in to comment.