lib/mittsu/loaders/obj_loader.rb in mittsu-0.4.0 vs lib/mittsu/loaders/obj_loader.rb in mittsu-0.4.1

- old
+ new

@@ -51,11 +51,11 @@ when NORMAL_PATTERN then handle_normal($1.to_f, $2.to_f, $3.to_f) when UV_PATTERN then handle_uv($1.to_f, $2.to_f) 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 when USE_MTL_PATTERN then set_material($1) when LOAD_MTL_PATTERN # TODO @@ -103,13 +103,19 @@ 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 = '') + # Reset if we're already working on a named object + # otherwise, just name the one we have in progress + unless @object&.name.nil? + end_object + reset_vertices + @object = nil + end + @object ||= Object3D.new @object.name = object_name end def end_object return if @object.nil? @@ -118,10 +124,10 @@ @object = nil end 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 @mesh.name += " #{@material.name}" unless @material.nil? end