Skip to content

Commit

Permalink
JSON export bugfixes (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy authored Nov 17, 2024
1 parent 78efada commit dd0467c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 56 deletions.
4 changes: 2 additions & 2 deletions lib/mittsu/core/geometry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def to_json
end
vertices = []
@vertices.each do |vert|
vertices << vertex.x << vertex.y << vertex.z
vertices << vert.x << vert.y << vert.z
end
faces = []
normals = []
Expand Down Expand Up @@ -507,7 +507,7 @@ def to_json
faces << get_uv_index(face_vertex_uvs[2], uvs_hash, uvs)
end
if has_face_normal
faces << get_normal_index(face.normal)
faces << get_normal_index(face.normal, normals_hash, normals)
end
if has_face_vertex_normal
vertex_normals = face.vertex_normals
Expand Down
76 changes: 29 additions & 47 deletions lib/mittsu/core/object_3d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,13 @@ def update_matrix_world(force = false)
end

def to_json
@_output = {
{
metadata: {
version: 4.3,
type: 'Object',
generator: 'ObjectExporter'
}
}
@_geometries = {}
@_materials = {}
@_output[:object] = self.jsonify
@_output
},
}.merge(jsonify)
end

def clone(object = nil, recursive = true)
Expand Down Expand Up @@ -346,46 +342,32 @@ def clone(object = nil, recursive = true)
protected

def jsonify
data = {
uuid: @uuid,
type: @type,
matrix: @matrix.to_a
}
data[:name] = @name unless @name.nil? || @name.empty?
data[:user_data] = @user_data unless @user_data.nil? || @user_data.empty?
data[:visible] = @visible unless @visible

if !self.children.empty?
data[:children] = @children.map do |child|
child.jsonify
end
end

# TODO: implement jsonify for PointCloud

data
end

def jsonify_geometry(geometry)
@_output[:geometries] ||= []
if @_geometries[geometry.uuid].nil?
json = geometry.to_json
json.delete :metadata
@_geometries[geometry.uuid] = json
@_output[:geometries] << json
end
geometry.uuid
end

def jsonify_material(material)
@_output[:materials] ||= []
if @_materials[material.uuid].nil?
json = material.to_json
json.delete :metadata
@_materials[material.uuid] = json
@_output[:materials] << json
end
material.uuid
children = @children.map(&:to_json)
{
object: {
uuid: @uuid,
type: @type,
matrix: @matrix.to_a,
geometry: @geometry&.uuid,
material: @material&.uuid,
name: @name&.empty? ? nil : @name,
user_data: @user_data&.empty? ? nil : @user_data,
visible: @visible ? nil : @visible,
children: children.map { |x| x[:object] }.flatten
}.reject { |k,v| v.nil? || v == [] },
geometries: ([jsonify_geometry] + children.map { |x| x[:geometries] }).flatten.compact,
materials: ([jsonify_material] + children.map { |x| x[:materials] }).flatten.compact
}.reject { |k,v| v.nil? || v == [] }
end

def jsonify_geometry
return nil if @geometry.nil?
@geometry.to_json.delete_if {|k, v| k == :metadata }
end

def jsonify_material
return nil if @material.nil?
@material.to_json.delete_if {|k, v| k == :metadata }
end
end
end
4 changes: 1 addition & 3 deletions lib/mittsu/objects/line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ def clone(object = Line.new(@geometry, @material, @mode))

def jsonify
data = super
data[:geometry] = jsonify_geometry(self.geometry)
data[:material] = jsonify_material(self.material)
data[:mode] = self.mode
data[:object][:mode] = self.mode
data
end
end
Expand Down
5 changes: 1 addition & 4 deletions lib/mittsu/objects/mesh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,7 @@ def clone(object = Mesh.new(@geometry, @material), recursive = true)
protected

def jsonify
data = super
data[:geometry] = jsonify_geometry(@geometry)
data[:material] = jsonify_material(@material)
data
super
end
end
end
34 changes: 34 additions & 0 deletions test/core/test_object_3d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,40 @@ def test_to_json_with_children
}, object.to_json)
end

def test_to_json_with_mesh_child
object = Mittsu::Object3D.new
geometry = Mittsu::BoxGeometry.new(1.0, 1.0, 1.0)
material = Mittsu::MeshBasicMaterial.new(color: 0x00ff00)
object.add(Mittsu::Mesh.new(geometry, material))
# Check object basics
json = object.to_json
assert_equal('Mesh', json[:object][:children][0][:type])
# Check geometry node and uuid references
assert_equal('BoxGeometry', json[:geometries][0][:type])
assert_equal(json[:object][:children][0][:geometry],
json[:geometries][0][:uuid])
# Check material node and uuid references
assert_equal('MeshBasicMaterial', json[:materials][0][:type])
assert_equal(json[:object][:children][0][:material],
json[:materials][0][:uuid])
end

def test_to_json_with_line_child
object = Mittsu::Object3D.new
object.add(Mittsu::Line.new)
# Check object basics
json = object.to_json
assert_equal('Line', json[:object][:children][0][:type])
# Check geometry node and uuid references
assert_equal('Geometry', json[:geometries][0][:type])
assert_equal(json[:object][:children][0][:geometry],
json[:geometries][0][:uuid])
# Check material node and uuid references
assert_equal('LineBasicMaterial', json[:materials][0][:type])
assert_equal(json[:object][:children][0][:material],
json[:materials][0][:uuid])
end

def test_clone
object = Mittsu::Object3D.new
object.name = 'Foo'
Expand Down

0 comments on commit dd0467c

Please sign in to comment.