mittsu 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +7 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/examples/01_-_Default1noCulling.png +0 -0
- data/examples/01_scene_example.rb +14 -0
- data/examples/02_box_mesh_example.rb +30 -0
- data/examples/02_sphere_mesh_example.rb +30 -0
- data/examples/03_complex_object_example.rb +52 -0
- data/examples/04_ambient_light_example.rb +33 -0
- data/examples/04_dir_light_example.rb +36 -0
- data/examples/04_hemi_light_example.rb +30 -0
- data/examples/04_point_light_example.rb +50 -0
- data/examples/04_spot_light_example.rb +44 -0
- data/examples/05_earth_example.rb +42 -0
- data/examples/05_earth_moon_example.rb +46 -0
- data/examples/05_texture_example.rb +32 -0
- data/examples/06_cube_texture_example.rb +36 -0
- data/examples/06_skybox_example.rb +60 -0
- data/examples/07_earth_normal_example.rb +36 -0
- data/examples/08_shadow_example.rb +87 -0
- data/examples/09_line_example.rb +52 -0
- data/examples/10_obj_loader_example.rb +68 -0
- data/examples/11_character_input_example.rb +18 -0
- data/examples/11_continuous_keyboard_input_example.rb +35 -0
- data/examples/11_keyboard_input_example.rb +43 -0
- data/examples/12_mouse_click_example.rb +38 -0
- data/examples/12_mouse_motion_example.rb +35 -0
- data/examples/12_mouse_scroll_example.rb +36 -0
- data/examples/12_orbit_zoom_example.rb +68 -0
- data/examples/13_joystick_example.rb +80 -0
- data/examples/cubemap/tron_bk.png +0 -0
- data/examples/cubemap/tron_dn.png +0 -0
- data/examples/cubemap/tron_ft.png +0 -0
- data/examples/cubemap/tron_lf.png +0 -0
- data/examples/cubemap/tron_rt.png +0 -0
- data/examples/cubemap/tron_up.png +0 -0
- data/examples/earth.png +0 -0
- data/examples/earth_normal.png +0 -0
- data/examples/example_helper.rb +2 -0
- data/examples/male-02-1noCulling.png +0 -0
- data/examples/male02.mtl +54 -0
- data/examples/male02.obj +13888 -0
- data/examples/moon.png +0 -0
- data/examples/orig_02_-_Defaul1noCulling.png +0 -0
- data/examples/texture.png +0 -0
- data/lib/mittsu.rb +15 -0
- data/lib/mittsu/cameras.rb +4 -0
- data/lib/mittsu/cameras/camera.rb +34 -0
- data/lib/mittsu/cameras/cube_camera.rb +74 -0
- data/lib/mittsu/cameras/orthographic_camera.rb +53 -0
- data/lib/mittsu/cameras/perspective_camera.rb +115 -0
- data/lib/mittsu/constants.rb +160 -0
- data/lib/mittsu/core.rb +10 -0
- data/lib/mittsu/core/buffer_attribute.rb +87 -0
- data/lib/mittsu/core/buffer_geometry.rb +694 -0
- data/lib/mittsu/core/clock.rb +44 -0
- data/lib/mittsu/core/dynamic_buffer_attribute.rb +16 -0
- data/lib/mittsu/core/event_dispatcher.rb +39 -0
- data/lib/mittsu/core/face3.rb +30 -0
- data/lib/mittsu/core/geometry.rb +596 -0
- data/lib/mittsu/core/hash_array.rb +36 -0
- data/lib/mittsu/core/hash_object.rb +19 -0
- data/lib/mittsu/core/object_3d.rb +421 -0
- data/lib/mittsu/core/raycaster.rb +78 -0
- data/lib/mittsu/extras.rb +3 -0
- data/lib/mittsu/extras/geometries.rb +2 -0
- data/lib/mittsu/extras/geometries/box_geometry.rb +108 -0
- data/lib/mittsu/extras/geometries/sphere_geometry.rb +88 -0
- data/lib/mittsu/extras/helpers.rb +1 -0
- data/lib/mittsu/extras/helpers/camera_helper.rb +155 -0
- data/lib/mittsu/extras/image.rb +3 -0
- data/lib/mittsu/extras/image_utils.rb +80 -0
- data/lib/mittsu/lights.rb +7 -0
- data/lib/mittsu/lights/ambient_light.rb +16 -0
- data/lib/mittsu/lights/area_light.rb +24 -0
- data/lib/mittsu/lights/directional_light.rb +131 -0
- data/lib/mittsu/lights/hemisphere_light.rb +29 -0
- data/lib/mittsu/lights/light.rb +21 -0
- data/lib/mittsu/lights/point_light.rb +27 -0
- data/lib/mittsu/lights/spot_light.rb +104 -0
- data/lib/mittsu/loaders.rb +7 -0
- data/lib/mittsu/loaders/cache.rb +53 -0
- data/lib/mittsu/loaders/file_loader.rb +22 -0
- data/lib/mittsu/loaders/image_loader.rb +32 -0
- data/lib/mittsu/loaders/loader.rb +212 -0
- data/lib/mittsu/loaders/loading_manager.rb +17 -0
- data/lib/mittsu/loaders/mtl_loader.rb +242 -0
- data/lib/mittsu/loaders/obj_mtl_loader.rb +225 -0
- data/lib/mittsu/materials.rb +7 -0
- data/lib/mittsu/materials/line_basic_material.rb +39 -0
- data/lib/mittsu/materials/material.rb +156 -0
- data/lib/mittsu/materials/mesh_basic_material.rb +122 -0
- data/lib/mittsu/materials/mesh_face_material.rb +30 -0
- data/lib/mittsu/materials/mesh_lambert_material.rb +126 -0
- data/lib/mittsu/materials/mesh_phong_material.rb +152 -0
- data/lib/mittsu/materials/shader_material.rb +108 -0
- data/lib/mittsu/math.rb +105 -0
- data/lib/mittsu/math/box2.rb +135 -0
- data/lib/mittsu/math/box3.rb +194 -0
- data/lib/mittsu/math/color.rb +252 -0
- data/lib/mittsu/math/color_keywords.rb +151 -0
- data/lib/mittsu/math/euler.rb +182 -0
- data/lib/mittsu/math/frustum.rb +106 -0
- data/lib/mittsu/math/line3.rb +76 -0
- data/lib/mittsu/math/matrix3.rb +163 -0
- data/lib/mittsu/math/matrix4.rb +581 -0
- data/lib/mittsu/math/plane.rb +128 -0
- data/lib/mittsu/math/quaternion.rb +309 -0
- data/lib/mittsu/math/ray.rb +292 -0
- data/lib/mittsu/math/sphere.rb +91 -0
- data/lib/mittsu/math/spline.rb +128 -0
- data/lib/mittsu/math/triangle.rb +121 -0
- data/lib/mittsu/math/vector2.rb +238 -0
- data/lib/mittsu/math/vector3.rb +491 -0
- data/lib/mittsu/math/vector4.rb +414 -0
- data/lib/mittsu/objects.rb +3 -0
- data/lib/mittsu/objects/group.rb +8 -0
- data/lib/mittsu/objects/line.rb +143 -0
- data/lib/mittsu/objects/mesh.rb +243 -0
- data/lib/mittsu/renderers.rb +1 -0
- data/lib/mittsu/renderers/glfw_window.rb +216 -0
- data/lib/mittsu/renderers/opengl/opengl_debug.rb +38 -0
- data/lib/mittsu/renderers/opengl/opengl_program.rb +402 -0
- data/lib/mittsu/renderers/opengl/opengl_shader.rb +58 -0
- data/lib/mittsu/renderers/opengl/opengl_state.rb +207 -0
- data/lib/mittsu/renderers/opengl/plugins/shadow_map_plugin.rb +416 -0
- data/lib/mittsu/renderers/opengl_render_target.rb +87 -0
- data/lib/mittsu/renderers/opengl_renderer.rb +3376 -0
- data/lib/mittsu/renderers/shaders/shader_chunk.rb +12 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/alphamap_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/alphamap_pars_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/alphatest_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/bumpmap_pars_fragment.glsl +40 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/color_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/color_pars_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/color_pars_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/color_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/common.glsl +60 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/default_vertex.glsl +15 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/defaultnormal_vertex.glsl +21 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/envmap_fragment.glsl +62 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/envmap_pars_fragment.glsl +21 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/envmap_pars_vertex.glsl +7 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/envmap_vertex.glsl +17 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/fog_fragment.glsl +26 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/fog_pars_fragment.glsl +15 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_pars_fragment.glsl +6 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_pars_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_lambert_pars_vertex.glsl +43 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_lambert_vertex.glsl +196 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_fragment.glsl +243 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_pars_fragment.glsl +58 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_pars_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/linear_to_gamma_fragment.glsl +2 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_pars_fragment.glsl +12 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_pars_vertex.glsl +11 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_vertex.glsl +15 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_fragment.glsl +9 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_pars_fragment.glsl +11 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_pars_vertex.glsl +6 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_particle_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_particle_pars_fragment.glsl +6 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/morphnormal_vertex.glsl +12 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/morphtarget_pars_vertex.glsl +13 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/morphtarget_vertex.glsl +20 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/normalmap_pars_fragment.glsl +27 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_fragment.glsl +217 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_pars_fragment.glsl +19 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_pars_vertex.glsl +6 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_vertex.glsl +9 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/skinbase_vertex.glsl +8 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/skinning_pars_vertex.glsl +47 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/skinning_vertex.glsl +20 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/skinnormal_vertex.glsl +20 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/specularmap_fragment.glsl +12 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/specularmap_pars_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/worldpos_vertex.glsl +17 -0
- data/lib/mittsu/renderers/shaders/shader_lib.rb +420 -0
- data/lib/mittsu/renderers/shaders/uniforms_lib.rb +107 -0
- data/lib/mittsu/renderers/shaders/uniforms_utils.rb +31 -0
- data/lib/mittsu/scenes.rb +1 -0
- data/lib/mittsu/scenes/scene.rb +27 -0
- data/lib/mittsu/textures.rb +5 -0
- data/lib/mittsu/textures/compressed_texture.rb +30 -0
- data/lib/mittsu/textures/cube_texture.rb +19 -0
- data/lib/mittsu/textures/data_texture.rb +17 -0
- data/lib/mittsu/textures/texture.rb +92 -0
- data/lib/mittsu/textures/video_texture.rb +17 -0
- data/lib/mittsu/version.rb +4 -0
- data/mittsu.gemspec +31 -0
- metadata +357 -0
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'mittsu/math'
|
2
|
+
|
3
|
+
module Mittsu
|
4
|
+
class Sphere
|
5
|
+
attr_accessor :center, :radius
|
6
|
+
|
7
|
+
def initialize(center = Mittsu::Vector3.new, radius = 0.0)
|
8
|
+
@center, @radius = center, radius.to_f
|
9
|
+
end
|
10
|
+
|
11
|
+
def set(center, radius)
|
12
|
+
@center.copy(center)
|
13
|
+
@radius = radius.to_f
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def set_from_points(points, optional_center = nil)
|
18
|
+
box = Mittsu::Box3.new
|
19
|
+
c = @center
|
20
|
+
if optional_center.nil?
|
21
|
+
box.set_from_points(points).center(c)
|
22
|
+
else
|
23
|
+
c.copy(optional_center)
|
24
|
+
end
|
25
|
+
max_radius_sq = 0.0
|
26
|
+
points.each do |point|
|
27
|
+
max_radius_sq = [max_radius_sq, c.distance_to_squared(point)].max
|
28
|
+
end
|
29
|
+
@radius = Math.sqrt(max_radius_sq)
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
def copy(sphere)
|
34
|
+
@center.copy(sphere.center)
|
35
|
+
@radius = sphere.radius
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def empty
|
40
|
+
@radius <= 0
|
41
|
+
end
|
42
|
+
|
43
|
+
def contains_point?(point)
|
44
|
+
point.distance_to_squared(@center) <= @radius * @radius
|
45
|
+
end
|
46
|
+
|
47
|
+
def distance_to_point(point)
|
48
|
+
point.distance_to(@center) - @radius
|
49
|
+
end
|
50
|
+
|
51
|
+
def intersects_sphere?(sphere)
|
52
|
+
radiusSum = @radius + sphere.radius
|
53
|
+
sphere.center.distance_to_squared(@center) <= radiusSum * radiusSum
|
54
|
+
end
|
55
|
+
|
56
|
+
def clamp_point(point, target = Mittsu::Vector3.new)
|
57
|
+
delta_length_sq = @center.distance_to_squared(point)
|
58
|
+
target.copy(point)
|
59
|
+
if delta_length_sq > (@radius * @radius)
|
60
|
+
target.sub(@center).normalize
|
61
|
+
target.multiply_scalar(@radius).add(@center)
|
62
|
+
end
|
63
|
+
target
|
64
|
+
end
|
65
|
+
|
66
|
+
def bounding_box(target = Mittsu::Box3.new)
|
67
|
+
target.set(@center, @center)
|
68
|
+
target.expand_by_scalar(@radius)
|
69
|
+
target
|
70
|
+
end
|
71
|
+
|
72
|
+
def apply_matrix4(matrix)
|
73
|
+
@center.apply_matrix4(matrix)
|
74
|
+
@radius = @radius * matrix.max_scale_on_axis
|
75
|
+
self
|
76
|
+
end
|
77
|
+
|
78
|
+
def translate(offset)
|
79
|
+
@center.add(offset)
|
80
|
+
self
|
81
|
+
end
|
82
|
+
|
83
|
+
def ==(sphere)
|
84
|
+
sphere.center == (@center) && sphere.radius == @radius
|
85
|
+
end
|
86
|
+
|
87
|
+
def clone
|
88
|
+
Mittsu::Sphere.new.copy(self)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'mittsu/math'
|
2
|
+
|
3
|
+
module Mittsu
|
4
|
+
class Spline
|
5
|
+
Point = Struct.new(:x, :y, :z)
|
6
|
+
Length = Struct.new(:chunks, :total)
|
7
|
+
|
8
|
+
attr_accessor :points
|
9
|
+
|
10
|
+
def initialize(points)
|
11
|
+
@points = points
|
12
|
+
@c = []
|
13
|
+
@v3 = Point.new(0.0, 0.0, 0.0)
|
14
|
+
end
|
15
|
+
|
16
|
+
def init_from_array(a)
|
17
|
+
@points = a.map do |p|
|
18
|
+
Point.new(*p.take(3))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def point(k)
|
23
|
+
point = (@points.length - 1) * k
|
24
|
+
int_point = point.floor
|
25
|
+
weight = point - int_point
|
26
|
+
|
27
|
+
@c[0] = int_point.zero? ? int_point : int_point - 1
|
28
|
+
@c[1] = int_point
|
29
|
+
@c[2] = int_point > @points.length - 1 ? @points.length - 1 : int_point + 1
|
30
|
+
@c[3] = int_point > @points.length - 3 ? @points.length - 1 : int_point + 2
|
31
|
+
|
32
|
+
pa = @points[c[0]]
|
33
|
+
pb = @points[c[1]]
|
34
|
+
pc = @points[c[2]]
|
35
|
+
pd = @points[c[3]]
|
36
|
+
|
37
|
+
w2 = weight * weight
|
38
|
+
w3 = weight * w2
|
39
|
+
|
40
|
+
v3.x = interpolate(pa.x, pb.x, pc.x, pd.x, weight, w2, w3)
|
41
|
+
v3.y = interpolate(pa.y, pb.y, pc.y, pd.y, weight, w2, w3)
|
42
|
+
v3.z = interpolate(pa.z, pb.z, pc.z, pd.z, weight, w2, w3)
|
43
|
+
|
44
|
+
v3
|
45
|
+
end
|
46
|
+
|
47
|
+
def control_points_array
|
48
|
+
@points.map do |p|
|
49
|
+
[ p.x, p.y, p.z ]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def length(n_sub_divisions = 100)
|
54
|
+
point, int_point, old_int_point = 0, 0, 0
|
55
|
+
old_position = Mittsu::Vector3.new
|
56
|
+
tmp_vec = Mittsu::Vector3.new
|
57
|
+
chunk_lengths = []
|
58
|
+
total_length = 0
|
59
|
+
|
60
|
+
# first point has 0 length
|
61
|
+
chunk_lengths << 0
|
62
|
+
n_samples = @points.length * n_sub_divisions
|
63
|
+
old_position.copy(@points.first)
|
64
|
+
|
65
|
+
(1...n_samples).each do |i|
|
66
|
+
index = i.to_f / n_samples.to_f
|
67
|
+
|
68
|
+
position = self.point(index)
|
69
|
+
tmp_vec.copy(position)
|
70
|
+
|
71
|
+
total_length += tmp_vec.distance_to(old_position)
|
72
|
+
|
73
|
+
old_position.copy(position)
|
74
|
+
|
75
|
+
point = (@points.length - 1) * index
|
76
|
+
int_point = point.floor
|
77
|
+
|
78
|
+
if (int_point != old_int_point)
|
79
|
+
chunk_lengths[int_point] = total_length
|
80
|
+
old_int_point = int_point
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# last point ends with total length
|
85
|
+
chunk_lengths << total_length
|
86
|
+
Length.new(chunk_lengths, total_length)
|
87
|
+
end
|
88
|
+
|
89
|
+
def reparametrize_by_arc_length(sampling_coef)
|
90
|
+
new_points = []
|
91
|
+
tmp_vec = Mittsu::Vector3.new
|
92
|
+
sl = self.length
|
93
|
+
|
94
|
+
new_points << tmp_vec.copy(@points[0]).clone
|
95
|
+
|
96
|
+
@points.each_with_index do |p,i|
|
97
|
+
#tmp_vec.copy(@points[i-1])
|
98
|
+
#linear_distance = tmp_vec.distance_to(p)
|
99
|
+
|
100
|
+
real_distance = sl.chunks[i] - sl.chunks[i - 1]
|
101
|
+
|
102
|
+
sampling = (sampling_coef * real_distance / sl.total).ceil
|
103
|
+
|
104
|
+
index_current = (i.to_f - 1.0) / (@points.length.to_f - 1.0)
|
105
|
+
index_next = i.to_f / (@points.length.to_f - 1.0)
|
106
|
+
|
107
|
+
(1...sampling-1).each do |j|
|
108
|
+
index = index_current + j * (1.0 / sampling) * (index_next - index_current)
|
109
|
+
|
110
|
+
position = self.point(index)
|
111
|
+
new_points << tmp_vec.copy(position).clone
|
112
|
+
end
|
113
|
+
|
114
|
+
new_points << tmp_vec.copy(p).clone
|
115
|
+
end
|
116
|
+
@points = new_points
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def interpolate(p0, p1, p2, p3, t, t2, t3)
|
122
|
+
v0 = (p2 - p0) * 0.5
|
123
|
+
v1 = (p4 - p1) * 0.5
|
124
|
+
|
125
|
+
(2.0 * (p1 - p2) + v0 + v1) * t3 + (-3.0 * (p1 - p2) - 2.0 * v0 - v1) * t2 + v0 * t + p1?
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'mittsu/math'
|
2
|
+
|
3
|
+
module Mittsu
|
4
|
+
class Triangle
|
5
|
+
attr_accessor :a, :b, :c
|
6
|
+
|
7
|
+
def initialize(a = Mittsu::Vector3.new, b = Mittsu::Vector3.new, c = Mittsu::Vector3.new)
|
8
|
+
@a, @b, @c = a, b, c
|
9
|
+
end
|
10
|
+
|
11
|
+
def set(a, b, c)
|
12
|
+
@a.copy(a)
|
13
|
+
@b.copy(b)
|
14
|
+
@c.copy(c)
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def set_from_points_and_indices(points, i0, i1, i2)
|
19
|
+
@a.copy(points[i0])
|
20
|
+
@b.copy(points[i1])
|
21
|
+
@c.copy(points[i2])
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def copy(triangle)
|
26
|
+
@a.copy(triangle.a)
|
27
|
+
@b.copy(triangle.b)
|
28
|
+
@c.copy(triangle.c)
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def area
|
33
|
+
v0 = Mittsu::Vector3.new
|
34
|
+
v1 = Mittsu::Vector3.new
|
35
|
+
v0.sub_vectors(@c, @b)
|
36
|
+
v1.sub_vectors(@a, @b)
|
37
|
+
v0.cross(v1).length * 0.5
|
38
|
+
end
|
39
|
+
|
40
|
+
def midpoint(target = Mittsu::Vector3.new)
|
41
|
+
target.add_vectors(@a, @b).add(@c).multiply_scalar(1.0 / 3.0)
|
42
|
+
end
|
43
|
+
|
44
|
+
def normal(target = Mittsu::Vector3.new)
|
45
|
+
Mittsu::Triangle.normal(@a, @b, @c, target)
|
46
|
+
end
|
47
|
+
|
48
|
+
def plane(target = Mittsu::Plane.new)
|
49
|
+
target.set_from_coplanar_points(@a, @b, @c)
|
50
|
+
end
|
51
|
+
|
52
|
+
def barycoord_from_point(point, target = Mittsu::Vector3.new)
|
53
|
+
Mittsu::Triangle.barycoord_from_point(point, @a, @b, @c, target)
|
54
|
+
end
|
55
|
+
|
56
|
+
def contains_point?(point)
|
57
|
+
Mittsu::Triangle.contains_point?(point, @a, @b, @c)
|
58
|
+
end
|
59
|
+
|
60
|
+
def equals(triangle)
|
61
|
+
triangle.a.equals(@a) && triangle.b.equals(@b) && triangle.c.equals(@c)
|
62
|
+
end
|
63
|
+
|
64
|
+
def clone
|
65
|
+
Mittsu::Triangle.new.copy(self)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.normal(a, b, c, target = Mittsu::Vector3.new)
|
69
|
+
v0 = Mittsu::Vector3.new
|
70
|
+
target.sub_vectors(c, b)
|
71
|
+
v0.sub_vectors(a, b)
|
72
|
+
target.cross(v0)
|
73
|
+
|
74
|
+
result_length_sq = target.length_sq
|
75
|
+
if (result_length_sq > 0)
|
76
|
+
target.multiply_scalar(1.0 / Math.sqrt(result_length_sq))
|
77
|
+
else
|
78
|
+
target.set(0.0, 0.0, 0.0)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# static/instance method to calculate barycoordinates
|
83
|
+
# based on: http://www.blackpawn.com/texts/pointinpoly/default.html
|
84
|
+
def self.barycoord_from_point(point, a, b, c, target = Mittsu::Vector3.new)
|
85
|
+
v0 = Mittsu::Vector3.new
|
86
|
+
v1 = Mittsu::Vector3.new
|
87
|
+
v2 = Mittsu::Vector3.new
|
88
|
+
|
89
|
+
v0.sub_vectors(c, a)
|
90
|
+
v1.sub_vectors(b, a)
|
91
|
+
v2.sub_vectors(point, a)
|
92
|
+
|
93
|
+
dot00 = v0.dot(v0)
|
94
|
+
dot01 = v0.dot(v1)
|
95
|
+
dot02 = v0.dot(v2)
|
96
|
+
dot11 = v1.dot(v1)
|
97
|
+
dot12 = v1.dot(v2)
|
98
|
+
|
99
|
+
denom = dot00 * dot11 - dot01 * dot01
|
100
|
+
|
101
|
+
# colinear or singular triangle
|
102
|
+
if denom.zero?
|
103
|
+
# arbitrary location outside of triangle?
|
104
|
+
# not sure if this is the best idea, maybe should be returning undefined
|
105
|
+
return target.set(-2.0, -1.0, -1.0)
|
106
|
+
end
|
107
|
+
|
108
|
+
inv_denom = 1.0 / denom
|
109
|
+
u = (dot11 * dot02 - dot01 * dot12) * inv_denom
|
110
|
+
v = (dot00 * dot12 - dot01 * dot02) * inv_denom
|
111
|
+
|
112
|
+
target.set(1.0 - u - v, v, u)
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.contains_point?(point, a, b, c)
|
116
|
+
v1 = Mittsu::Vector3.new
|
117
|
+
result = Mittsu::Triangle.barycoord_from_point(point, a, b, c, v1)
|
118
|
+
result.x >= 0 && result.y >= 0 && result.x + result.y <= 1
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,238 @@
|
|
1
|
+
require 'mittsu/math'
|
2
|
+
|
3
|
+
module Mittsu
|
4
|
+
class Vector2
|
5
|
+
attr_reader :x, :y
|
6
|
+
|
7
|
+
def initialize(x = 0, y = 0)
|
8
|
+
@x = x.to_f
|
9
|
+
@y = y.to_f
|
10
|
+
end
|
11
|
+
|
12
|
+
def x=(value)
|
13
|
+
@x = value.to_f
|
14
|
+
end
|
15
|
+
|
16
|
+
def y=(value)
|
17
|
+
@y = value.to_f
|
18
|
+
end
|
19
|
+
|
20
|
+
def set(x, y)
|
21
|
+
@x = x.to_f
|
22
|
+
@y = y.to_f
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def []=(index, value)
|
27
|
+
return @x = value.to_f if index == 0
|
28
|
+
return @y = value.to_f if index == 1
|
29
|
+
raise IndexError
|
30
|
+
end
|
31
|
+
|
32
|
+
def [](index)
|
33
|
+
return @x if index == 0
|
34
|
+
return @y if index == 1
|
35
|
+
raise IndexError
|
36
|
+
end
|
37
|
+
|
38
|
+
def copy(v)
|
39
|
+
@x = v.x
|
40
|
+
@y = v.y
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def add(v)
|
45
|
+
@x += v.x
|
46
|
+
@y += v.y
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
def add_scalar(s)
|
51
|
+
@x += s
|
52
|
+
@y += s
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def add_vectors(a, b)
|
57
|
+
@x = a.x + b.x
|
58
|
+
@y = a.y + b.y
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
def sub(v)
|
63
|
+
@x -= v.x
|
64
|
+
@y -= v.y
|
65
|
+
self
|
66
|
+
end
|
67
|
+
|
68
|
+
def sub_scalar(s)
|
69
|
+
@x -= s
|
70
|
+
@y -= s
|
71
|
+
self
|
72
|
+
end
|
73
|
+
|
74
|
+
def sub_vectors(a, b)
|
75
|
+
@x = a.x - b.x
|
76
|
+
@y = a.y - b.y
|
77
|
+
self
|
78
|
+
end
|
79
|
+
|
80
|
+
def multiply(v)
|
81
|
+
@x *= v.x
|
82
|
+
@y *= v.y
|
83
|
+
self
|
84
|
+
end
|
85
|
+
|
86
|
+
def multiply_scalar(s)
|
87
|
+
@x *= s
|
88
|
+
@y *= s
|
89
|
+
self
|
90
|
+
end
|
91
|
+
|
92
|
+
def divide(v)
|
93
|
+
@x /= v.x
|
94
|
+
@y /= v.y
|
95
|
+
self
|
96
|
+
end
|
97
|
+
|
98
|
+
def divide_scalar(s)
|
99
|
+
@x /= s
|
100
|
+
@y /= s
|
101
|
+
self
|
102
|
+
end
|
103
|
+
|
104
|
+
def min(v)
|
105
|
+
@x = [@x, v.x].min
|
106
|
+
@y = [@y, v.y].min
|
107
|
+
self
|
108
|
+
end
|
109
|
+
|
110
|
+
def max(v)
|
111
|
+
@x = [@x, v.x].max
|
112
|
+
@y = [@y, v.y].max
|
113
|
+
self
|
114
|
+
end
|
115
|
+
|
116
|
+
def clamp(min, max)
|
117
|
+
@x = Math.clamp(@x, min.x, max.x)
|
118
|
+
@y = Math.clamp(@y, min.y, max.y)
|
119
|
+
self
|
120
|
+
end
|
121
|
+
|
122
|
+
def clamp_scalar(min, max)
|
123
|
+
min, max = min.to_f, max.to_f
|
124
|
+
@x = Math.clamp(@x, min, max)
|
125
|
+
@y = Math.clamp(@y, min, max)
|
126
|
+
self
|
127
|
+
end
|
128
|
+
|
129
|
+
def floor
|
130
|
+
@x = @x.floor.to_f
|
131
|
+
@y = @y.floor.to_f
|
132
|
+
self
|
133
|
+
end
|
134
|
+
|
135
|
+
def ceil
|
136
|
+
@x = @x.ceil.to_f
|
137
|
+
@y = @y.ceil.to_f
|
138
|
+
self
|
139
|
+
end
|
140
|
+
|
141
|
+
def round
|
142
|
+
@x = @x.round.to_f
|
143
|
+
@y = @y.round.to_f
|
144
|
+
self
|
145
|
+
end
|
146
|
+
|
147
|
+
def round_to_zero
|
148
|
+
@x = ( @x < 0 ) ? @x.ceil.to_f : @x.floor.to_f
|
149
|
+
@y = ( @y < 0 ) ? @y.ceil.to_f : @y.floor.to_f
|
150
|
+
self
|
151
|
+
end
|
152
|
+
|
153
|
+
def negate
|
154
|
+
@x = -@x
|
155
|
+
@y = -@y
|
156
|
+
self
|
157
|
+
end
|
158
|
+
|
159
|
+
def dot(v)
|
160
|
+
@x * v.x + @y * v.y
|
161
|
+
end
|
162
|
+
|
163
|
+
def length
|
164
|
+
Math.sqrt(length_sq)
|
165
|
+
end
|
166
|
+
|
167
|
+
def length_sq
|
168
|
+
self.dot(self)
|
169
|
+
end
|
170
|
+
|
171
|
+
def normalize
|
172
|
+
self.divide_scalar(self.length)
|
173
|
+
end
|
174
|
+
|
175
|
+
def distance_to(v)
|
176
|
+
Math.sqrt(distance_to_squared(v))
|
177
|
+
end
|
178
|
+
|
179
|
+
def distance_to_squared(v)
|
180
|
+
dx, dy = @x - v.x, @y - v.y
|
181
|
+
dx * dx + dy * dy
|
182
|
+
end
|
183
|
+
|
184
|
+
def set_length(l)
|
185
|
+
old_length = self.length
|
186
|
+
if old_length != 0 && l != old_length
|
187
|
+
self.multiply_scalar(l / old_length)
|
188
|
+
end
|
189
|
+
self
|
190
|
+
end
|
191
|
+
|
192
|
+
def lerp(v, alpha)
|
193
|
+
@x += (v.x - @x) * alpha
|
194
|
+
@y += (v.y - @y) * alpha
|
195
|
+
self
|
196
|
+
end
|
197
|
+
|
198
|
+
def lerp_vectors(v1, v2, alpha)
|
199
|
+
self.sub_vectors(v2, v1).multiply_scalar(alpha).add(v1);
|
200
|
+
end
|
201
|
+
|
202
|
+
def ==(v)
|
203
|
+
v.x == @x && v.y == @y
|
204
|
+
end
|
205
|
+
|
206
|
+
def from_array(array, offset = 0)
|
207
|
+
@x = array[offset]
|
208
|
+
@y = array[offset + 1]
|
209
|
+
self
|
210
|
+
end
|
211
|
+
|
212
|
+
def to_array(array = [], offset = 0)
|
213
|
+
array.tap { |a|
|
214
|
+
a[offset] = @x;
|
215
|
+
a[offset + 1] = @y;
|
216
|
+
}
|
217
|
+
end
|
218
|
+
|
219
|
+
def to_a
|
220
|
+
self.to_array
|
221
|
+
end
|
222
|
+
|
223
|
+
def from_attribute(attribute, index, offset = 0)
|
224
|
+
index = index * attribute.item_size + offset
|
225
|
+
@x = attribute.array[index]
|
226
|
+
@y = attribute.array[index + 1]
|
227
|
+
self
|
228
|
+
end
|
229
|
+
|
230
|
+
def clone
|
231
|
+
Vector2.new(@x, @y)
|
232
|
+
end
|
233
|
+
|
234
|
+
def to_s
|
235
|
+
"[#{x}, #{y}]"
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|