obj_parser 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/obj_parser/obj.rb +10 -10
- data/lib/obj_parser/version.rb +1 -1
- data/test/obj_spec.rb +1 -0
- metadata +1 -1
data/lib/obj_parser/obj.rb
CHANGED
@@ -41,35 +41,35 @@ module ObjParser
|
|
41
41
|
pindex = 0
|
42
42
|
self.faces.each do |face|
|
43
43
|
pindex += 1
|
44
|
-
tangent_for_face =
|
45
|
-
tangent_for_face =
|
44
|
+
tangent_for_face = MathUtils::tangent_for_vertices_and_texures(face.vertice.map(&:data), face.textures.map(&:data))
|
45
|
+
tangent_for_face = MathUtils::normalized_vector(tangent_for_face)
|
46
46
|
#set the same tangent for the 3 vertex of current face
|
47
47
|
#re-compute tangents for duplicates vertices to get tangent per face
|
48
48
|
face.vertice.each_with_index do |vertex, index|
|
49
|
-
vertex.tangent.data =
|
49
|
+
vertex.tangent.data = MathUtils::sum_vectors(vertex.tangent.data, tangent_for_face)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
#orthonormalize
|
54
54
|
self.faces.each_with_index do |face,pindex|
|
55
55
|
face.vertice.each_with_index do |vertex, index|
|
56
|
-
vertex.tangent.data =
|
57
|
-
vertex.tangent.data =
|
56
|
+
vertex.tangent.data = MathUtils::orthogonalized_vector_with_vector(vertex.tangent.data, self.normals[self.normals_indexes[pindex * 3 + index]].data)
|
57
|
+
vertex.tangent.data = MathUtils::normalized_vector(vertex.tangent.data)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
61
|
#binormal should be computed with per vertex tangent and summed for each vertex
|
62
62
|
self.faces.each_with_index do |face,pindex|
|
63
63
|
face.vertice.each_with_index do |vertex, index|
|
64
|
-
binormal =
|
65
|
-
vertex.binormal.data =
|
64
|
+
binormal = MathUtils::cross_product(self.normals[self.normals_indexes[pindex * 3 + index]].data, vertex.tangent.data)
|
65
|
+
vertex.binormal.data = MathUtils::sum_vectors(vertex.binormal.data, binormal)
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
69
|
self.faces.each_with_index do |face,pindex|
|
70
70
|
face.vertice.each_with_index do |vertex, index|
|
71
|
-
vertex.binormal.data =
|
72
|
-
if(
|
71
|
+
vertex.binormal.data = MathUtils::normalized_vector(vertex.binormal.data)
|
72
|
+
if(MathUtils::dot(MathUtils::cross_product(self.normals[self.normals_indexes[pindex * 3 + index]].data, vertex.tangent.data), vertex.binormal.data) < 0.0)
|
73
73
|
vertex.tangent.data[3] = -1.0
|
74
74
|
else
|
75
75
|
vertex.tangent.data[3] = 1.0
|
@@ -88,7 +88,7 @@ module ObjParser
|
|
88
88
|
self.resolve_faces
|
89
89
|
result = self.faces.each_with_index.map do |face, index|
|
90
90
|
face.vertice.map do |vertex|
|
91
|
-
("%.2f" %
|
91
|
+
("%.2f" % MathUtils::dot(vertex.tangent.data[0..2], vertex.normal.data)).to_f
|
92
92
|
end.reduce(&:+)
|
93
93
|
end.reduce(&:+)
|
94
94
|
puts "RESULT: tangents and normals are orthogonal -> [#{result == 0 ? "VALID" : "NOT VALID"}]"
|
data/lib/obj_parser/version.rb
CHANGED
data/test/obj_spec.rb
CHANGED
@@ -22,6 +22,7 @@ describe ObjParser::Obj do
|
|
22
22
|
|
23
23
|
it 'compute tangents' do
|
24
24
|
@obj.compute_tangents
|
25
|
+
puts @obj.tangents.map(&:data)
|
25
26
|
result = @obj.faces.each_with_index.map do |face, index|
|
26
27
|
face.vertice.map do |vertex|
|
27
28
|
("%.2f" % ObjParser::MathUtils::dot(vertex.tangent.data[0..2], vertex.normal.data)).to_f
|