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,491 @@
1
+ require 'mittsu/math'
2
+
3
+ module Mittsu
4
+ class Vector3
5
+ attr_reader :x, :y, :z
6
+
7
+ def initialize(x = 0, y = 0, z = 0)
8
+ @x = x.to_f
9
+ @y = y.to_f
10
+ @z = z.to_f
11
+ end
12
+
13
+ def x=(value)
14
+ @x = value.to_f
15
+ end
16
+
17
+ def y=(value)
18
+ @y = value.to_f
19
+ end
20
+
21
+ def z=(value)
22
+ @z = value.to_f
23
+ end
24
+
25
+ def set(x, y, z)
26
+ @x = x.to_f
27
+ @y = y.to_f
28
+ @z = z.to_f
29
+ self
30
+ end
31
+
32
+ def []=(index, value)
33
+ return @x = value.to_f if index == 0 || index == :x
34
+ return @y = value.to_f if index == 1 || index == :y
35
+ return @z = value.to_f if index == 2 || index == :z
36
+ raise IndexError
37
+ end
38
+
39
+ def [](index)
40
+ return @x if index == 0 || index == :x
41
+ return @y if index == 1 || index == :y
42
+ return @z if index == 2 || index == :z
43
+ raise IndexError
44
+ end
45
+
46
+ def copy(v)
47
+ @x = v.x
48
+ @y = v.y
49
+ @z = v.z
50
+ self
51
+ end
52
+
53
+ def add(v)
54
+ @x += v.x
55
+ @y += v.y
56
+ @z += v.z
57
+ self
58
+ end
59
+
60
+ def add_scalar(s)
61
+ @x += s
62
+ @y += s
63
+ @z += s
64
+ self
65
+ end
66
+
67
+ def add_vectors(a, b)
68
+ @x = a.x + b.x
69
+ @y = a.y + b.y
70
+ @z = a.z + b.z
71
+ self
72
+ end
73
+
74
+ def sub(v)
75
+ @x -= v.x
76
+ @y -= v.y
77
+ @z -= v.z
78
+ self
79
+ end
80
+
81
+ def sub_scalar(s)
82
+ @x -= s
83
+ @y -= s
84
+ @z -= s
85
+ self
86
+ end
87
+
88
+ def sub_vectors(a, b)
89
+ @x = a.x - b.x
90
+ @y = a.y - b.y
91
+ @z = a.z - b.z
92
+ self
93
+ end
94
+
95
+ def multiply(v)
96
+ @x *= v.x
97
+ @y *= v.y
98
+ @z *= v.z
99
+ self
100
+ end
101
+
102
+ def multiply_scalar(scalar)
103
+ @x *= scalar
104
+ @y *= scalar
105
+ @z *= scalar
106
+ self
107
+ end
108
+
109
+ def multiply_vectors(a, b)
110
+ @x = a.x * b.x
111
+ @y = a.y * b.y
112
+ @z = a.z * b.z
113
+ self
114
+ end
115
+
116
+ def apply_euler(euler)
117
+ quaternion = Mittsu::Quaternion.new
118
+ self.apply_quaternion(quaternion.set_from_euler(euler))
119
+ self
120
+ end
121
+
122
+ def apply_axis_angle(axis, angle)
123
+ quaternion = Mittsu::Quaternion.new
124
+ self.apply_quaternion(quaternion.set_from_axis_angle(axis, angle))
125
+ self
126
+ end
127
+
128
+ def apply_matrix3(m)
129
+ x = @x
130
+ y = @y
131
+ z = @z
132
+
133
+ e = m.elements
134
+
135
+ @x = e[0] * x + e[3] * y + e[6] * z
136
+ @y = e[1] * x + e[4] * y + e[7] * z
137
+ @z = e[2] * x + e[5] * y + e[8] * z
138
+
139
+ self
140
+ end
141
+
142
+ def apply_matrix4(m)
143
+ # input: THREE.Matrix4 affine matrix
144
+
145
+ xx, yy, zz = @x, @y, @z
146
+
147
+ e = m.elements
148
+
149
+ @x = e[0] * xx + e[4] * yy + e[8] * zz + e[12]
150
+ @y = e[1] * xx + e[5] * yy + e[9] * zz + e[13]
151
+ @z = e[2] * xx + e[6] * yy + e[10] * zz + e[14]
152
+
153
+ self
154
+ end
155
+
156
+ def apply_projection(m)
157
+ # input: THREE.Matrix4 projection matrix
158
+
159
+ _x, _y, _z = @x, @y, @z
160
+
161
+ e = m.elements
162
+ d = 1.0 / (e[3] * _x + e[7] * _y + e[11] * _z + e[15]) # perspective divide
163
+
164
+ @x = (e[0] * _x + e[4] * _y + e[8] * _z + e[12]) * d
165
+ @y = (e[1] * _x + e[5] * _y + e[9] * _z + e[13]) * d
166
+ @z = (e[2] * _x + e[6] * _y + e[10] * _z + e[14]) * d
167
+
168
+ self
169
+ end
170
+
171
+ def apply_quaternion(q)
172
+ x = @x
173
+ y = @y
174
+ z = @z
175
+
176
+ qx = q.x
177
+ qy = q.y
178
+ qz = q.z
179
+ qw = q.w
180
+
181
+ # calculate quat * vector
182
+
183
+ ix = qw * x + qy * z - qz * y
184
+ iy = qw * y + qz * x - qx * z
185
+ iz = qw * z + qx * y - qy * x
186
+ iw = - qx * x - qy * y - qz * z
187
+
188
+ # calculate result * inverse quat
189
+
190
+ @x = ix * qw + iw * - qx + iy * - qz - iz * - qy
191
+ @y = iy * qw + iw * - qy + iz * - qx - ix * - qz
192
+ @z = iz * qw + iw * - qz + ix * - qy - iy * - qx
193
+
194
+ self
195
+ end
196
+
197
+ def project(camera)
198
+ matrix = Mittsu::Matrix4.new
199
+ matrix.multiply_matrices(camera.projection_matrix, matrix.get_inverse(camera.matrix_world))
200
+ self.apply_projection(matrix)
201
+ end
202
+
203
+ def unproject(camera)
204
+ matrix = Mittsu::Matrix4.new
205
+ matrix.multiply_matrices(camera.matrix_world, matrix.inverse(camera.projection_matrix))
206
+ self.apply_projection(matrix)
207
+ end
208
+
209
+ def transform_direction(m)
210
+ # input: THREE.Matrix4 affine matrix
211
+ # vector interpreted as a direction
212
+
213
+ x = @x, y = @y, z = @z
214
+
215
+ e = m.elements
216
+
217
+ @x = e[0] * x + e[4] * y + e[8] * z
218
+ @y = e[1] * x + e[5] * y + e[9] * z
219
+ @z = e[2] * x + e[6] * y + e[10] * z
220
+
221
+ self.normalize
222
+
223
+ self
224
+ end
225
+
226
+ def divide(v)
227
+ @x /= v.x
228
+ @y /= v.y
229
+ @z /= v.z
230
+ self
231
+ end
232
+
233
+ def divide_scalar(scalar)
234
+ if scalar != 0
235
+ invScalar = 1.0 / scalar
236
+ @x *= invScalar
237
+ @y *= invScalar
238
+ @z *= invScalar
239
+ else
240
+ @x = 0
241
+ @y = 0
242
+ @z = 0
243
+ end
244
+ self
245
+ end
246
+
247
+ def min(v)
248
+ if @x > v.x
249
+ @x = v.x
250
+ end
251
+ if @y > v.y
252
+ @y = v.y
253
+ end
254
+ if @z > v.z
255
+ @z = v.z
256
+ end
257
+ self
258
+ end
259
+
260
+ def max(v)
261
+ if @x < v.x
262
+ @x = v.x
263
+ end
264
+ if @y < v.y
265
+ @y = v.y
266
+ end
267
+ if @z < v.z
268
+ @z = v.z
269
+ end
270
+ self
271
+ end
272
+
273
+ def clamp(min, max)
274
+ @x = Math.clamp(@x, min.x, max.x)
275
+ @y = Math.clamp(@y, min.y, max.y)
276
+ @z = Math.clamp(@z, min.z, max.z)
277
+ self
278
+ end
279
+
280
+ def clamp_scalar(min, max)
281
+ @x = Math.clamp(@x, min, max)
282
+ @y = Math.clamp(@y, min, max)
283
+ @z = Math.clamp(@z, min, max)
284
+ self
285
+ end
286
+
287
+ def floor
288
+ @x = @x.floor.to_f
289
+ @y = @y.floor.to_f
290
+ @z = @z.floor.to_f
291
+ self
292
+ end
293
+
294
+ def ceil
295
+ @x = @x.ceil.to_f
296
+ @y = @y.ceil.to_f
297
+ @z = @z.ceil.to_f
298
+ self
299
+ end
300
+
301
+ def round
302
+ @x = @x.round.to_f
303
+ @y = @y.round.to_f
304
+ @z = @z.round.to_f
305
+ self
306
+ end
307
+
308
+ def round_to_zero
309
+ @x = (@x < 0) ? @x.ceil.to_f : @x.floor.to_f
310
+ @y = (@y < 0) ? @y.ceil.to_f : @y.floor.to_f
311
+ @z = (@z < 0) ? @z.ceil.to_f : @z.floor.to_f
312
+ self
313
+ end
314
+
315
+ def negate
316
+ @x = - @x
317
+ @y = - @y
318
+ @z = - @z
319
+ self
320
+ end
321
+
322
+ def dot(v)
323
+ @x * v.x + @y * v.y + @z * v.z
324
+ end
325
+
326
+ def length_sq
327
+ self.dot(self)
328
+ end
329
+
330
+ def length
331
+ Math.sqrt(length_sq)
332
+ end
333
+
334
+ def length_manhattan
335
+ @x.abs + @y.abs + @z.abs
336
+ end
337
+
338
+ def normalize
339
+ self.divide_scalar(self.length)
340
+ end
341
+
342
+ def set_length(l)
343
+ old_length = self.length
344
+ if old_length != 0 && l != old_length
345
+ self.multiply_scalar(l / old_length)
346
+ end
347
+ self
348
+ end
349
+
350
+ def lerp(v, alpha)
351
+ @x += (v.x - @x) * alpha
352
+ @y += (v.y - @y) * alpha
353
+ @z += (v.z - @z) * alpha
354
+ self
355
+ end
356
+
357
+ def lerp_vectors(v1, v2, alpha)
358
+ self.sub_vectors(v2, v1).multiply_scalar(alpha).add(v1)
359
+ self
360
+ end
361
+
362
+ def cross(v)
363
+ x, y, z = @x, @y, @z
364
+ @x = y * v.z - z * v.y
365
+ @y = z * v.x - x * v.z
366
+ @z = x * v.y - y * v.x
367
+ self
368
+ end
369
+
370
+ def cross_vectors(a, b)
371
+ ax, ay, az = a.x, a.y, a.z
372
+ bx, by, bz = b.x, b.y, b.z
373
+
374
+ @x = ay * bz - az * by
375
+ @y = az * bx - ax * bz
376
+ @z = ax * by - ay * bx
377
+
378
+ self
379
+ end
380
+
381
+ def project_on_vector(vector)
382
+ v1 = Mittsu::Vector3.new
383
+ v1.copy(vector).normalize
384
+ dot = self.dot(v1)
385
+ self.copy(v1).multiply_scalar(dot)
386
+ end
387
+
388
+ def project_on_plane(plane_normal)
389
+ v1 = Mittsu::Vector3.new
390
+ v1.copy(self).project_on_vector(plane_normal)
391
+ self.sub(v1)
392
+ end
393
+
394
+ def reflect(normal)
395
+ # reflect incident vector off plane orthogonal to normal
396
+ # normal is assumed to have unit length
397
+ v1 = Mittsu::Vector3.new
398
+ self.sub(v1.copy(normal).multiply_scalar(2.0 * self.dot(normal)))
399
+ end
400
+
401
+ def angle_to(v)
402
+ theta = self.dot(v) / (self.length * v.length)
403
+
404
+ # clamp, to handle numerical problems
405
+ Math.acos(Math.clamp(theta, -1.0, 1.0))
406
+ end
407
+
408
+ def distance_to(v)
409
+ Math.sqrt(self.distance_to_squared(v))
410
+ end
411
+
412
+ def distance_to_squared(v)
413
+ dx = @x - v.x
414
+ dy = @y - v.y
415
+ dz = @z - v.z
416
+ dx * dx + dy * dy + dz * dz
417
+ end
418
+
419
+ def set_from_matrix_position(m)
420
+ @x = m.elements[12]
421
+ @y = m.elements[13]
422
+ @z = m.elements[14]
423
+ self
424
+ end
425
+
426
+ def set_from_matrix_scale(m)
427
+ sx = self.set(m.elements[0], m.elements[1], m.elements[ 2]).length
428
+ sy = self.set(m.elements[4], m.elements[5], m.elements[ 6]).length
429
+ sz = self.set(m.elements[8], m.elements[9], m.elements[10]).length
430
+
431
+ @x = sx
432
+ @y = sy
433
+ @z = sz
434
+
435
+ self
436
+ end
437
+
438
+ def set_from_matrix_column(index, matrix)
439
+ offset = index * 4
440
+
441
+ me = matrix.elements
442
+
443
+ @x = me[offset]
444
+ @y = me[offset + 1]
445
+ @z = me[offset + 2]
446
+
447
+ self
448
+ end
449
+
450
+ def ==(v)
451
+ ((v.x == @x) && (v.y == @y) && (v.z == @z))
452
+ end
453
+
454
+ def fromArray(array, offset = 0)
455
+ @x = array[offset]
456
+ @y = array[offset + 1]
457
+ @z = array[offset + 2]
458
+ self
459
+ end
460
+
461
+ def to_array(array = [], offset = 0)
462
+ array[offset] = @x
463
+ array[offset + 1] = @y
464
+ array[offset + 2] = @z
465
+
466
+ array
467
+ end
468
+
469
+ def to_a
470
+ self.to_array
471
+ end
472
+
473
+ def from_attribute(attribute, index, offse = 0)
474
+ index = index * attribute.itemSize + offset
475
+
476
+ @x = attribute.array[index]
477
+ @y = attribute.array[index + 1]
478
+ @z = attribute.array[index + 2]
479
+
480
+ self
481
+ end
482
+
483
+ def clone
484
+ Mittsu::Vector3.new(@x, @y, @z)
485
+ end
486
+
487
+ def to_s
488
+ "[#{x}, #{y}, #{z}]"
489
+ end
490
+ end
491
+ end