Class: Mittsu::Line

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

Direct Known Subclasses

CameraHelper

Constant Summary

Constants inherited from Object3D

Object3D::DefaultUp

Instance Attribute Summary collapse

Attributes inherited from Object3D

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

Instance Method Summary collapse

Methods inherited from Object3D

#active?, #add, #apply_matrix, #buffer_material, #deinit, #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, #init, #load_uniforms_matrices, #local_to_world, #look_at, #print_tree, #project, #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, #setup_matrices, #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

#add_opengl_objectObject



41
42
43
# File 'lib/mittsu/renderers/opengl/objects/line.rb', line 41

def add_opengl_object
  @renderer.add_opengl_object(geometry, self)
end

#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

#init_geometryObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mittsu/renderers/opengl/objects/line.rb', line 29

def init_geometry
  geometry.renderer = @renderer
  if geometry.vertex_buffer.nil?
    geometry.create_line_buffers
    geometry.init_line_buffers(self)

    geometry.vertices_need_update = true
    geometry.colors_need_update = true
    geometry.line_distances_need_update = true
  end
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

#render_buffer(camera, lights, fog, material, geometry_group, update_buffers) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/mittsu/renderers/opengl/objects/line.rb', line 3

def render_buffer(camera, lights, fog, material, geometry_group, update_buffers)
  opengl_mode = mode == LineStrip ? GL_LINE_STRIP : GL_LINES

  @renderer.state.set_line_width(material.line_width * @renderer.pixel_ratio)

  glDrawArrays(opengl_mode, 0, geometry_group.line_count)

  @renderer.info[:render][:calls] += 1
end

#updateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mittsu/renderers/opengl/objects/line.rb', line 13

def update
  # TODO: glBindVertexArray ???
  material = buffer_material(geometry)
  custom_attributes_dirty = material.attributes && material.custom_attributes_dirty?

  if geometry.vertices_need_update || geometry.colors_need_update || geometry.line_distances_need_update || custom_attributes_dirty
    geometry.set_line_buffers(GL_DYNAMIC_DRAW)
  end

  geometry.vertices_need_update = false
  geometry.colors_need_update = false
  geometry.line_distances_need_update = false

  material.attributes && material.clear_custom_attributes
end