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,135 @@
1
+ require 'mittsu/math'
2
+
3
+ module Mittsu
4
+ class Box2
5
+ attr_accessor :min, :max
6
+
7
+ def initialize(min = nil, max = nil)
8
+ @min = min || Mittsu::Vector2.new(Float::INFINITY, Float::INFINITY)
9
+ @max = max || Mittsu::Vector2.new(-Float::INFINITY, -Float::INFINITY)
10
+ end
11
+
12
+ def set(min, max)
13
+ @min.copy(min)
14
+ @max.copy(max)
15
+ self
16
+ end
17
+
18
+ def set_from_points(points)
19
+ self.make_empty
20
+ points.each do |point|
21
+ self.expand_by_point(point)
22
+ end
23
+ self
24
+ end
25
+
26
+ def set_from_center_and_size(center, size)
27
+ halfSize = size.clone.multiply_scalar(0.5)
28
+ @min.copy(center).sub(halfSize)
29
+ @max.copy(center).add(halfSize)
30
+ self
31
+ end
32
+
33
+ def copy(box)
34
+ @min.copy(box.min)
35
+ @max.copy(box.max)
36
+ self
37
+ end
38
+
39
+ def make_empty
40
+ @min.x = @min.y = Float::INFINITY
41
+ @max.x = @max.y = - Float::INFINITY
42
+ self
43
+ end
44
+
45
+ def empty?
46
+ # this is a more robust check for empty than (volume <= 0) because volume can get positive with two negative axes
47
+ (@max.x < @min.x) || (@max.y < @min.y)
48
+ end
49
+
50
+ def center(target = Mittsu::Vector2.new)
51
+ target.add_vectors(@min, @max).multiply_scalar(0.5)
52
+ end
53
+
54
+ def size(target = Mittsu::Vector2.new)
55
+ target.sub_vectors(@max, @min)
56
+ end
57
+
58
+ def expand_by_point(point)
59
+ @min.min(point)
60
+ @max.max(point)
61
+ self
62
+ end
63
+
64
+ def expand_by_vector(vector)
65
+ @min.sub(vector)
66
+ @max.add(vector)
67
+ self
68
+ end
69
+
70
+ def expand_by_scalar(scalar)
71
+ @min.add_scalar(-scalar)
72
+ @max.add_scalar(scalar)
73
+ self
74
+ end
75
+
76
+ def contains_point?(point)
77
+ !(point.x < @min.x || point.x > @max.x || point.y < @min.y || point.y > @max.y)
78
+ end
79
+
80
+ def contains_box?(box)
81
+ ((@min.x <= box.min.x) && (box.max.x <= @max.x) && (@min.y <= box.min.y) && (box.max.y <= @max.y))
82
+ end
83
+
84
+ def parameter(point, target = Mittsu::Vector2.new)
85
+ # This can potentially have a divide by zero if the box
86
+ # has a size dimension of 0.
87
+ target.set(
88
+ (point.x - @min.x) / (@max.x - @min.x),
89
+ (point.y - @min.y) / (@max.y - @min.y)
90
+ )
91
+ end
92
+
93
+ def intersection_box?(box)
94
+ # using 6 splitting planes to rule out intersections.
95
+ !(box.max.x < @min.x || box.min.x > @max.x || box.max.y < @min.y || box.min.y > @max.y)
96
+ end
97
+
98
+ def clamp_point(point, target = nil)
99
+ result = target || Mittsu::Vector2.new
100
+ result.copy(point).clamp(@min, @max)
101
+ end
102
+
103
+ def distance_to_point(point)
104
+ clampedPoint = point.clone.clamp(@min, @max)
105
+ clampedPoint.sub(point).length
106
+ end
107
+
108
+ def intersect(box)
109
+ @min.max(box.min)
110
+ @max.min(box.max)
111
+ self
112
+ end
113
+
114
+ def union(box)
115
+ @min.min(box.min)
116
+ @max.max(box.max)
117
+ self
118
+ end
119
+
120
+ def translate(offset)
121
+ @min.add(offset)
122
+ @max.add(offset)
123
+ self
124
+ end
125
+
126
+ def ==(box)
127
+ box.min == @min && box.max == @max
128
+ end
129
+
130
+ def clone
131
+ Mittsu::Box2.new.copy(self)
132
+ end
133
+
134
+ end
135
+ end
@@ -0,0 +1,194 @@
1
+ require 'mittsu/math'
2
+
3
+ module Mittsu
4
+ class Box3
5
+ attr_accessor :min, :max
6
+
7
+ def initialize(min = nil, max = nil)
8
+ @min = min || Mittsu::Vector3.new(Float::INFINITY, Float::INFINITY, Float::INFINITY)
9
+ @max = max || Mittsu::Vector3.new(-Float::INFINITY, -Float::INFINITY, -Float::INFINITY)
10
+ end
11
+
12
+ def set(min, max)
13
+ @min.copy(min)
14
+ @max.copy(max)
15
+ self
16
+ end
17
+
18
+ def set_from_points(points)
19
+ self.make_empty
20
+ points.each do |point|
21
+ self.expand_by_point(point)
22
+ end
23
+ self
24
+ end
25
+
26
+ def set_from_center_and_size(center, size)
27
+ halfSize = size.clone.multiply_scalar(0.5)
28
+ @min.copy(center).sub(halfSize)
29
+ @max.copy(center).add(halfSize)
30
+ self
31
+ end
32
+
33
+ def set_from_object(object)
34
+ # Computes the world-axis-aligned bounding box of an object (including its children),
35
+ # accounting for both the object's, and childrens', world transforms
36
+ v1 = Mittsu::Vector3.new
37
+ scope = self
38
+ object.update_matrix_world(true)
39
+ self.make_empty
40
+ object.traverse do |node|
41
+ geometry = node.geometry
42
+ if geometry != nil
43
+ if geometry instanceof Mittsu::Geometry
44
+ vertices = geometry.vertices
45
+ vertices.each do |vertex|
46
+ v1.copy(vertex)
47
+ v1.apply_matrix4(node.matrixWorld)
48
+ scope.expand_by_point(v1)
49
+ end
50
+ elsif geometry instanceof Mittsu::BufferGeometry && geometry.attributes['position'] != nil
51
+ positions = geometry.attributes['position'].array
52
+ positions.each_slice(3) do |postition|
53
+ v1.set(position[0], position[1], position[2])
54
+ v1.apply_matrix4(node.matrixWorld)
55
+ scope.expand_by_point(v1)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ return self
61
+ end
62
+
63
+ def copy(box)
64
+ @min.copy(box.min)
65
+ @max.copy(box.max)
66
+ self
67
+ end
68
+
69
+ def make_empty
70
+ @min.x = @min.y = @min.z = Float::INFINITY
71
+ @max.x = @max.y = @max.z = -Float::INFINITY
72
+ self
73
+ end
74
+
75
+ def empty?
76
+ # self is a more robust check for empty than (volume <= 0) because volume can get positive with two negative axes
77
+ (@max.x < @min.x) || (@max.y < @min.y) || (@max.z < @min.z)
78
+ end
79
+
80
+ def center(target = Mittsu::Vector3.new)
81
+ target.add_vectors(@min, @max).multiply_scalar(0.5)
82
+ end
83
+
84
+ def size(target = Mittsu::Vector3.new)
85
+ target.sub_vectors(@max, @min)
86
+ end
87
+
88
+ def expand_by_point(point)
89
+ @min.min(point)
90
+ @max.max(point)
91
+ self
92
+ end
93
+
94
+ def expand_by_vector(vector)
95
+ @min.sub(vector)
96
+ @max.add(vector)
97
+ self
98
+ end
99
+
100
+ def expand_by_scalar(scalar)
101
+ @min.add_scalar(-scalar)
102
+ @max.add_scalar(scalar)
103
+ self
104
+ end
105
+
106
+ def contains_point?(point)
107
+ !(point.x < @min.x || point.x > @max.x ||
108
+ point.y < @min.y || point.y > @max.y ||
109
+ point.z < @min.z || point.z > @max.z)
110
+ end
111
+
112
+ def contains_box?(box)
113
+ ((@min.x <= box.min.x) && (box.max.x <= @max.x) &&
114
+ (@min.y <= box.min.y) && (box.max.y <= @max.y) &&
115
+ (@min.z <= box.min.z) && (box.max.z <= @max.z))
116
+ end
117
+
118
+ def parameter(point, target = nil)
119
+ # This can potentially have a divide by zero if the box
120
+ # has a size dimension of 0.
121
+ result = target || Mittsu::Vector3.new
122
+ result.set(
123
+ (point.x - @min.x) / (@max.x - @min.x),
124
+ (point.y - @min.y) / (@max.y - @min.y),
125
+ (point.z - @min.z) / (@max.z - @min.z)
126
+ )
127
+ end
128
+
129
+ def intersection_box?(box)
130
+ # using 6 splitting planes to rule out intersections.
131
+ !(box.max.x < @min.x || box.min.x > @max.x ||
132
+ box.max.y < @min.y || box.min.y > @max.y ||
133
+ box.max.z < @min.z || box.min.z > @max.z)
134
+ end
135
+
136
+ def clamp_point(point, target = Mittsu::Vector3.new)
137
+ target.copy(point).clamp(@min, @max)
138
+ end
139
+
140
+ def distance_to_point(point, target = Mittsu::Vector3.new)
141
+ target.copy(point).clamp(@min, @max).sub(point).length
142
+ end
143
+
144
+ def bounding_sphere(target = Mittsu::Sphere.new)
145
+ target.center = self.center
146
+ target.radius = self.size().length * 0.5
147
+ target
148
+ end
149
+
150
+ def intersect(box)
151
+ @min.max(box.min)
152
+ @max.min(box.max)
153
+ self
154
+ end
155
+
156
+ def union(box)
157
+ @min.min(box.min)
158
+ @max.max(box.max)
159
+ self
160
+ end
161
+
162
+ def apply_matrix4(matrix)
163
+ points = [
164
+ # NOTE: I am using a binary pattern to specify all 2^3 combinations below
165
+ Mittsu::Vector3.new(@min.x, @min.y, @min.z).apply_matrix4(matrix), # 000
166
+ Mittsu::Vector3.new(@min.x, @min.y, @max.z).apply_matrix4(matrix), # 001
167
+ Mittsu::Vector3.new(@min.x, @max.y, @min.z).apply_matrix4(matrix), # 010
168
+ Mittsu::Vector3.new(@min.x, @max.y, @max.z).apply_matrix4(matrix), # 011
169
+ Mittsu::Vector3.new(@max.x, @min.y, @min.z).apply_matrix4(matrix), # 100
170
+ Mittsu::Vector3.new(@max.x, @min.y, @max.z).apply_matrix4(matrix), # 101
171
+ Mittsu::Vector3.new(@max.x, @max.y, @min.z).apply_matrix4(matrix), # 110
172
+ Mittsu::Vector3.new(@max.x, @max.y, @max.z).apply_matrix4(matrix) # 111
173
+ ]
174
+ self.make_empty
175
+ self.set_from_points(points)
176
+ self
177
+ end
178
+
179
+ def translate(offset)
180
+ @min.add(offset)
181
+ @max.add(offset)
182
+ self
183
+ end
184
+
185
+ def ==(box)
186
+ box.min == @min && box.max == @max
187
+ end
188
+
189
+ def clone
190
+ Mittsu::Box3.new.copy(self)
191
+ end
192
+
193
+ end
194
+ end
@@ -0,0 +1,252 @@
1
+ require 'mittsu/math'
2
+
3
+ module Mittsu
4
+ class Color
5
+ attr_accessor :r, :g, :b
6
+
7
+ def initialize(*args)
8
+ case args.length
9
+ when 3 then self.set_rgb(*args)
10
+ when 1 then self.set(args.first)
11
+ when 0 then self.set_rgb(1.0, 1.0, 1.0)
12
+ else raise ArgumentError, "Arguments must be (r, g, b), (color), or none"
13
+ end
14
+ end
15
+
16
+ def set(value)
17
+ case value
18
+ when Color
19
+ self.copy(value)
20
+ when Fixnum
21
+ self.set_hex(value)
22
+ when String
23
+ self.set_style(value)
24
+ else
25
+ raise ArgumentError, "Arguments must be Color, Fixnum or String"
26
+ end
27
+ self
28
+ end
29
+
30
+ def set_hex(hex)
31
+ hex = hex.floor
32
+ @r = (hex >> 16 & 255) / 255.0
33
+ @g = (hex >> 8 & 255) / 255.0
34
+ @b = (hex & 255) / 255.0
35
+ self
36
+ end
37
+
38
+ def set_rgb(r, g, b)
39
+ @r = r.to_f
40
+ @g = g.to_f
41
+ @b = b.to_f
42
+ self
43
+ end
44
+
45
+ def set_hsl(h, s, l)
46
+ # h,s,l ranges are in 0.0 - 1.0
47
+ if s.zero?
48
+ @r = @g = @b = l
49
+ else
50
+ p = l <= 0.5 ? l * (1.0 + s) : l + s - (l * s)
51
+ q = (2.0 * l) - p
52
+ @r = hue2rgb(q, p, h + 1.0 / 3.0)
53
+ @g = hue2rgb(q, p, h)
54
+ @b = hue2rgb(q, p, h - 1.0 / 3.0)
55
+ end
56
+ self
57
+ end
58
+
59
+ def set_style(style)
60
+ # rgb(255,0,0)
61
+ if /^rgb\((\d+), ?(\d+), ?(\d+)\)$/i =~ style
62
+ @r = [255.0, $1.to_f].min / 255.0
63
+ @g = [255.0, $2.to_f].min / 255.0
64
+ @b = [255.0, $3.to_f].min / 255.0
65
+ return self
66
+ end
67
+ # rgb(100%,0%,0%)
68
+ if /^rgb\((\d+)\%, ?(\d+)\%, ?(\d+)\%\)$/i =~ style
69
+ @r = [100.0, $1.to_f].min / 100.0
70
+ @g = [100.0, $2.to_f].min / 100.0
71
+ @b = [100.0, $3.to_f].min / 100.0
72
+ return self
73
+ end
74
+ # #ff0000
75
+ if /^\#([0-9a-f]{6})$/i =~ style
76
+ self.set_hex($1.hex)
77
+ return self
78
+ end
79
+ # #f00
80
+ if /^\#([0-9a-f])([0-9a-f])([0-9a-f])$/i =~ style
81
+ self.set_hex(($1 + $1 + $2 + $2 + $3 + $3).hex)
82
+ return self
83
+ end
84
+ # red
85
+ if /^(\w+)$/i =~ style
86
+ self.set_hex(Mittsu::ColorKeywords[style])
87
+ return self
88
+ end
89
+ end
90
+
91
+ def copy(color)
92
+ @r = color.r
93
+ @g = color.g
94
+ @b = color.b
95
+ self
96
+ end
97
+
98
+ def copy_gamma_to_linear(color, gamma_factor = 2.0)
99
+ @r = color.r ** gamma_factor
100
+ @g = color.g ** gamma_factor
101
+ @b = color.b ** gamma_factor
102
+ self
103
+ end
104
+
105
+ def copy_linear_to_gamma(color, gamma_factor = 2.0)
106
+ safe_inverse = (gamma_factor > 0) ? (1.0 / gamma_factor) : 1.0
107
+ @r = color.r ** safe_inverse
108
+ @g = color.g ** safe_inverse
109
+ @b = color.b ** safe_inverse
110
+ self
111
+ end
112
+
113
+ def convert_gamma_to_linear
114
+ rr, gg, bb = @r, @g, @b
115
+ @r = rr * rr
116
+ @g = gg * gg
117
+ @b = bb * bb
118
+ self
119
+ end
120
+
121
+ def convert_linear_to_gamma
122
+ @r = Math.sqrt(@r)
123
+ @g = Math.sqrt(@g)
124
+ @b = Math.sqrt(@b)
125
+ self
126
+ end
127
+
128
+ def hex
129
+ (@r * 255).to_i << 16 ^ (@g * 255).to_i << 8 ^ (@b * 255).to_i << 0
130
+ end
131
+
132
+ def hex_string
133
+ ('000000' + self.hex.to_s(16))[-6..-1]
134
+ end
135
+
136
+ def hsl(target = nil)
137
+ # h,s,l ranges are in 0.0 - 1.0
138
+ hsl = target || { h: 0.0, s: 0.0, l: 0.0 }
139
+ rr, gg, bb = @r, @g, @b
140
+ max = [r, g, b].max
141
+ min = [r, g, b].min
142
+ hue, saturation = nil, nil
143
+ lightness = (min + max) / 2.0
144
+ if min == max
145
+ hue = 0.0
146
+ saturation = 0.0
147
+ else
148
+ delta = max - min
149
+ saturation = lightness <= 0.5 ? delta / (max + min) : delta / (2.0 - max - min)
150
+ case max
151
+ when rr then hue = (gg - bb) / delta + (gg < bb ? 6.0 : 0.0)
152
+ when gg then hue = (bb - rr) / delta + 2.0
153
+ when bb then hue = (rr - gg) / delta + 4.0
154
+ end
155
+ hue /= 6.0
156
+ end
157
+ hsl[:h] = hue
158
+ hsl[:s] = saturation
159
+ hsl[:l] = lightness
160
+ hsl
161
+ end
162
+
163
+ def style
164
+ "rgb(#{ (@r * 255).to_i },#{ (@g * 255).to_i },#{ (@b * 255).to_i })"
165
+ end
166
+
167
+ def offset_hsl(h, s, l)
168
+ hsl = self.hsl
169
+ hsl[:h] += h
170
+ hsl[:s] += s
171
+ hsl[:l] += l
172
+ self.set_hsl(hsl[:h], hsl[:s], hsl[:l])
173
+ self
174
+ end
175
+
176
+ def add(color)
177
+ @r += color.r
178
+ @g += color.g
179
+ @b += color.b
180
+ self
181
+ end
182
+
183
+ def add_colors(color1, color2)
184
+ @r = color1.r + color2.r
185
+ @g = color1.g + color2.g
186
+ @b = color1.b + color2.b
187
+ self
188
+ end
189
+
190
+ def add_scalar(s)
191
+ @r += s
192
+ @g += s
193
+ @b += s
194
+ self
195
+ end
196
+
197
+ def multiply(color)
198
+ @r *= color.r
199
+ @g *= color.g
200
+ @b *= color.b
201
+ self
202
+ end
203
+
204
+ def multiply_scalar(s)
205
+ @r *= s
206
+ @g *= s
207
+ @b *= s
208
+ self
209
+ end
210
+
211
+ def lerp(color, alpha)
212
+ @r += (color.r - @r) * alpha
213
+ @g += (color.g - @g) * alpha
214
+ @b += (color.b - @b) * alpha
215
+ self
216
+ end
217
+
218
+ def equals(c)
219
+ (c.r == @r) && (c.g == @g) && (c.b == @b)
220
+ end
221
+
222
+ def from_array(array)
223
+ @r = array[0]
224
+ @g = array[1]
225
+ @b = array[2]
226
+ self
227
+ end
228
+
229
+ def to_array(array = [], offset = 0)
230
+ array[offset] = @r
231
+ array[offset + 1] = @g
232
+ array[offset + 2] = @b
233
+ array
234
+ end
235
+
236
+ def clone
237
+ Mittsu::Color.new(@r, @g, @b)
238
+ end
239
+
240
+ private
241
+
242
+ def hue2rgb(p, q, t)
243
+ t += 1.0 if t < 0.0
244
+ t -= 1.0 if t > 1.0
245
+ return p + (q - p) * 6.0 * t if t < 1.0 / 6.0
246
+ return q if t < 1.0 / 2.0
247
+ return p + (q - p) * 6.0 * (2.0 / 3.0 - t) if t < 2.0 / 3.0
248
+ p
249
+ end
250
+
251
+ end
252
+ end