mittsu 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +3 -0
  4. data/CODE_OF_CONDUCT.md +13 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +39 -0
  8. data/Rakefile +7 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +7 -0
  11. data/examples/01_-_Default1noCulling.png +0 -0
  12. data/examples/01_scene_example.rb +14 -0
  13. data/examples/02_box_mesh_example.rb +30 -0
  14. data/examples/02_sphere_mesh_example.rb +30 -0
  15. data/examples/03_complex_object_example.rb +52 -0
  16. data/examples/04_ambient_light_example.rb +33 -0
  17. data/examples/04_dir_light_example.rb +36 -0
  18. data/examples/04_hemi_light_example.rb +30 -0
  19. data/examples/04_point_light_example.rb +50 -0
  20. data/examples/04_spot_light_example.rb +44 -0
  21. data/examples/05_earth_example.rb +42 -0
  22. data/examples/05_earth_moon_example.rb +46 -0
  23. data/examples/05_texture_example.rb +32 -0
  24. data/examples/06_cube_texture_example.rb +36 -0
  25. data/examples/06_skybox_example.rb +60 -0
  26. data/examples/07_earth_normal_example.rb +36 -0
  27. data/examples/08_shadow_example.rb +87 -0
  28. data/examples/09_line_example.rb +52 -0
  29. data/examples/10_obj_loader_example.rb +68 -0
  30. data/examples/11_character_input_example.rb +18 -0
  31. data/examples/11_continuous_keyboard_input_example.rb +35 -0
  32. data/examples/11_keyboard_input_example.rb +43 -0
  33. data/examples/12_mouse_click_example.rb +38 -0
  34. data/examples/12_mouse_motion_example.rb +35 -0
  35. data/examples/12_mouse_scroll_example.rb +36 -0
  36. data/examples/12_orbit_zoom_example.rb +68 -0
  37. data/examples/13_joystick_example.rb +80 -0
  38. data/examples/cubemap/tron_bk.png +0 -0
  39. data/examples/cubemap/tron_dn.png +0 -0
  40. data/examples/cubemap/tron_ft.png +0 -0
  41. data/examples/cubemap/tron_lf.png +0 -0
  42. data/examples/cubemap/tron_rt.png +0 -0
  43. data/examples/cubemap/tron_up.png +0 -0
  44. data/examples/earth.png +0 -0
  45. data/examples/earth_normal.png +0 -0
  46. data/examples/example_helper.rb +2 -0
  47. data/examples/male-02-1noCulling.png +0 -0
  48. data/examples/male02.mtl +54 -0
  49. data/examples/male02.obj +13888 -0
  50. data/examples/moon.png +0 -0
  51. data/examples/orig_02_-_Defaul1noCulling.png +0 -0
  52. data/examples/texture.png +0 -0
  53. data/lib/mittsu.rb +15 -0
  54. data/lib/mittsu/cameras.rb +4 -0
  55. data/lib/mittsu/cameras/camera.rb +34 -0
  56. data/lib/mittsu/cameras/cube_camera.rb +74 -0
  57. data/lib/mittsu/cameras/orthographic_camera.rb +53 -0
  58. data/lib/mittsu/cameras/perspective_camera.rb +115 -0
  59. data/lib/mittsu/constants.rb +160 -0
  60. data/lib/mittsu/core.rb +10 -0
  61. data/lib/mittsu/core/buffer_attribute.rb +87 -0
  62. data/lib/mittsu/core/buffer_geometry.rb +694 -0
  63. data/lib/mittsu/core/clock.rb +44 -0
  64. data/lib/mittsu/core/dynamic_buffer_attribute.rb +16 -0
  65. data/lib/mittsu/core/event_dispatcher.rb +39 -0
  66. data/lib/mittsu/core/face3.rb +30 -0
  67. data/lib/mittsu/core/geometry.rb +596 -0
  68. data/lib/mittsu/core/hash_array.rb +36 -0
  69. data/lib/mittsu/core/hash_object.rb +19 -0
  70. data/lib/mittsu/core/object_3d.rb +421 -0
  71. data/lib/mittsu/core/raycaster.rb +78 -0
  72. data/lib/mittsu/extras.rb +3 -0
  73. data/lib/mittsu/extras/geometries.rb +2 -0
  74. data/lib/mittsu/extras/geometries/box_geometry.rb +108 -0
  75. data/lib/mittsu/extras/geometries/sphere_geometry.rb +88 -0
  76. data/lib/mittsu/extras/helpers.rb +1 -0
  77. data/lib/mittsu/extras/helpers/camera_helper.rb +155 -0
  78. data/lib/mittsu/extras/image.rb +3 -0
  79. data/lib/mittsu/extras/image_utils.rb +80 -0
  80. data/lib/mittsu/lights.rb +7 -0
  81. data/lib/mittsu/lights/ambient_light.rb +16 -0
  82. data/lib/mittsu/lights/area_light.rb +24 -0
  83. data/lib/mittsu/lights/directional_light.rb +131 -0
  84. data/lib/mittsu/lights/hemisphere_light.rb +29 -0
  85. data/lib/mittsu/lights/light.rb +21 -0
  86. data/lib/mittsu/lights/point_light.rb +27 -0
  87. data/lib/mittsu/lights/spot_light.rb +104 -0
  88. data/lib/mittsu/loaders.rb +7 -0
  89. data/lib/mittsu/loaders/cache.rb +53 -0
  90. data/lib/mittsu/loaders/file_loader.rb +22 -0
  91. data/lib/mittsu/loaders/image_loader.rb +32 -0
  92. data/lib/mittsu/loaders/loader.rb +212 -0
  93. data/lib/mittsu/loaders/loading_manager.rb +17 -0
  94. data/lib/mittsu/loaders/mtl_loader.rb +242 -0
  95. data/lib/mittsu/loaders/obj_mtl_loader.rb +225 -0
  96. data/lib/mittsu/materials.rb +7 -0
  97. data/lib/mittsu/materials/line_basic_material.rb +39 -0
  98. data/lib/mittsu/materials/material.rb +156 -0
  99. data/lib/mittsu/materials/mesh_basic_material.rb +122 -0
  100. data/lib/mittsu/materials/mesh_face_material.rb +30 -0
  101. data/lib/mittsu/materials/mesh_lambert_material.rb +126 -0
  102. data/lib/mittsu/materials/mesh_phong_material.rb +152 -0
  103. data/lib/mittsu/materials/shader_material.rb +108 -0
  104. data/lib/mittsu/math.rb +105 -0
  105. data/lib/mittsu/math/box2.rb +135 -0
  106. data/lib/mittsu/math/box3.rb +194 -0
  107. data/lib/mittsu/math/color.rb +252 -0
  108. data/lib/mittsu/math/color_keywords.rb +151 -0
  109. data/lib/mittsu/math/euler.rb +182 -0
  110. data/lib/mittsu/math/frustum.rb +106 -0
  111. data/lib/mittsu/math/line3.rb +76 -0
  112. data/lib/mittsu/math/matrix3.rb +163 -0
  113. data/lib/mittsu/math/matrix4.rb +581 -0
  114. data/lib/mittsu/math/plane.rb +128 -0
  115. data/lib/mittsu/math/quaternion.rb +309 -0
  116. data/lib/mittsu/math/ray.rb +292 -0
  117. data/lib/mittsu/math/sphere.rb +91 -0
  118. data/lib/mittsu/math/spline.rb +128 -0
  119. data/lib/mittsu/math/triangle.rb +121 -0
  120. data/lib/mittsu/math/vector2.rb +238 -0
  121. data/lib/mittsu/math/vector3.rb +491 -0
  122. data/lib/mittsu/math/vector4.rb +414 -0
  123. data/lib/mittsu/objects.rb +3 -0
  124. data/lib/mittsu/objects/group.rb +8 -0
  125. data/lib/mittsu/objects/line.rb +143 -0
  126. data/lib/mittsu/objects/mesh.rb +243 -0
  127. data/lib/mittsu/renderers.rb +1 -0
  128. data/lib/mittsu/renderers/glfw_window.rb +216 -0
  129. data/lib/mittsu/renderers/opengl/opengl_debug.rb +38 -0
  130. data/lib/mittsu/renderers/opengl/opengl_program.rb +402 -0
  131. data/lib/mittsu/renderers/opengl/opengl_shader.rb +58 -0
  132. data/lib/mittsu/renderers/opengl/opengl_state.rb +207 -0
  133. data/lib/mittsu/renderers/opengl/plugins/shadow_map_plugin.rb +416 -0
  134. data/lib/mittsu/renderers/opengl_render_target.rb +87 -0
  135. data/lib/mittsu/renderers/opengl_renderer.rb +3376 -0
  136. data/lib/mittsu/renderers/shaders/shader_chunk.rb +12 -0
  137. data/lib/mittsu/renderers/shaders/shader_chunk/alphamap_fragment.glsl +5 -0
  138. data/lib/mittsu/renderers/shaders/shader_chunk/alphamap_pars_fragment.glsl +5 -0
  139. data/lib/mittsu/renderers/shaders/shader_chunk/alphatest_fragment.glsl +5 -0
  140. data/lib/mittsu/renderers/shaders/shader_chunk/bumpmap_pars_fragment.glsl +40 -0
  141. data/lib/mittsu/renderers/shaders/shader_chunk/color_fragment.glsl +5 -0
  142. data/lib/mittsu/renderers/shaders/shader_chunk/color_pars_fragment.glsl +5 -0
  143. data/lib/mittsu/renderers/shaders/shader_chunk/color_pars_vertex.glsl +5 -0
  144. data/lib/mittsu/renderers/shaders/shader_chunk/color_vertex.glsl +5 -0
  145. data/lib/mittsu/renderers/shaders/shader_chunk/common.glsl +60 -0
  146. data/lib/mittsu/renderers/shaders/shader_chunk/default_vertex.glsl +15 -0
  147. data/lib/mittsu/renderers/shaders/shader_chunk/defaultnormal_vertex.glsl +21 -0
  148. data/lib/mittsu/renderers/shaders/shader_chunk/envmap_fragment.glsl +62 -0
  149. data/lib/mittsu/renderers/shaders/shader_chunk/envmap_pars_fragment.glsl +21 -0
  150. data/lib/mittsu/renderers/shaders/shader_chunk/envmap_pars_vertex.glsl +7 -0
  151. data/lib/mittsu/renderers/shaders/shader_chunk/envmap_vertex.glsl +17 -0
  152. data/lib/mittsu/renderers/shaders/shader_chunk/fog_fragment.glsl +26 -0
  153. data/lib/mittsu/renderers/shaders/shader_chunk/fog_pars_fragment.glsl +15 -0
  154. data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_fragment.glsl +5 -0
  155. data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_pars_fragment.glsl +6 -0
  156. data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_pars_vertex.glsl +5 -0
  157. data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_vertex.glsl +5 -0
  158. data/lib/mittsu/renderers/shaders/shader_chunk/lights_lambert_pars_vertex.glsl +43 -0
  159. data/lib/mittsu/renderers/shaders/shader_chunk/lights_lambert_vertex.glsl +196 -0
  160. data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_fragment.glsl +243 -0
  161. data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_pars_fragment.glsl +58 -0
  162. data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_pars_vertex.glsl +5 -0
  163. data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_vertex.glsl +5 -0
  164. data/lib/mittsu/renderers/shaders/shader_chunk/linear_to_gamma_fragment.glsl +2 -0
  165. data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_fragment.glsl +5 -0
  166. data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_pars_fragment.glsl +12 -0
  167. data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_pars_vertex.glsl +11 -0
  168. data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_vertex.glsl +15 -0
  169. data/lib/mittsu/renderers/shaders/shader_chunk/map_fragment.glsl +9 -0
  170. data/lib/mittsu/renderers/shaders/shader_chunk/map_pars_fragment.glsl +11 -0
  171. data/lib/mittsu/renderers/shaders/shader_chunk/map_pars_vertex.glsl +6 -0
  172. data/lib/mittsu/renderers/shaders/shader_chunk/map_particle_fragment.glsl +5 -0
  173. data/lib/mittsu/renderers/shaders/shader_chunk/map_particle_pars_fragment.glsl +6 -0
  174. data/lib/mittsu/renderers/shaders/shader_chunk/map_vertex.glsl +5 -0
  175. data/lib/mittsu/renderers/shaders/shader_chunk/morphnormal_vertex.glsl +12 -0
  176. data/lib/mittsu/renderers/shaders/shader_chunk/morphtarget_pars_vertex.glsl +13 -0
  177. data/lib/mittsu/renderers/shaders/shader_chunk/morphtarget_vertex.glsl +20 -0
  178. data/lib/mittsu/renderers/shaders/shader_chunk/normalmap_pars_fragment.glsl +27 -0
  179. data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_fragment.glsl +217 -0
  180. data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_pars_fragment.glsl +19 -0
  181. data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_pars_vertex.glsl +6 -0
  182. data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_vertex.glsl +9 -0
  183. data/lib/mittsu/renderers/shaders/shader_chunk/skinbase_vertex.glsl +8 -0
  184. data/lib/mittsu/renderers/shaders/shader_chunk/skinning_pars_vertex.glsl +47 -0
  185. data/lib/mittsu/renderers/shaders/shader_chunk/skinning_vertex.glsl +20 -0
  186. data/lib/mittsu/renderers/shaders/shader_chunk/skinnormal_vertex.glsl +20 -0
  187. data/lib/mittsu/renderers/shaders/shader_chunk/specularmap_fragment.glsl +12 -0
  188. data/lib/mittsu/renderers/shaders/shader_chunk/specularmap_pars_fragment.glsl +5 -0
  189. data/lib/mittsu/renderers/shaders/shader_chunk/worldpos_vertex.glsl +17 -0
  190. data/lib/mittsu/renderers/shaders/shader_lib.rb +420 -0
  191. data/lib/mittsu/renderers/shaders/uniforms_lib.rb +107 -0
  192. data/lib/mittsu/renderers/shaders/uniforms_utils.rb +31 -0
  193. data/lib/mittsu/scenes.rb +1 -0
  194. data/lib/mittsu/scenes/scene.rb +27 -0
  195. data/lib/mittsu/textures.rb +5 -0
  196. data/lib/mittsu/textures/compressed_texture.rb +30 -0
  197. data/lib/mittsu/textures/cube_texture.rb +19 -0
  198. data/lib/mittsu/textures/data_texture.rb +17 -0
  199. data/lib/mittsu/textures/texture.rb +92 -0
  200. data/lib/mittsu/textures/video_texture.rb +17 -0
  201. data/lib/mittsu/version.rb +4 -0
  202. data/mittsu.gemspec +31 -0
  203. metadata +357 -0
@@ -0,0 +1,225 @@
1
+ module Mittsu
2
+ class OBJMTLLoader
3
+ include EventDispatcher
4
+
5
+ def initialize(manager = DefaultLoadingManager)
6
+ @manager = manager
7
+ end
8
+
9
+ def load(url, mtlurl)
10
+ mtl_loader = MTLLoader.new(File.dirname(url))
11
+ # mtl_loader.cross_origin = @cross_origin # TODO: not needed?
12
+ materials_creator = mtl_loader.load(mtlurl)
13
+
14
+ materials_creator.preload
15
+
16
+ loader = FileLoader.new(@manager)
17
+ # loader.cross_origin = @cross_origin # TODO: not needed?
18
+
19
+ text = loader.load(url)
20
+ object = parse(text)
21
+
22
+ object.traverse do |child_object|
23
+ if child_object.is_a?(Mesh) && child_object.material.name && !child_object.material.name.empty?
24
+ material = materials_creator.create(child_object.material.name)
25
+ child_object.material = material if material
26
+ end
27
+ end
28
+
29
+ object
30
+ end
31
+
32
+ def parse(data)
33
+ @face_offset = 0
34
+ @group = Group.new
35
+ @object = @group
36
+
37
+ @geometry = Geometry.new
38
+ @material = MeshLambertMaterial.new
39
+ @mesh = Mesh.new(@geometry, @material)
40
+
41
+ @vertices = []
42
+ @normals = []
43
+ @uvs = []
44
+
45
+ # v float float float
46
+ vertex_pattern = /v( +[\d|.|+|\-|e]+)( +[\d|.|+|\-|e]+)( +[\d|.|+|\-|e]+)/
47
+
48
+ # vn float float float
49
+ normal_pattern = /vn( +[\d|.|+|\-|e]+)( +[\d|.|+|\-|e]+)( +[\d|.|+|\-|e]+)/
50
+
51
+ # vt float flot
52
+ uv_pattern = /vt( +[\d|.|+|\-|e]+)( +[\d|.|+|\-|e]+)/
53
+
54
+ # f vertex vertex vertex
55
+ face_pattern1 = /f( +\d+)( +\d+)( +\d+)?/
56
+
57
+ # f vertex/uv vertex/uv vertex/uv
58
+ face_pattern2 = /f( +(\d+)\/(\d+))( +(\d+)\/(\d+))( +(\d+)\/(\d+))( +(\d+)\/(\d+))?/
59
+
60
+ # f vertex/uv/normal vertex/uv/normal vertex/uv/normal ...
61
+ face_pattern3 = /f( +(\d+)\/(\d+)\/(\d+))( +(\d+)\/(\d+)\/(\d+))( +(\d+)\/(\d+)\/(\d+))( +(\d+)\/(\d+)\/(\d+))?/
62
+
63
+ # f vertex//normal vertex//normal vertex//normal ...
64
+ face_pattern4 = /f( +(\d+)\/\/(\d+))( +(\d+)\/\/(\d+))( +(\d+)\/\/(\d+))( +(\d+)\/\/(\d+))?/
65
+
66
+ lines = data.split("\n")
67
+
68
+ lines.each do |line|
69
+ line = line.strip
70
+
71
+ next if line.empty? || line.start_with?('#')
72
+
73
+ if vertex_pattern =~ line
74
+ # ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
75
+ @vertices << vector($1.to_f, $2.to_f, $3.to_f)
76
+ elsif normal_pattern =~ line
77
+ # ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
78
+ @normals << vector($1.to_f, $2.to_f, $3.to_f)
79
+ elsif uv_pattern =~ line
80
+ # ["vt 0.1 0.2", "0.1", "0.2"]
81
+ @uvs << uv($1.to_f, $2.to_f)
82
+ elsif face_pattern1 =~ line
83
+ # ["f 1 2 3", "1", "2", "3", undefined]
84
+ handle_face_line([ $1, $2, $3, $4 ])
85
+ elsif face_pattern2 =~ line
86
+ # ["f 1/1 2/2 3/3", " 1/1", "1", "1", " 2/2", "2", "2", " 3/3", "3", "3", undefined, undefined, undefined]
87
+ handle_face_line(
88
+ [ $2, $5, $8, $11 ], #faces
89
+ [ $3, $6, $9, $12 ] #uv
90
+ )
91
+ elsif face_pattern3 =~ line
92
+ # ["f 1/1/1 2/2/2 3/3/3", " 1/1/1", "1", "1", "1", " 2/2/2", "2", "2", "2", " 3/3/3", "3", "3", "3", undefined, undefined, undefined, undefined]
93
+ handle_face_line(
94
+ [ $2, $6, $10, $14 ], #faces
95
+ [ $3, $7, $11, $15 ], #uv
96
+ [ $4, $8, $12, $16 ] #normal
97
+ )
98
+ elsif face_pattern4 =~ line
99
+ # ["f 1//1 2//2 3//3", " 1//1", "1", "1", " 2//2", "2", "2", " 3//3", "3", "3", undefined, undefined, undefined]
100
+ handle_face_line(
101
+ [ $2, $5, $8, $11 ], #faces
102
+ [ ], #uv
103
+ [ $3, $6, $9, $12 ] #normal
104
+ )
105
+ elsif /^o / =~ line
106
+ # object
107
+ mesh_n
108
+ @face_offset = @face_offset + @vertices.length
109
+ @vertices = []
110
+ object = Object3D.new
111
+ object.name = line[2..-1].strip
112
+ @group.add(object)
113
+ elsif /^g / =~ line
114
+ # group
115
+ mesh_n(line[2..-1].strip, nil)
116
+ elsif /^usemtl / =~ line
117
+ # material
118
+ mesh_n(nil, line[7..-1].strip)
119
+ elsif /^mtllib / =~ line
120
+ # mtl file
121
+
122
+ # TODO: ???
123
+ # if mtllib_callback
124
+ # mtlfile = line[7..-1].strip
125
+ # mtllib_callback.(mtlfile)
126
+ # end
127
+ elsif /^s / =~ line
128
+ # Smooth shading
129
+ else
130
+ puts "Mittsu::OBJMTLLoader: Unhandled line #{line}"
131
+ end
132
+ end
133
+
134
+ mesh_n(nil, nil)
135
+
136
+ @group
137
+ end
138
+
139
+ private
140
+
141
+ def vector(x, y, z)
142
+ Vector3.new(x, y, z)
143
+ end
144
+
145
+ def uv(u, v)
146
+ Vector2.new(u, v)
147
+ end
148
+
149
+ def face3(a, b, c, normals = nil)
150
+ Face3.new(a, b, c, normals)
151
+ end
152
+
153
+ def mesh_n(mesh_name = nil, material_name = nil)
154
+ if !@vertices.empty?
155
+ @geometry.vertices = @vertices
156
+
157
+ @geometry.merge_vertices
158
+ @geometry.compute_face_normals
159
+ @geometry.compute_bounding_sphere
160
+
161
+ @object.add(@mesh)
162
+
163
+ @geometry = Geometry.new
164
+ @mesh = Mesh.new(@geometry, @material)
165
+ end
166
+
167
+ @mesh.name = mesh_name unless mesh_name.nil?
168
+
169
+ if !material_name.nil?
170
+ @material = MeshLambertMaterial.new
171
+ @material.name = material_name
172
+
173
+ @mesh.material = @material
174
+ end
175
+ end
176
+
177
+ def add_face(a, b, c, normal_inds = nil)
178
+ if normal_inds.nil?
179
+ @geometry.faces << face3(
180
+ a.to_i - (@face_offset + 1),
181
+ b.to_i - (@face_offset + 1),
182
+ c.to_i - (@face_offset + 1)
183
+ )
184
+ else
185
+ @geometry.faces << face3(
186
+ a.to_i - (@face_offset + 1),
187
+ b.to_i - (@face_offset + 1),
188
+ c.to_i - (@face_offset + 1),
189
+ normal_inds.take(3).map { |i| @normals[i.to_i - 1].clone }
190
+ )
191
+ end
192
+ end
193
+
194
+ def add_uvs(a, b, c)
195
+ @geometry.face_vertex_uvs[0] << [
196
+ @uvs[a.to_i - 1].clone,
197
+ @uvs[b.to_i - 1].clone,
198
+ @uvs[c.to_i - 1].clone
199
+ ]
200
+ end
201
+
202
+ def handle_face_line(faces, uvs = nil, normal_inds = nil)
203
+ if faces[3].nil?
204
+ add_face(faces[0], faces[1], faces[2], normal_inds)
205
+
206
+ if !uvs.nil? && !uvs.empty?
207
+ add_uvs(uvs[0], uvs[1], uvs[2])
208
+ end
209
+ else
210
+ if !normal_inds.nil? && !normal_inds.empty?
211
+ add_face(faces[0], faces[1], fances[3], [normal_inds[0], normal_inds[1], normal_inds[3]])
212
+ add_face(faces[1], faces[2], fances[3], [normal_inds[1], normal_inds[2], normal_inds[3]])
213
+ else
214
+ add_face(faces[0], faces[1], faces[3])
215
+ add_face(faces[1], faces[2], faces[3])
216
+ end
217
+
218
+ if !uvs.nil? && !uvs.empty?
219
+ add_uvs(uvs[0], uvs[1], uvs[2])
220
+ add_uvs(uvs[1], uvs[2], uvs[2])
221
+ end
222
+ end
223
+ end
224
+ end
225
+ end
@@ -0,0 +1,7 @@
1
+ require 'mittsu/materials/material'
2
+ require 'mittsu/materials/mesh_basic_material'
3
+ require 'mittsu/materials/mesh_lambert_material'
4
+ require 'mittsu/materials/mesh_phong_material'
5
+ require 'mittsu/materials/shader_material'
6
+ require 'mittsu/materials/line_basic_material'
7
+ require 'mittsu/materials/mesh_face_material'
@@ -0,0 +1,39 @@
1
+ module Mittsu
2
+ class LineBasicMaterial < Material
3
+ attr_accessor :line_width, :line_cap, :line_join
4
+
5
+ def initialize(parameters = {})
6
+ super()
7
+
8
+ @type = 'LineBasicMaterial'
9
+
10
+ @color = Color.new(0xffffff)
11
+
12
+ @line_width = 1.0
13
+ @line_cap = :round
14
+ @line_join = :round
15
+
16
+ @vertex_colors = NoColors
17
+
18
+ @fog = true
19
+
20
+ self.set_values(parameters)
21
+ end
22
+
23
+ def clone
24
+ LineBasicMaterial.new.tap do |material|
25
+ super(material)
26
+
27
+ material.color.copy(@color)
28
+
29
+ material.line_width = @line_width
30
+ material.line_cap = @line_cap
31
+ material.line_join = @line_join
32
+
33
+ material.vertex_colors = @vertex_colors
34
+
35
+ material.fog = @fog
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,156 @@
1
+ require 'securerandom'
2
+ require 'mittsu'
3
+ require 'mittsu/core/hash_object'
4
+
5
+ module Mittsu
6
+ class Material < HashObject
7
+ include EventDispatcher
8
+
9
+ attr_reader :id, :uuid, :type
10
+
11
+ attr_accessor :name, :side, :opacity, :transparent, :blending, :blend_src, :blend_dst, :blend_equation, :blend_src_alpha, :blend_dst_alpha, :blend_equation_alpha, :depth_test, :depth_write, :color_write, :polygon_offset, :polygon_offset_factor, :polygon_offset_units, :alpha_test, :overdraw, :visible, :attributes, :shading, :program
12
+
13
+ attr_accessor :map, :env_map, :light_map, :light_map, :normal_map, :specular_map, :alpha_map, :combine, :vertex_colors, :fog, :size_attenuation, :skinning, :morph_targets, :morph_normals, :metal, :wrap_around, :defines, :lights, :color, :bump_map, :reflectivity, :refraction_ratio, :wireframe, :default_attribute_values, :uniforms, :vertex_shader, :fragment_shader
14
+
15
+ def initialize
16
+ super
17
+ @id = (@@id ||= 1).tap { @@id += 1 }
18
+
19
+ @uuid = SecureRandom.uuid
20
+
21
+ @name = ''
22
+ @type = 'Material'
23
+
24
+ @side = FrontSide
25
+
26
+ @opacity = 1.0
27
+ @transparent = false
28
+
29
+ @blending = NormalBlending
30
+
31
+ @blend_src = SrcAlphaFactor
32
+ @blend_dst = OneMinusSrcAlphaFactor
33
+ @blend_equation = AddEquation
34
+ @blend_src_alpha = nil
35
+ @blend_dst_alpha = nil
36
+ @blend_equation_alpha = nil
37
+
38
+ @depth_test = true
39
+ @depth_write = true
40
+
41
+ @color_write = true
42
+
43
+ @polygon_offset = false
44
+ @polygon_offset_factor = 0
45
+ @polygon_offset_units = 0
46
+
47
+ @alpha_test = 0
48
+
49
+ # TODO: remove this maybe???
50
+ @overdraw = 0 # Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
51
+
52
+ @visible = true
53
+
54
+ @_needs_update = true
55
+ end
56
+
57
+ def needs_update?
58
+ @_needs_update
59
+ end
60
+
61
+ def needs_update=(value)
62
+ update if value
63
+ @_needs_update = value
64
+ end
65
+
66
+ def set_values(values = nil)
67
+ return if values.nil?
68
+
69
+ values.each do |(key, new_value)|
70
+ if new_value.nil?
71
+ puts "WARNING: Mittsu::Material: #{key} parameter is undefined"
72
+ next
73
+ end
74
+
75
+ if has_property? key
76
+ current_value = get_property(key)
77
+
78
+ if current_value.is_a? Color
79
+ current_value.set(new_value)
80
+ elsif current_value.is_a?(Vector3) && new_value.is_a?(Vector3)
81
+ current_value.copy(new_value)
82
+ else
83
+ set_property(key, new_value)
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ def to_json
90
+ output = {
91
+ metadata: {
92
+ version: 4.2,
93
+ type: 'material',
94
+ generator: 'MaterialExporter'
95
+ },
96
+ uuid: @uuid,
97
+ type: @type
98
+ }
99
+
100
+ output[:name] = @name if !@name.nil? && !@name.empty?
101
+
102
+ output[:opacity] = @opacity if @opacity < 1.0
103
+ output[:transparent] = @transparent if @transparent
104
+ output[:wireframe] = @wireframe if @wireframe
105
+ output
106
+ end
107
+
108
+ def clone(material = Material.new)
109
+ material.name = @name
110
+ material.side = @side
111
+ material.opacity = @opacity
112
+ material.transparent = @transparent
113
+ material.blending = @blending
114
+ material.blend_src = @blend_src
115
+ material.blend_dst = @blend_dst
116
+ material.blend_equation = @blend_equation
117
+ material.blend_src_alpha = @blend_src_alpha
118
+ material.blend_dst_alpha = @blend_dst_alpha
119
+ material.blend_equation_alpha = @blend_equation_alpha
120
+ material.depth_test = @depth_test
121
+ material.depth_write = @depth_write
122
+ material.color_write = @color_write
123
+ material.polygon_offset = @polygon_offset
124
+ material.polygon_offset_factor = @polygon_offset_factor
125
+ material.polygon_offset_units = @polygon_offset_units
126
+ material.alpha_test = @alpha_test
127
+ material.overdraw = @overdraw
128
+ material.visible = @visible
129
+ end
130
+
131
+ def update
132
+ dispatch_event type: :update
133
+ end
134
+
135
+ def dispose
136
+ dispatch_event type: :dispose
137
+ end
138
+
139
+ private
140
+
141
+ def has_property?(key)
142
+ sym = "@#{key}".to_sym
143
+ self.instance_variable_defined?(sym)
144
+ end
145
+
146
+ def set_property(key, value)
147
+ sym = "@#{key}".to_sym
148
+ self.instance_variable_set(sym, value)
149
+ end
150
+
151
+ def get_property(key)
152
+ sym = "@#{key}".to_sym
153
+ self.instance_variable_get(sym)
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,122 @@
1
+ require 'mittsu/math'
2
+ require 'mittsu/materials/material'
3
+
4
+ # parameters = {
5
+ # color: <hex>,
6
+ # opacity: <float>,
7
+ # map: Mittsu::Texture.new( <Image> ),
8
+ #
9
+ # light_map: Mittsu::Texture.new( <Image> ),
10
+ #
11
+ # specular_map: Mittsu::Texture.new( <Image> ),
12
+ #
13
+ # alpha_map: Mittsu::Texture.new( <Image> ),
14
+ #
15
+ # env_map: Mittsu::TextureCube.new( [posx, negx, posy, negy, posz, negz] ),
16
+ # combine: Mittsu::Multiply,
17
+ # reflectivity: <float>,
18
+ # refraction_ratio: <float>,
19
+ #
20
+ # shading: Mittsu::SmoothShading,
21
+ # blending: Mittsu::NormalBlending,
22
+ # depth_test: <bool>,
23
+ # depth_write: <bool>,
24
+ #
25
+ # wireframe: <boolean>,
26
+ # wireframe_linewidth: <float>,
27
+ #
28
+ # vertex_colors: Mittsu::NoColors / Mittsu::VertexColors / Mittsu::FaceColors,
29
+ #
30
+ # skinning: <bool>,
31
+ # morph_targets: <bool>,
32
+ #
33
+ # fog: <bool>
34
+ # }
35
+ module Mittsu
36
+ attr_accessor :color, :map, :light_map, :specular_map, :alpha_map, :env_map, :combine, :reflectivity, :refraction_ratio, :shading, :wireframe, :wireframe_linewidth, :wireframe_linecap, :wireframe_linejoin, :vertex_colors, :skinning, :morph_targets, :fog
37
+
38
+ class MeshBasicMaterial < Material
39
+ def initialize(parameters = {})
40
+ super()
41
+
42
+ @type = 'MeshBasicMaterial'
43
+
44
+ @color = Color.new(0xffffff) # emissive
45
+
46
+ @map = nil
47
+
48
+ @light_map = nil
49
+
50
+ @specular_map = nil
51
+
52
+ @alpha_map = nil
53
+
54
+ @env_map = nil
55
+ @combine = MultiplyOperation
56
+ @reflectivity = 1.0
57
+ @refraction_ratio = 0.98
58
+
59
+ @fog = true
60
+
61
+ @shading = SmoothShading
62
+
63
+ @wireframe = false
64
+ @wireframe_linewidth = 1
65
+ @wireframe_linecap = :round
66
+ @wireframe_linejoin = :round
67
+
68
+ @vertex_colors = NoColors
69
+
70
+ @skinning = false
71
+ @morph_targets = false
72
+
73
+ set_values(parameters)
74
+ end
75
+
76
+ def clone
77
+ material = Material.new
78
+
79
+ super(material)
80
+
81
+ material.color.copy(@color)
82
+
83
+ material.map = @map
84
+
85
+ material.lightMap = @lightMap
86
+
87
+ material.specularMap = @specularMap
88
+
89
+ material.alphaMap = @alphaMap
90
+
91
+ material.envMap = @envMap
92
+ material.combine = @combine
93
+ material.reflectivity = @reflectivity
94
+ material.refractionRatio = @refractionRatio
95
+
96
+ material.fog = @fog
97
+
98
+ material.shading = @shading
99
+
100
+ material.wireframe = @wireframe
101
+ material.wireframeLinewidth = @wireframeLinewidth
102
+ material.wireframeLinecap = @wireframeLinecap
103
+ material.wireframeLinejoin = @wireframeLinejoin
104
+
105
+ material.vertexColors = @vertexColors
106
+
107
+ material.skinning = @skinning
108
+ material.morphTargets = @morphTargets
109
+
110
+ material
111
+ end
112
+
113
+ def to_json
114
+ output = super
115
+ output[:color] = @color.hex
116
+ output[:vertex_colors] = @vertex_colors unless @vertex_colors == NoColors
117
+ output[:blending] = @blending unless @blending == NormalBlending
118
+ output[:side] = @side unless @side == FrontSide
119
+ output
120
+ end
121
+ end
122
+ end