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 d706c5b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 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
5 changes: 2 additions & 3 deletions test/loaders/test_obj_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -485,18 +485,16 @@ 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
o square2
v 0.0 2.0 0.0
v 0.0 0.0 0.0
v 2.0 0.0 0.0
Expand All @@ -518,6 +516,7 @@ def test_parse_with_objects
square_mesh = square.children.first
assert_kind_of Mittsu::Geometry, square_mesh.geometry, "#{square.name} geometry"
assert_equal square.name, square_mesh.name, "#{square.name} geometry name"
assert_equal 4, square_mesh.geometry.vertices.count

[
Mittsu::Vector3.new(0.0, 1.0, 0.0).multiply_scalar(i+1),
Expand Down

0 comments on commit d706c5b

Please sign in to comment.