mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Collada parser mostly works for single object/material models
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
class IMICFPS
|
||||
class Model
|
||||
class ModelObject
|
||||
attr_reader :name, :vertices, :textures, :normals, :bounding_box, :debug_color
|
||||
attr_reader :id, :name, :vertices, :textures, :normals, :bounding_box, :debug_color
|
||||
attr_accessor :faces, :scale
|
||||
|
||||
def initialize(name)
|
||||
def initialize(id, name)
|
||||
@id = id
|
||||
@name = name
|
||||
@vertices = []
|
||||
@textures = []
|
||||
|
||||
@@ -30,8 +30,26 @@ class IMICFPS
|
||||
def parse
|
||||
end
|
||||
|
||||
def change_object(name)
|
||||
@model.objects << Model::ModelObject.new(name)
|
||||
def set_object(id: nil, name: nil)
|
||||
_model = nil
|
||||
|
||||
if id
|
||||
_model = @model.objects.find { |o| o.id == id }
|
||||
elsif name
|
||||
_model = @model.objects.find { |o| o.id == id }
|
||||
else
|
||||
raise "Must provide either an id: or name:"
|
||||
end
|
||||
|
||||
if _model
|
||||
@model.current_object = _model
|
||||
else
|
||||
raise "Couldn't find ModelObject!"
|
||||
end
|
||||
end
|
||||
|
||||
def change_object(id, name)
|
||||
@model.objects << Model::ModelObject.new(id, name)
|
||||
@model.current_object = @model.objects.last
|
||||
end
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ class IMICFPS
|
||||
geometry_id = geometry.attributes["id"].value
|
||||
geometry_name = geometry.attributes["name"].value
|
||||
|
||||
change_object(geometry_name)
|
||||
change_object(geometry_id, geometry_name)
|
||||
|
||||
mesh = geometry.at_css("mesh")
|
||||
|
||||
@@ -80,10 +80,20 @@ class IMICFPS
|
||||
|
||||
def build_faces(id, mesh)
|
||||
material_name = mesh.at_css("triangles").attributes["material"].value
|
||||
set_material(material_name)
|
||||
|
||||
positions_index = []
|
||||
normals_index = []
|
||||
uvs_index = []
|
||||
|
||||
mesh.at_css("triangles p").children.first.to_s.split(" ").map { |i| Integer(i) }.each_slice(3).each do |slice|
|
||||
set_material(material_name)
|
||||
positions_index << slice[0]
|
||||
normals_index << slice[1]
|
||||
uvs_index << slice[2]
|
||||
end
|
||||
|
||||
norm_index = 0
|
||||
positions_index.each_slice(3) do |slice|
|
||||
face = Face.new
|
||||
face.vertices = []
|
||||
face.uvs = []
|
||||
@@ -95,8 +105,10 @@ class IMICFPS
|
||||
slice.each do |index|
|
||||
face.vertices << @model.vertices[index]
|
||||
# face.uvs << @model.uvs[index]
|
||||
face.normals << @model.normals[index]
|
||||
face.normals << @model.normals[normals_index[norm_index]]
|
||||
face.colors << current_material.diffuse
|
||||
|
||||
norm_index += 1
|
||||
end
|
||||
|
||||
@model.current_object.faces << face
|
||||
|
||||
@@ -23,7 +23,7 @@ class IMICFPS
|
||||
when 'usemtl'
|
||||
set_material(array[1])
|
||||
when 'o'
|
||||
change_object(array[1])
|
||||
change_object(nil, array[1])
|
||||
when 's'
|
||||
set_smoothing(array[1])
|
||||
when 'v'
|
||||
|
||||
Reference in New Issue
Block a user