simple3d_mesh 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 895f68b55490157d1bf3e383b733265cb887aef3
4
+ data.tar.gz: f59cc2519a634fd8f5f71f21e54fa5fec8038d6e
5
+ SHA512:
6
+ metadata.gz: 2663ecdca5e7c7238b004a9f94e2b3c5c717ab1f00b87ee895db6a4d79cc734725787c7f73a084554a4eb0b5cc924d365909682be726548cab429ebcc982a1eb
7
+ data.tar.gz: 390caecb263ec2a2f36535be32dbedcae9dfcea513317c31ab0113fe4b35047d0f0d81df164bf649ba73ba4082b31b65fc683e01e273d91d5282dfc63b990d80
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple3d_mesh.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Misha Conway
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Simple3dMesh
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'simple3d_mesh'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install simple3d_mesh
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/simple3d_mesh/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/box.obj ADDED
@@ -0,0 +1,98 @@
1
+ # Wavefront OBJ file written by AC3D
2
+ # OBJ export version 1.2 Gary Sanders
3
+ mtllib box.mtl
4
+ v -0.490000 -0.500000 -0.500000
5
+ v 0.510000 -0.500000 -0.500000
6
+ v 0.510000 -0.500000 0.500000
7
+ v -0.490000 -0.500000 0.500000
8
+ v -0.490000 0.500000 0.500000
9
+ v 0.510000 0.500000 0.500000
10
+ v 0.510000 0.500000 -0.500000
11
+ v -0.490000 0.500000 -0.500000
12
+ vn 1.000000 0.000000 0.000000
13
+ vn 1.000000 0.000000 0.000000
14
+ vn 1.000000 0.000000 0.000000
15
+ vn 1.000000 0.000000 0.000000
16
+ vn 1.000000 0.000000 0.000000
17
+ vn 1.000000 0.000000 0.000000
18
+ vn -1.000000 0.000000 0.000000
19
+ vn -1.000000 0.000000 0.000000
20
+ vn -1.000000 0.000000 0.000000
21
+ vn -1.000000 0.000000 0.000000
22
+ vn -1.000000 0.000000 0.000000
23
+ vn -1.000000 0.000000 0.000000
24
+ vn 0.000000 0.000000 -1.000000
25
+ vn 0.000000 0.000000 -1.000000
26
+ vn 0.000000 0.000000 -1.000000
27
+ vn 0.000000 0.000000 -1.000000
28
+ vn 0.000000 0.000000 -1.000000
29
+ vn 0.000000 0.000000 -1.000000
30
+ vn 0.000000 0.000000 1.000000
31
+ vn 0.000000 0.000000 1.000000
32
+ vn 0.000000 0.000000 1.000000
33
+ vn 0.000000 0.000000 1.000000
34
+ vn 0.000000 0.000000 1.000000
35
+ vn 0.000000 0.000000 1.000000
36
+ vn 0.000000 1.000000 0.000000
37
+ vn 0.000000 1.000000 0.000000
38
+ vn 0.000000 1.000000 0.000000
39
+ vn 0.000000 1.000000 0.000000
40
+ vn 0.000000 1.000000 0.000000
41
+ vn 0.000000 1.000000 0.000000
42
+ vn 0.000000 -1.000000 0.000000
43
+ vn 0.000000 -1.000000 0.000000
44
+ vn 0.000000 -1.000000 0.000000
45
+ vn 0.000000 -1.000000 0.000000
46
+ vn 0.000000 -1.000000 0.000000
47
+ vn 0.000000 -1.000000 0.000000
48
+ vt 1.000000 0.000000
49
+ vt 0.000000 1.000000
50
+ vt 0.000000 0.000000
51
+ vt 0.000000 1.000000
52
+ vt 1.000000 0.000000
53
+ vt 1.000000 1.000000
54
+ vt 1.000000 0.000000
55
+ vt 0.000000 1.000000
56
+ vt 0.000000 0.000000
57
+ vt 0.000000 1.000000
58
+ vt 1.000000 0.000000
59
+ vt 1.000000 1.000000
60
+ vt 1.000000 1.000000
61
+ vt 0.000000 0.000000
62
+ vt 1.000000 0.000000
63
+ vt 0.000000 0.000000
64
+ vt 1.000000 1.000000
65
+ vt 0.000000 1.000000
66
+ vt 0.000000 0.000000
67
+ vt 1.000000 1.000000
68
+ vt 0.000000 1.000000
69
+ vt 1.000000 1.000000
70
+ vt 0.000000 0.000000
71
+ vt 1.000000 0.000000
72
+ vt 0.000000 1.000000
73
+ vt 1.000000 0.000000
74
+ vt 1.000000 1.000000
75
+ vt 1.000000 0.000000
76
+ vt 0.000000 1.000000
77
+ vt 0.000000 0.000000
78
+ vt 0.000000 0.000000
79
+ vt 1.000000 1.000000
80
+ vt 0.000000 1.000000
81
+ vt 1.000000 1.000000
82
+ vt 0.000000 0.000000
83
+ vt 1.000000 0.000000
84
+ usemtl newmtl_1
85
+ f 3/1/1 7/2/2 6/3/3
86
+ f 7/4/4 3/5/5 2/6/6
87
+ f 1/7/7 5/8/8 8/9/9
88
+ f 5/10/10 1/11/11 4/12/12
89
+ f 1/13/13 7/14/14 2/15/15
90
+ f 7/16/16 1/17/17 8/18/18
91
+ f 5/19/19 3/20/20 6/21/21
92
+ f 3/22/22 5/23/23 4/24/24
93
+ f 7/25/25 5/26/26 6/27/27
94
+ f 5/28/28 7/29/29 8/30/30
95
+ f 1/31/31 3/32/32 4/33/33
96
+ f 3/34/34 1/35/35 2/36/36
97
+ #number of verticies 8 number of Polygons 12
98
+ #end of Wavefront OBJ export by AC3D
@@ -0,0 +1,190 @@
1
+ module Simple3d
2
+ class Mesh
3
+ attr_accessor :indices
4
+ attr_accessor :vertices
5
+
6
+ def initialize options = {}
7
+ @indices = options[:indices]
8
+ if @indices.nil? && options[:vertices]
9
+ options[:vertices].each_slice(3) do |points|
10
+ add_face points, options[:reuse_existing_points]
11
+ end
12
+ else
13
+ @vertices = options[:vertices]
14
+ end
15
+
16
+ @indices ||= []
17
+ @vertices ||= []
18
+ end
19
+
20
+ def self.from_file filename
21
+ m = self.new
22
+
23
+ w = Wavefront::File.new filename
24
+ w.compute_vertex_buffer.each_slice(3) do |slice|
25
+ m.add_face slice.map{|wv| wv.position.to_a + [wv.tex.x, wv.tex.y] + wv.normal.to_a }
26
+ end
27
+
28
+ m
29
+ end
30
+
31
+ def add_face points, reuse_existing_points = true
32
+ if points.size > 4
33
+ raise "faces with more than four points currently not supported"
34
+ elsif points.size == 4
35
+ add_face [points[0], points[1], points[3]]
36
+ add_face [points[3], points[1], points[2]]
37
+ else
38
+ points.each do |pt|
39
+ pt = Vertex.new *pt if pt.kind_of? Array
40
+ similar_vertex_index = if reuse_existing_points
41
+ find_similar_vertex pt
42
+ else
43
+ nil
44
+ end
45
+ if similar_vertex_index
46
+ @indices << similar_vertex_index
47
+ else
48
+ @vertices << pt
49
+ @indices << @vertices.size - 1
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+
56
+ def find_similar_vertex v
57
+ vertices.each_with_index do |vt, i|
58
+ return i if vt.position == v.position && vt.texcoord == v.texcoord
59
+ end
60
+ nil
61
+ end
62
+
63
+ def collapse_similar_vertices!
64
+ faces = indices.each_slice(3)
65
+
66
+ old_vertices = @vertices
67
+ old_indices = @indices
68
+ @vertices = @indices = nil
69
+
70
+
71
+ end
72
+
73
+ def num_triangles
74
+ indices.size / 3
75
+ end
76
+
77
+ def renderable_vertices
78
+ indices.map do |i|
79
+ vertices[i]
80
+ end
81
+ end
82
+
83
+ def indices_for_triangle i
84
+ [indices[i*3], indices[i*3+1], indices[i*3+2]]
85
+ end
86
+
87
+ def vertices_for_triangle i
88
+ indices_for_triangle(i).map { |index| vertices[index] }
89
+ end
90
+
91
+ def triangle i
92
+ Geo3d::Triangle.new *(vertices_for_triangle(i).map(&:position))
93
+ end
94
+
95
+
96
+ def smooth_normals!
97
+ indices_to_face_normals = Array.new vertices.size
98
+ for i in 0..vertices.size-1
99
+ indices_to_face_normals[i] = []
100
+ end
101
+
102
+ for a in 0..num_triangles-1
103
+ tri = triangle a
104
+ indices_for_triangle(a).each do |vertex_index|
105
+ face_normal = tri.normal
106
+ indices_to_face_normals[vertex_index] << face_normal
107
+ end
108
+ end
109
+
110
+ for i in 0..vertices.size-1
111
+ face_normals = indices_to_face_normals[i]
112
+ vertices[i].normal = face_normals.inject(:+).normalize
113
+ end
114
+ end
115
+
116
+ def calculate_tangents!
117
+ tan1 = Array.new vertices.size
118
+ tan2 = Array.new vertices.size
119
+ for i in 0..vertices.size-1
120
+ tan1[i] = Geo3d::Vector.new
121
+ tan2[i] = Geo3d::Vector.new
122
+ end
123
+
124
+ for a in 0..num_triangles-1
125
+ i1, i2, i3 = indices_for_triangle a
126
+
127
+ v1 = vertices[i1].position
128
+ v2 = vertices[i2].position
129
+ v3 = vertices[i3].position
130
+
131
+ w1 = vertices[i1].texcoord
132
+ w2 = vertices[i2].texcoord
133
+ w3 = vertices[i3].texcoord
134
+
135
+ x1 = v2.x - v1.x
136
+ x2 = v3.x - v1.x
137
+ y1 = v2.y - v1.y
138
+ y2 = v3.y - v1.y
139
+ z1 = v2.z - v1.z
140
+ z2 = v3.z - v1.z
141
+
142
+ s1 = w2.x - w1.x
143
+ s2 = w3.x - w1.x
144
+ t1 = w2.y - w1.y
145
+ t2 = w3.y - w1.y
146
+
147
+ r = 1.0 / (s1 * t2 - s2 * t1)
148
+ sdir = Geo3d::Vector.new (t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r
149
+ tdir = Geo3d::Vector.new (s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r
150
+
151
+ tan1[i1] += sdir
152
+ tan1[i2] += sdir
153
+ tan1[i3] += sdir
154
+
155
+ tan2[i1] += tdir
156
+ tan2[i2] += tdir
157
+ tan2[i3] += tdir
158
+ end
159
+
160
+ for a in 0..vertices.size-1
161
+
162
+ n = vertices[a].normal
163
+ t = tan1[a]
164
+
165
+ puts "n is #{n.inspect}"
166
+ puts "t is #{t.inspect}"
167
+
168
+ # Gram-Schmidt orthogonalize
169
+ vertices[a].tangent = (t - n * n.dot(t)).normalize
170
+
171
+ # Calculate handedness
172
+ # tangent(a).w = (Dot(Cross(n, t), tan2[a]) < 0.0F) ? -1.0F : 1.0F;
173
+ vertices[a].tangent.w = n.cross(t).dot(tan2[a]) < 0.0 ? -1.0 : 1.0
174
+ end
175
+ end
176
+
177
+
178
+ def self.unit_cube
179
+ cube = self.new
180
+ cube.add_face [[1, -1, -1], [1, -1, 1], [-1, -1, 1], [-1, -1, -1]]
181
+ cube.add_face [[1, 1, -1], [-1, 1, -1], [-1, 1, 1], [1, 1, 1]]
182
+ cube.add_face [[1, -1, -1], [1, 1, -1], [1, 1, 1], [1, -1, 1]]
183
+ cube.add_face [[1, -1, 1], [1, 1, 1], [-1, 1, 1], [-1, -1, 1]]
184
+ cube.add_face [[-1, -1, 1], [-1, 1, 1], [-1, 1, -1], [-1, -1, -1]]
185
+ cube.add_face [[1, 1, -1], [1, -1, -1], [-1, -1, -1], [-1, 1, -1]]
186
+ cube
187
+ end
188
+
189
+ end
190
+ end
@@ -0,0 +1,3 @@
1
+ module Simple3d
2
+ MESH_VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ module Simple3d
2
+ class Vertex
3
+ attr_accessor :position
4
+ attr_accessor :texcoord
5
+ attr_accessor :normal
6
+ attr_accessor :tangent
7
+
8
+
9
+ def initialize *args
10
+ @position = ::Geo3d::Vector.point( args[0], args[1], args[2] )
11
+ @texcoord = ::Geo3d::Vector.point( args[3], args[4])
12
+ @normal = ::Geo3d::Vector.new( args[5], args[6], args[7] )
13
+ @tangent = ::Geo3d::Vector.new( args[8], args[9], args[10] )
14
+ end
15
+
16
+ def bitangent
17
+ normal.cross tangent
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ require 'wavefront'
2
+ require 'geo3d'
3
+ require 'simple3d/version'
4
+ require 'simple3d/mesh'
5
+ require 'simple3d/vertex'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple3d/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple3d_mesh"
8
+ spec.version = Simple3d::MESH_VERSION
9
+ spec.authors = ["Misha Conway"]
10
+ spec.email = ["MishaAConway@gmail.com"]
11
+ spec.summary = %q{A simple to use 3d mesh}
12
+ spec.description = %q{A simple to use 3d mesh}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'geo3d'
22
+ spec.add_runtime_dependency 'wavefront'
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple3d_mesh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Misha Conway
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: geo3d
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: wavefront
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A simple to use 3d mesh
70
+ email:
71
+ - MishaAConway@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - box.obj
82
+ - lib/simple3d/mesh.rb
83
+ - lib/simple3d/version.rb
84
+ - lib/simple3d/vertex.rb
85
+ - lib/simple3d_mesh.rb
86
+ - simple3d_mesh.gemspec
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: A simple to use 3d mesh
111
+ test_files: []