Class: Mittsu::Mesh

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

Constant Summary

Constants inherited from Object3D

Object3D::DefaultUp

Instance Attribute Summary collapse

Attributes inherited from Object3D

#active, #cast_shadow, #children, #frustum_culled, #geometry, #id, #matrix, #matrix_auto_update, #matrix_world, #matrix_world_needs_update, #model_view_matrix, #morph_target_influences, #name, #parent, #position, #quaternion, #receive_shadow, #render_order, #rotation, #rotation_auto_update, #scale, #type, #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 = Geometry.new, material = MeshBasicMaterial.new(color: (rand * 0xffffff).to_i)) ⇒ Mesh

Returns a new instance of Mesh.


8
9
10
11
12
13
14
15
16
# File 'lib/mittsu/objects/mesh.rb', line 8

def initialize(geometry = Geometry.new, material = MeshBasicMaterial.new(color: (rand * 0xffffff).to_i))
  super()

  @type = 'Mesh'

  @geometry, @material = geometry, material

  update_morph_targets
end

Instance Attribute Details

#materialObject

Returns the value of attribute material


6
7
8
# File 'lib/mittsu/objects/mesh.rb', line 6

def material
  @material
end

#morph_target_baseObject

Returns the value of attribute morph_target_base


6
7
8
# File 'lib/mittsu/objects/mesh.rb', line 6

def morph_target_base
  @morph_target_base
end

#rendererObject

Returns the value of attribute renderer


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

def renderer
  @renderer
end

Instance Method Details

#add_opengl_objectObject


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mittsu/renderers/opengl/objects/mesh.rb', line 57

def add_opengl_object
  case geometry
  when BufferGeometry
    @renderer.add_opengl_object(geometry, self)
  when Geometry
    geometry.groups.each do |group|
      @renderer.add_opengl_object(group, self)
    end
  else
    raise "GEOMETRY IS NULL"
  end
end

#check_intersection(object, raycaster, ray, pA, pB, pC, point) ⇒ Object


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
# File 'lib/mittsu/objects/mesh.rb', line 40

def check_intersection object, raycaster, ray, pA, pB, pC, point

  intersect = nil;
  material = object.material;

  if material.side == BackSide

    intersect = ray.intersect_triangle( pC, pB, pA, true, point );

  else

    intersect = ray.intersect_triangle( pA, pB, pC, material.side != DoubleSide, point );

  end

   return nil if intersect.nil?

   @intersectionPointWorld ||= Vector3.new
    @intersectionPointWorld.copy( point );
    @intersectionPointWorld.apply_matrix4( @matrix_world );

    distance = raycaster.ray.origin.distance_to( @intersectionPointWorld );

    return nil if ( distance < raycaster.near || distance > raycaster.far )

      return {
        distance: distance,
        point: @intersectionPointWorld.clone(),
        object: object
      };

end

#clone(object = Mesh.new(@geometry, @material), recursive = true) ⇒ Object


264
265
266
267
# File 'lib/mittsu/objects/mesh.rb', line 264

def clone(object = Mesh.new(@geometry, @material), recursive = true)
  super(object, recursive)
  return object
end

#init_geometryObject


52
53
54
55
# File 'lib/mittsu/renderers/opengl/objects/mesh.rb', line 52

def init_geometry
  geometry.renderer = @renderer
  geometry.init_geometry_groups(self)
end

#raycast(raycaster, intersects) ⇒ Object


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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/mittsu/objects/mesh.rb', line 73

def raycast(raycaster, intersects)
  @_inverse_matrix ||= Matrix4.new
  @_ray ||= Ray.new
  @_sphere ||= Sphere.new

  @_v_a ||= Vector3.new
  @_v_b ||= Vector3.new
  @_v_c ||= Vector3.new
  v_a = @_v_a
  v_b = @_v_b
  v_c = @_v_c

  # Checking bounding_sphere distance to ray

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

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

  return unless raycaster.ray.intersection_sphere?(@_sphere)

  # check bounding box before continuing

  @_inverse_matrix = Matrix4.new
  @_inverse_matrix.inverse(@matrix_world)
  @_ray.copy(raycaster.ray).apply_matrix4(@_inverse_matrix)

  if !geometry.bounding_box.nil?
    return unless ray.intersection_box?(geometry.bounding_box)
  end


  if geometry.is_a?(BufferGeometry)
    return if @material.nil?

    attributes = geometry.attributes
    precision = raycaster.precision

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

      offsets = [BufferGeometry::DrawCall.new(0, indices.length, 0)]

      @offsets.each_with_index do |index, oi|
        start = offsets[oi].start
        count = offsets[oi].count
        index = offsets[oi].index

        i = start
        il = start + count
        while i < il
          a = index + indices[i]
          b = index + indices[i + 1]
          c = index + indices[i + 2]

          v_a.from_array(positions, a * 3)
          v_b.from_array(positions, b * 3)
          v_c.from_array(positions, c * 3)

          if material.side == BackSide
            intersection_point = ray.intersect_triangle(v_c, v_b, v_a, true)
          else
            intersection_point = ray.intersect_triangle(v_a, v_b, v_c, material.side != DoubleSide)
          end

          next if intersection_point.nil?

          intersection_point.apply_matrix4(@matrix_world)

          distance = racaster.ray.origin.distance_to(intersection_point)

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

          intersects << {
            distance: distance,
            point: intersection_point,
            face: Face.new(a, b, c, Triangle.normal(v_a, v_b, v_c)),
            face_index: nil,
            object: self
          }
          i += 3
        end
      end
    else
      positions = attributes[:position].array

      i = 0
      j = 0
      il = positions.length
      while i < il
        a = i
        b = i + 1
        c = i + 2

        v_a.from_array(positions, j)
        v_b.from_array(positions, j + 3)
        v_c.from_array(positions, j + 6)

        if material.side = BackSide
          intersection_point = ray.intersect_triangle(v_c, v_b, v_a, true)
        else
          intersection_point = ray.intersect_triangle(v_a, v_b, v_c, material.side != DoubleSide)
        end

        next if intersection_point.nil?

        intersection_point.apply_matrix4(@matrix_world)

        distance = racaster.ray.origin.distance_to(intersection_point)

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

        intersects << {
          distance: distance,
          point: intersection_point,
          face: Face.new(a, b, c, Triangle.normal(v_a, v_b, v_c)),
          face_index: nil,
          object: self
        }

        i += 3
        j += 9
      end
    end
  elsif geometry.is_a? Geometry
    is_face_material = @material.is_a? MeshFaceMaterial
    object_materials = is_face_material ? @material.materials : nil

    precision = raycaster.precision

    vertices = geometry.vertices

    geometry.faces.each do |face|
      material = is_face_material ? object_materials[face.material_index] : @material
      next if material.nil?

      a = vertices[face.a]
      b = vertices[face.b]
      c = vertices[face.c]

      if material.morph_targets
        morph_targets = geometry.morph_targets
        morph_influences

        v_a.set(0.0, 0.0, 0.0)
        v_b.set(0.0, 0.0, 0.0)
        v_c.set(0.0, 0.0, 0.0)

        morph_targets.each_with_index do |morph_target, t|
          influence = morph_influences[t]
          next if influence.zero?

          targets = morph_target.vertices

          v_a.x += (targets[face.a].x - a.x) * influence
          v_a.y += (targets[face.a].y - a.y) * influence
          v_a.z += (targets[face.a].z - a.z) * influence

          v_b.x += (targets[face.b].x - b.x) * influence
          v_b.y += (targets[face.b].y - b.y) * influence
          v_b.z += (targets[face.b].z - b.z) * influence

          v_c.x += (targets[face.c].x - c.x) * influence
          v_c.y += (targets[face.c].y - c.y) * influence
          v_c.z += (targets[face.c].z - c.z) * influence
        end

        v_a.add(a)
        v_b.add(b)
        v_c.add(c)

        a = v_a
        b = v_b
        c = v_c
      end


      intersection_point = Vector3.new
      intersection = check_intersection self, raycaster, @_ray, a, b, c, intersection_point

      next if not intersection
      if intersection
        intersection[:face] = face
      end
      intersects << intersection
    end
  end
end

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


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mittsu/renderers/opengl/objects/mesh.rb', line 5

def render_buffer(camera, lights, fog, material, geometry_group, update_buffers)
  type = GL_UNSIGNED_INT # geometry_group.type_array == Uint32Array ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT

  # wireframe
  if material.wireframe
    @renderer.state.set_line_width(material.wireframe_linewidth * @renderer.pixel_ratio)

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, geometry_group.line_buffer) if update_buffers
    glDrawElements(GL_LINES, geometry_group.line_count, type, 0)

  # triangles
  else
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, geometry_group.face_buffer) if update_buffers
    glDrawElements(GL_TRIANGLES, geometry_group.face_count, type, 0)
  end

  @renderer.info[:render][:calls] += 1
  @renderer.info[:render][:vertices] += geometry_group.face_count
  @renderer.info[:render][:faces] += geometry_group.face_count / 3
end

#updateObject


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
# File 'lib/mittsu/renderers/opengl/objects/mesh.rb', line 26

def update
  # check all geometry groubs
  mat = nil
  geometry.groups.each do |geometry_group|
    # TODO: place to put this???
    # glBindVertexArray(geometry_group.vertex_array_object)
    mat = buffer_material(geometry_group)

    custom_attributes_dirty = mat.attributes && mat.custom_attributes_dirty?

    if geometry.vertices_need_update || geometry.morph_targets_need_update || geometry.elements_need_update || geometry.uvs_need_update || geometry.normals_need_update || geometry.colors_need_update || geometry.tangents_need_update || custom_attributes_dirty
      geometry_group.set_mesh_buffers(self, GL_DYNAMIC_DRAW, !geometry.dynamic, mat)
    end
  end

  geometry.vertices_need_update = false
  geometry.morph_targets_need_update = false
  geometry.elements_need_update = false
  geometry.uvs_need_update = false
  geometry.normals_need_update = false
  geometry.colors_need_update = false
  geometry.tangents_need_update = false

  mat.attributes && mat.clear_custom_attributes
end

#update_morph_targetsObject


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mittsu/objects/mesh.rb', line 18

def update_morph_targets
  if !@geometry.morph_targets.nil? && !@geometry.morph_targets.empty?
    @morph_targets_base = -1
    @morph_target_forced_order = []
    @morph_targets_influences = []
    @morph_targets_dictionary = {}

    @geometry.morph_targets.each_with_index do |target, m|
      @morph_targets_influences << 0
      @morph_targets_dictionary[target.name] = m
    end

    def morph_target_index_by_name(name)
      morph_target_index = @morph_targets_dictionary[name]
      return morph_target_index unless morph_target_index.nil?

      puts "WARNING: Mittsu::Mest#morph_target_index_by_name: morph target #{name} does not exist. Returning 0."
      0
    end
  end
end