Class: Mittsu::Line

Inherits:
Object3D show all
Defined in:
lib/mittsu/objects/line.rb

Direct Known Subclasses

CameraHelper

Constant Summary

Constants inherited from Object3D

Object3D::DefaultUp

Instance Attribute Summary collapse

Attributes inherited from Object3D

#cast_shadow, #children, #frustum_culled, #id, #matrix, #matrix_auto_update, #matrix_world, #matrix_world_needs_update, #name, #parent, #position, #quaternion, #receive_shadow, #render_order, #rotation, #rotation_auto_update, #scale, #up, #user_data, #uuid, #visible

Instance Method Summary collapse

Methods inherited from Object3D

#add, #apply_matrix, #get_object_by_id, #get_object_by_name, #get_object_by_property, #get_world_direction, #get_world_position, #get_world_quaternion, #get_world_rotation, #get_world_scale, #local_to_world, #look_at, #print_tree, #remove, #rotate_on_axis, #rotate_x, #rotate_y, #rotate_z, #set_rotation_from_axis_angle, #set_rotation_from_euler, #set_rotation_from_matrix, #set_rotation_from_quaternion, #to_json, #to_s, #translate_on_axis, #translate_x, #translate_y, #translate_z, #traverse, #traverse_ancestors, #traverse_visible, #update_matrix, #update_matrix_world, #world_to_local

Methods included from EventDispatcher

#add_event_listener, #dispatch_event, #has_event_listener, #remove_event_listener

Constructor Details

#initialize(geometry = nil, material = nil, mode = nil) ⇒ Line

Returns a new instance of Line.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mittsu/objects/line.rb', line 5

def initialize(geometry = nil, material = nil, mode = nil)
  super()

  @type = 'Line'

  @geometry = geometry || Geometry.new
  @material = material || LineBasicMaterial.new(color: (rand * 0xffffff).to_i)

  @mode = mode || LineStrip

  @_inverse_matrix = Matrix4.new
  @_ray = Ray.new
  @_sphere = Sphere.new
end

Instance Attribute Details

#geometryObject

Returns the value of attribute geometry.



3
4
5
# File 'lib/mittsu/objects/line.rb', line 3

def geometry
  @geometry
end

#materialObject

Returns the value of attribute material.



3
4
5
# File 'lib/mittsu/objects/line.rb', line 3

def material
  @material
end

#modeObject

Returns the value of attribute mode.



3
4
5
# File 'lib/mittsu/objects/line.rb', line 3

def mode
  @mode
end

#morph_target_baseObject

Returns the value of attribute morph_target_base.



3
4
5
# File 'lib/mittsu/objects/line.rb', line 3

def morph_target_base
  @morph_target_base
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/mittsu/objects/line.rb', line 3

def type
  @type
end

Instance Method Details

#clone(object = Line.new(@geometry, @material, @mode)) ⇒ Object



137
138
139
140
# File 'lib/mittsu/objects/line.rb', line 137

def clone(object = Line.new(@geometry, @material, @mode))
  super(object)
  object
end

#raycast(raycaster, intersects) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/mittsu/objects/line.rb', line 20

def raycast(raycaster, intersects)
  precision = raycaster.line_precision
  precision_sq = precision * precision

  @geometry.compute_bounding_sphere if @geometry.bounding_sphere.nil?

  # Checking bounding_sphere distance to ray

  @_sphere.copy(geometry.bounding_sphere)
  @_sphere.apply_matrix4(@matrix_world)

  return unless raycaster.ray.intersetion_sphere?(sphere)

  @_inverse_matrix.get_inverse(@matrix_world)
  @_ray.copy(raycaster.ray).apply_matrix4(@_inverse_matrix)

  v_start = Vector3.new
  v_end = Vector3.new
  inter_segment = Vector3.new
  inter_ray = Vector3.new
  step = @mode == LineStrip ? 1 : 2

  if geometry.is_a?(BufferGeometry)
    attributes = @geometry.attributes

    if !attributes.index.nil?
      indices = attributes.index.array
      positions = attributes.position.array
      offsets = geometry.offsets

      if offsets.empty?
        offsets = [{ start: 0, count: indices.length, index: 0 }]
      end

      offsets.each_with_index do |offset, oi|
        start = offset[:start]
        count = offset[:count]

        (start...(count-1)).step(step).each do |i|
          v_start.from_array(positions, a * 3)
          v_end.from_array(positions, b * 3)

          dist_sq = @_ray.distance_sq_to_segment(v_start, v_end, inter_ray, inter_segment)

          next if dist_sq > precision_sq

          distance = @_ray.origin.distance_to(inter_ray)

          next if distance < raycaster.near || distance > raycaster.far

          intersects << {
            distance: distance,
            # What to we want? intersection point on the ray or on the segment??
            # point: raycaster.ray.at(distance),
            point: inter_segment.clone.apply_matrix4(@matrix_matrix),
            index: i,
            offset_index: oi,
            face: nil,
            face_index: nil,
            object: self
          }
        end
      end
    else
      positions = attributes.position.array

      (0...(positions.length / 3 - 1)).step(step) do |i|
        v_start.from_array(positions, 3 * i)
        v_end.from_array(positions, 3 * i + 3)

        dist_sq = @_ray.distance_sq_to_segment(v_start, v_end, inter_ray, inter_segment)

        next if dist_sq > precision_sq

        distance = @_ray.origin.distance_to(inter_ray)

        next if distance < raycaster.near || distance > raycaster.far

        intersects << {
          distance: distance,
          # What do we want? intersection point on the ray or on the segment??
          # point: raycaster.ray.at(distance),
          point: inter_segment.clone.apply_matrix4(@matrix_world),
          index: i,
          face: nil,
          face_index: nil,
          object: self
        }
      end
    end
  elsif geometry.is_a?(Geometry)
    vertices = @geometry.vertices
    nb_vertices = vertices.length

    (0...(nb_vertices - 1)).step(step).each do |i|
      dist_sq = @_ray.distance_sq_to_segment(vertices[i], vertices[i + 1], inter_ray, inter_segment)

      next if dist_sq > precision_sq

      distance = @_ray.origin.distance_to(inter_ray)

      next if distance < raycaster.near || distance > raycaster.far

      intersects << {
        distance: distance,
        # What do we want? intersection point on the ray or on the segment??
        # point: raycaster.ray.at(distance),
        point: inter_segment.clone.apply_matrix4(@matrix_world),
        index: i,
        face: nil,
        face_index: nil,
        object: self
      }
    end
  end
end