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,78 @@
1
+ require 'mittsu'
2
+
3
+ module Mittsu
4
+ class Raycaster
5
+ attr_accessor :near, :far, :ray, :params
6
+
7
+ def initialize(origin, direction, near = 0.0, far = Float::INFINITY)
8
+ @ray = Mittsu::Ray.new(origin, direction)
9
+ # direction is assumed to be normalized (for accurate distance calculations)
10
+
11
+ @precision = 0.0001
12
+ @line_precision = 1
13
+
14
+ @near, @far = near, far
15
+
16
+ @params = {
17
+ sprite: {},
18
+ mesh: {},
19
+ point_cloud: { threshold: 1 },
20
+ lod:{},
21
+ line: {}
22
+ }
23
+ end
24
+
25
+ def set(origin, direction)
26
+ # direction is assumed to be normalized (for accurate distance calculations)
27
+ @ray.set(origin, direction)
28
+ end
29
+
30
+ def set_from_camera(coords, camera)
31
+ # camera is assumed _not_ to be a child of a transformed object
32
+
33
+ if camera.is_a? Mittsu::PerspectiveCamera
34
+ @ray.origin.copy(camera.position)
35
+ @ray.direction.set(coords.x, coords.y, 0.5).unproject(camera).sub(camera.position).normalize
36
+ elsif camera.is_a? Mittsu::OrthographicCamera
37
+ @ray.origin.set(coords.x, coords.y, -1.0).unproject(camera)
38
+ @ray.direction.set(0.0, 0.0, -1.0).transform_direction(camera.matrix_world)
39
+ else
40
+ puts 'ERROR: Mittsu::Raycaster: Unsupported camera type'
41
+ end
42
+ end
43
+
44
+ def intersect_object(object, recursive)
45
+ intersects = []
46
+ intersect(object, intersects, recursive)
47
+ intersects.sort do |a, b|
48
+ a.distance <=> b.distance
49
+ end
50
+ end
51
+
52
+ def intersect_objects(objects, recursive)
53
+ intersects = []
54
+ if !objects.is_a? Array
55
+ puts 'WARNING: Mittsu::Raycaster#intersect_objects: objects is not an array'
56
+ return intersects
57
+ end
58
+
59
+ objects.each do |object|
60
+ intersect(object, intersects, recursive)
61
+ end
62
+
63
+ intersects.sort do |a, b|
64
+ a.distance <=> b.distance
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def intersect(object, intersects, recursive)
71
+ object.raycast(self, intersects)
72
+
73
+ object.children.each do |child|
74
+ intersect(chil, intersects, true)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ require 'mittsu/extras/geometries'
2
+ require 'mittsu/extras/image_utils'
3
+ require 'mittsu/extras/helpers'
@@ -0,0 +1,2 @@
1
+ require 'mittsu/extras/geometries/box_geometry'
2
+ require 'mittsu/extras/geometries/sphere_geometry'
@@ -0,0 +1,108 @@
1
+ require 'mittsu/core'
2
+ require 'mittsu/math'
3
+
4
+ module Mittsu
5
+ class BoxGeometry < Geometry
6
+ def initialize(width, height, depth, width_segments = nil, height_segments = nil, depth_segments = nil)
7
+ super()
8
+
9
+ @type = 'BoxGeometry'
10
+
11
+ @parameters = {
12
+ width: width,
13
+ height: height,
14
+ depth: depth,
15
+ width_segments: width_segments,
16
+ height_segments: height_segments,
17
+ depth_segments: depth_segments
18
+ }
19
+
20
+ @width_segments = width_segments || 1
21
+ @height_segments = height_segments || 1
22
+ @depth_segments = depth_segments || 1
23
+
24
+ width_half = width / 2.0
25
+ height_half = height / 2.0
26
+ depth_half = depth / 2.0
27
+
28
+ build_plane(:z, :y, -1, -1, depth.to_f, height.to_f, width_half, 0) # px
29
+ build_plane(:z, :y, 1, -1, depth.to_f, height.to_f, -width_half, 1) # nx
30
+ build_plane(:x, :z, 1, 1, width.to_f, depth.to_f, height_half, 2) # py
31
+ build_plane(:x, :z, 1, -1, width.to_f, depth.to_f, -height_half, 3) # ny
32
+ build_plane(:x, :y, 1, -1, width.to_f, height.to_f, depth_half, 4) # pz
33
+ build_plane(:x, :y, -1, -1, width.to_f, height.to_f, -depth_half, 5) # nz
34
+
35
+ merge_vertices
36
+ end
37
+
38
+ private
39
+
40
+ def build_plane(u, v, udir, vdir, width, height, depth, material_index)
41
+ grid_x = @width_segments
42
+ grid_y = @height_segments
43
+ width_half = width / 2.0
44
+ height_half = height / 2.0
45
+ offset = @vertices.length
46
+
47
+ if (u == :x && v == :y) || (u == :y && v == :x)
48
+ w = :z
49
+ elsif (u == :x && v == :z) || (u == :z && v == :x)
50
+ w = :y
51
+ grid_y = @depth_segments
52
+ elsif (u == :z && v == :y) || (u == :y && v == :z)
53
+ w = :x
54
+ grid_x = @depth_segments
55
+ end
56
+
57
+ grid_x1 = grid_x + 1
58
+ grid_y1 = grid_y + 1
59
+ segment_width = width / grid_x
60
+ segment_height = height / grid_y
61
+ normal = Vector3.new
62
+
63
+ normal[w] = depth > 0 ? 1.0 : -1.0
64
+
65
+ grid_y1.times do |iy|
66
+ grid_x1.times do |ix|
67
+ vector = Vector3.new
68
+ vector[u] = (ix * segment_width - width_half) * udir
69
+ vector[v] = (iy * segment_height - height_half) * vdir
70
+ vector[w] = depth
71
+
72
+ @vertices.push(vector)
73
+ end
74
+ end
75
+
76
+ grid_y.times do |iy|
77
+ grid_x.times do |ix|
78
+ a = ix + grid_x1 * iy
79
+ b = ix + grid_x1 * (iy + 1)
80
+ c = (ix + 1) + grid_x1 * (iy + 1)
81
+ d = (ix + 1) + grid_x1 * iy
82
+
83
+ uva = Vector2.new(ix / grid_x.to_f, 1.0 - iy / grid_y.to_f)
84
+ uvb = Vector2.new(ix / grid_x.to_f, 1.0 - (iy + 1.0) / grid_y.to_f)
85
+ uvc = Vector2.new((ix + 1.0) / grid_x.to_f, 1.0 - (iy + 1.0) / grid_y.to_f)
86
+ uvd = Vector2.new((ix + 1.0) / grid_x.to_f, 1.0 - iy / grid_y.to_f)
87
+
88
+ face = Face3.new(a + offset, b + offset, d + offset)
89
+ face.normal.copy(normal)
90
+ face.vertex_normals << normal.clone << normal.clone << normal.clone
91
+ face.material_index = material_index
92
+
93
+ faces << face
94
+ face_vertex_uvs[0] << [uva, uvb, uvd]
95
+
96
+ face = Face3.new(b + offset, c + offset, d + offset)
97
+ face.normal.copy(normal)
98
+ face.vertex_normals << normal.clone << normal.clone << normal.clone
99
+ face.material_index = material_index
100
+
101
+ faces << face
102
+ face_vertex_uvs[0] << [uvb.clone, uvc, uvd.clone]
103
+ end
104
+ end
105
+
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,88 @@
1
+ require 'mittsu/core'
2
+ require 'mittsu/math'
3
+
4
+ module Mittsu
5
+ class SphereGeometry < Geometry
6
+ def initialize(radius = 50.0, width_segments = 8, height_segments = 6, phi_start = 0.0, phi_length = (Math::PI * 2.0), theta_start = 0.0, theta_length = Math::PI)
7
+ super()
8
+
9
+ @type = 'SphereGeometry'
10
+
11
+ @parameters = {
12
+ radius: radius,
13
+ width_segments: width_segments,
14
+ height_segments: height_segments,
15
+ phi_start: phi_start,
16
+ phi_length: phi_length,
17
+ theta_start: theta_start,
18
+ theta_length: theta_length
19
+ }
20
+
21
+ width_segments = [3, width_segments.floor].max
22
+ height_segments = [2, height_segments].max
23
+
24
+ _vertices = []
25
+ uvs = []
26
+
27
+ (height_segments + 1).times do |y|
28
+ vertices_row = []
29
+ uvs_row = []
30
+
31
+ (width_segments + 1).times do |x|
32
+ u = x / width_segments.to_f
33
+ v = y / height_segments.to_f
34
+
35
+ vertex = Vector3.new
36
+ vertex.x = -radius * Math.cos(phi_start + u * phi_length) * Math.sin(theta_start + v * theta_length)
37
+ vertex.y = radius * Math.cos(theta_start + v * theta_length)
38
+ vertex.z = radius * Math.sin(phi_start + u * phi_length) * Math.sin(theta_start + v * theta_length)
39
+
40
+ @vertices << vertex
41
+
42
+ vertices_row << @vertices.length - 1.0
43
+ uvs_row << Vector2.new(u, 1.0 - v)
44
+ end
45
+
46
+ _vertices << vertices_row
47
+ uvs << uvs_row
48
+ end
49
+
50
+ height_segments.times do |y|
51
+ width_segments.times do |x|
52
+ v1 = _vertices[y][x + 1]
53
+ v2 = _vertices[y][x]
54
+ v3 = _vertices[y + 1][x]
55
+ v4 = _vertices[y + 1][x + 1]
56
+
57
+ n1 = @vertices[v1].clone.normalize
58
+ n2 = @vertices[v2].clone.normalize
59
+ n3 = @vertices[v3].clone.normalize
60
+ n4 = @vertices[v4].clone.normalize
61
+
62
+ uv1 = uvs[y][x + 1].clone
63
+ uv2 = uvs[y][x].clone
64
+ uv3 = uvs[y + 1][x].clone
65
+ uv4 = uvs[y + 1][x + 1].clone
66
+
67
+ if @vertices[v1].y.abs == radius
68
+ uv1.x = (uv1.x + uv2.x) / 2.0
69
+ @faces << Face3.new(v1, v3, v4, [n1, n3, n4])
70
+ @face_vertex_uvs[0] << [uv1, uv3, uv4]
71
+ elsif @vertices[v3].y == radius
72
+ uv3.x = (uv3.x + uv4.x) / 2.0
73
+ @faces << Face3.new(v1, v2, v3, [n1, n2, n3])
74
+ @face_vertex_uvs[0] << [uv1, uv2, uv3]
75
+ else
76
+ @faces << Face3.new(v1, v2, v4, [n1, n2, n4])
77
+ @face_vertex_uvs[0] << [uv1, uv2, uv4]
78
+ @faces << Face3.new(v2, v3, v4, [n2.clone, n3, n4.clone])
79
+ @face_vertex_uvs[0] << [uv2.clone, uv3, uv4.clone]
80
+ end
81
+ end
82
+ end
83
+
84
+ compute_face_normals
85
+ @bounding_sphere = Sphere.new(Vector3.new, radius)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1 @@
1
+ require 'mittsu/extras/helpers/camera_helper'
@@ -0,0 +1,155 @@
1
+ require 'mittsu/objects/line'
2
+
3
+ module Mittsu
4
+ class CameraHelper < Line
5
+ attr_accessor :point_map, :camera, :matrix, :matrix_auto_update, :material, :geometry
6
+
7
+ def initialize(camera)
8
+ @_vector = Vector3.new
9
+ @_camera = Camera.new
10
+
11
+ @geometry = Geometry.new
12
+ @material = LineBasicMaterial.new(color: 0xffffff, vertex_colors: FaceColors)
13
+
14
+ @point_map = {}
15
+
16
+ # colors
17
+
18
+ @hex_frustrum = 0xffaa00
19
+ @hex_cone = 0xff0000
20
+ @hex_up = 0x00aaff
21
+ @hex_target = 0xffffff
22
+ @hex_cross = 0x333333
23
+
24
+ # near
25
+
26
+ add_line(:n1, :n2, @hex_frustrum)
27
+ add_line(:n2, :n4, @hex_frustrum)
28
+ add_line(:n4, :n3, @hex_frustrum)
29
+ add_line(:n3, :n1, @hex_frustrum)
30
+
31
+ # far
32
+
33
+ add_line(:f1, :f2, @hex_frustrum)
34
+ add_line(:f2, :f4, @hex_frustrum)
35
+ add_line(:f4, :f3, @hex_frustrum)
36
+ add_line(:f3, :f1, @hex_frustrum)
37
+
38
+ # sides
39
+
40
+ add_line(:n1, :f1, @hex_frustrum)
41
+ add_line(:n2, :f2, @hex_frustrum)
42
+ add_line(:n3, :f3, @hex_frustrum)
43
+ add_line(:n4, :f4, @hex_frustrum)
44
+
45
+ # cone
46
+
47
+ add_line(:p, :n1, @hex_frustrum)
48
+ add_line(:p, :n2, @hex_frustrum)
49
+ add_line(:p, :n3, @hex_frustrum)
50
+ add_line(:p, :n4, @hex_frustrum)
51
+
52
+ # up
53
+
54
+ add_line(:u1, :u2, @hex_frustrum)
55
+ add_line(:u2, :u3, @hex_frustrum)
56
+ add_line(:u3, :u1, @hex_frustrum)
57
+
58
+ # target
59
+
60
+ add_line(:c, :t, @hex_frustrum)
61
+ add_line(:p, :c, @hex_frustrum)
62
+
63
+ # cross
64
+
65
+ add_line(:cn1, :cn2, @hex_frustrum)
66
+ add_line(:cn3, :cn4, @hex_frustrum)
67
+
68
+ add_line(:cf1, :cf2, @hex_frustrum)
69
+ add_line(:cf3, :cf4, @hex_frustrum)
70
+
71
+ super(@geometry, @material, LinePieces)
72
+
73
+ @camera = camera
74
+
75
+ @matrix = camera.matrix_world
76
+ @matrix_auto_update = false
77
+
78
+ update
79
+ end
80
+
81
+ def update
82
+ w = 1.0
83
+ h = 1.0
84
+
85
+ # we need just camera projection matrix
86
+ # world matrix must be identity
87
+
88
+ @_camera.projection_matrix.copy(@camera.projection_matrix)
89
+
90
+ # center / target
91
+
92
+ set_point(:c, 0.0, 0.0, -1.0)
93
+ set_point(:t, 0.0, 0.0, 1.0)
94
+
95
+ # near
96
+
97
+ set_point(:n1, -w, -h, -1.0)
98
+ set_point(:n2, w, -h, -1.0)
99
+ set_point(:n3, -w, h, -1.0)
100
+ set_point(:n4, w, h, -1.0)
101
+
102
+ # far
103
+
104
+ set_point(:f1, -w, -h, -1.0)
105
+ set_point(:f2, w, -h, -1.0)
106
+ set_point(:f3, -w, h, -1.0)
107
+ set_point(:f4, w, h, -1.0)
108
+
109
+ # up
110
+
111
+ set_point(:u1, w * 0.7, h * 1.1, -1.0)
112
+ set_point(:u2, -w * 0.7, h * 1.1, -1.0)
113
+ set_point(:u3, 0.0, h * 2.0, -1.0)
114
+
115
+ # cross
116
+
117
+ set_point(:cf1, -w, 0.0, 1.0)
118
+ set_point(:cf2, w, 0.0, 1.0)
119
+ set_point(:cf3, 0.0, -h, 1.0)
120
+ set_point(:cf4, 0.0, h, 1.0)
121
+
122
+ set_point(:cn1, -w, 0.0, 1.0)
123
+ set_point(:cn2, w, 0.0, 1.0)
124
+ set_point(:cn3, 0.0, -h, 1.0)
125
+ set_point(:cn4, 0.0, h, 1.0)
126
+
127
+ @geometry.vertices_need_update = true
128
+ end
129
+
130
+ private
131
+
132
+ def add_line(a, b, hex)
133
+ add_point(a, hex)
134
+ add_point(b, hex)
135
+ end
136
+
137
+ def add_point(id, hex)
138
+ @geometry.vertices << Vector3.new
139
+ @geometry.colors << Color.new(hex)
140
+
141
+ @point_map[id] ||= []
142
+ @point_map[id] << geometry.vertices.length - 1
143
+ end
144
+
145
+ def set_point(point, x, y, z)
146
+ @_vector.set(x, y, z).unproject(@_camera)
147
+
148
+ points = @point_map[point]
149
+
150
+ if !points.nil?
151
+ points.each { |p| @geometry.vertices[p].copy(@_vector) }
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,3 @@
1
+ module Mittsu
2
+ class Image < Struct.new(:width, :height, :data); end
3
+ end