Class: Mittsu::Object3D

Inherits:
Object
  • Object
show all
Includes:
EventDispatcher
Defined in:
lib/mittsu/core/object_3d.rb,
lib/mittsu/renderers/opengl/core/object_3d.rb

Direct Known Subclasses

Camera, CubeCamera, Group, Light, Line, Mesh, PointCloud, Scene, Sprite

Constant Summary collapse

DefaultUp =
Vector3.new(0.0, 1.0, 0.0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventDispatcher

#add_event_listener, #dispatch_event, #has_event_listener, #remove_event_listener

Constructor Details

#initializeObject3D

Returns a new instance of Object3D.



15
16
17
18
19
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
# File 'lib/mittsu/core/object_3d.rb', line 15

def initialize
  super
  @id = (@@id ||= 1).tap { @@id += 1 }

  @uuid = SecureRandom.uuid

  @type = 'Object3D'

  @children = []
  @parent = nil
  @name = nil

  @up = DefaultUp.clone

  @position = Vector3.new
  @rotation = Euler.new
  @quaternion = Quaternion.new
  @scale = Vector3.new(1.0, 1.0, 1.0)

  @rotation.on_change do
    @quaternion.set_from_euler(rotation, false)
  end

  @quaternion.on_change do
    @rotation.set_from_quaternion(quaternion, false)
  end

  @rotation_auto_update = true

  @matrix = Matrix4.new
  @matrix_world = Matrix4.new

  @matrix_auto_update = true
  @matrix_world_needs_update = false

  @visible = true

  @cast_shadow = false
  @receive_shadow = false

  @frustum_culled = true
  @render_order = 0

  @user_data = {}
  @_listeners = {}
end

Instance Attribute Details

#active=(value) ⇒ Object (writeonly)

Sets the attribute active

Parameters:

  • value

    the value to set the attribute active to.



5
6
7
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 5

def active=(value)
  @active = value
end

#cast_shadowObject

Returns the value of attribute cast_shadow.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def cast_shadow
  @cast_shadow
end

#childrenObject

Returns the value of attribute children.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def children
  @children
end

#frustum_culledObject

Returns the value of attribute frustum_culled.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def frustum_culled
  @frustum_culled
end

#geometryObject

Returns the value of attribute geometry.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def geometry
  @geometry
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/mittsu/core/object_3d.rb', line 11

def id
  @id
end

#inittedObject

Returns the value of attribute initted.



3
4
5
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 3

def initted
  @initted
end

#matrixObject

Returns the value of attribute matrix.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def matrix
  @matrix
end

#matrix_auto_updateObject

Returns the value of attribute matrix_auto_update.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def matrix_auto_update
  @matrix_auto_update
end

#matrix_worldObject

Returns the value of attribute matrix_world.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def matrix_world
  @matrix_world
end

#matrix_world_needs_updateObject

Returns the value of attribute matrix_world_needs_update.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def matrix_world_needs_update
  @matrix_world_needs_update
end

#model_view_matrixObject (readonly)

Returns the value of attribute model_view_matrix.



4
5
6
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 4

def model_view_matrix
  @model_view_matrix
end

#morph_target_influencesObject

Returns the value of attribute morph_target_influences.



3
4
5
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 3

def morph_target_influences
  @morph_target_influences
end

#nameObject



62
63
64
# File 'lib/mittsu/core/object_3d.rb', line 62

def name
  @name || "<#{@type} ##{@id}>"
end

#parentObject

Returns the value of attribute parent.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def parent
  @parent
end

#positionObject

Returns the value of attribute position.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def position
  @position
end

#quaternionObject

Returns the value of attribute quaternion.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def quaternion
  @quaternion
end

#receive_shadowObject

Returns the value of attribute receive_shadow.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def receive_shadow
  @receive_shadow
end

#render_orderObject

Returns the value of attribute render_order.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def render_order
  @render_order
end

#rendererObject

Returns the value of attribute renderer.



3
4
5
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 3

def renderer
  @renderer
end

#rotationObject

Returns the value of attribute rotation.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def rotation
  @rotation
end

#rotation_auto_updateObject

Returns the value of attribute rotation_auto_update.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def rotation_auto_update
  @rotation_auto_update
end

#scaleObject

Returns the value of attribute scale.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def scale
  @scale
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/mittsu/core/object_3d.rb', line 11

def type
  @type
end

#upObject

Returns the value of attribute up.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def up
  @up
end

#user_dataObject

Returns the value of attribute user_data.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def user_data
  @user_data
end

#uuidObject (readonly)

Returns the value of attribute uuid.



11
12
13
# File 'lib/mittsu/core/object_3d.rb', line 11

def uuid
  @uuid
end

#visibleObject

Returns the value of attribute visible.



7
8
9
# File 'lib/mittsu/core/object_3d.rb', line 7

def visible
  @visible
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 7

def active?
  @active
end

#add(*arguments) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/mittsu/core/object_3d.rb', line 154

def add(*arguments)
  if arguments.length > 1
    arguments.each do |arg|
      self.add(arg)
    end
    return self
  end
  object = arguments.first
  if object == self
    puts("ERROR: Mittsu::Object3D#add: object can't be added as a child of itself.", object.inspect)
    return self
  end
  if object.is_a? Object3D
    object.parent.remove(object) unless object.parent.nil?
    object.parent = self
    object.dispatch_event type: :added
    @children << object
  else
    puts('ERROR: Mittsu::Object3D#add: object not an instance of Object3D.', object.inspect)
  end
  self
end

#add_opengl_objectObject



115
116
117
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 115

def add_opengl_object
  # NOOP
end

#apply_matrix(matrix) ⇒ Object



66
67
68
69
# File 'lib/mittsu/core/object_3d.rb', line 66

def apply_matrix(matrix)
  @matrix.multiply_matrices(matrix, @matrix)
  @matrix.decompose(@position, @quaternion, @scale)
end

#buffer_material(geometry_group) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 103

def buffer_material(geometry_group)
  if material.is_a?(MeshFaceMaterial)
    material.materials[geometry_group.material_index]
  else
    material
  end
end

#clone(object = nil, recursive = true) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/mittsu/core/object_3d.rb', line 321

def clone(object = nil, recursive = true)
  object ||= Object3D.new
  object.name = @name
  object.up.copy(@up)
  object.position.copy(@position)
  object.quaternion.copy(@quaternion)
  object.scale.copy(@scale)
  object.rotation_auto_update = @rotation_auto_update
  object.matrix.copy(@matrix)
  object.matrix_world.copy(@matrix_world)
  object.matrix_auto_update = @matrix_auto_update
  object.matrix_world_needs_update = @matrix_world_needs_update
  object.visible = @visible
  object.cast_shadow = @cast_shadow
  object.receive_shadow = @receive_shadow
  object.frustum_culled = @frustum_culled
  object.user_data = @user_data
  if recursive
    @children.each do |child|
      object.add(child.clone)
    end
  end
  object
end

#deinitObject



119
120
121
122
123
124
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 119

def deinit
  @initted = nil
  @model_view_matrix = nil
  @normal_matrix = nil
  @active = nil
end

#get_object_by_id(id) ⇒ Object



194
195
196
# File 'lib/mittsu/core/object_3d.rb', line 194

def get_object_by_id(id)
  self.get_object_by_property(:id, id)
end

#get_object_by_name(name) ⇒ Object



198
199
200
# File 'lib/mittsu/core/object_3d.rb', line 198

def get_object_by_name(name)
  self.get_object_by_property(:name, name)
end

#get_object_by_property(name, value) ⇒ Object



202
203
204
205
206
207
208
209
# File 'lib/mittsu/core/object_3d.rb', line 202

def get_object_by_property(name, value)
  return self if self.send(name) == value
  @children.each do |child|
    object = child.get_object_by_property(name, value)
    return object unless object.nil?
  end
  nil
end

#get_world_direction(target = Vector3.new) ⇒ Object



238
239
240
241
242
# File 'lib/mittsu/core/object_3d.rb', line 238

def get_world_direction(target = Vector3.new)
  @_quaternion ||= Quaternion.new
  self.get_world_quaternion(@_quaternion)
  target.set(0.0, 0.0, 1.0).apply_quaternion(@_quaternion)
end

#get_world_position(target = Vector3.new) ⇒ Object



211
212
213
214
# File 'lib/mittsu/core/object_3d.rb', line 211

def get_world_position(target = Vector3.new)
  self.update_matrix_world(true)
  target.set_from_matrix_position(@matrix_world)
end

#get_world_quaternion(target = Quaternion.new) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/mittsu/core/object_3d.rb', line 216

def get_world_quaternion(target = Quaternion.new)
  @_position ||= Vector3.new
  @_scale ||= Vector3.new
  self.update_matrix_world(true)
  @matrix_world.decompose(@_position, target, @_scale)
  target
end

#get_world_rotation(target = Euler.new) ⇒ Object



224
225
226
227
228
# File 'lib/mittsu/core/object_3d.rb', line 224

def get_world_rotation(target = Euler.new)
  @_quaternion ||= Quaternion.new
  self.get_world_quaternion(quaternion)
  target.set_from_quaternion(quaternion, @rotation.order, false)
end

#get_world_scale(target = Vector3.new) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/mittsu/core/object_3d.rb', line 230

def get_world_scale(target = Vector3.new)
  @_position ||= Vector3.new
  @_quaternion ||= Quaternion.new
  self.update_matrix_world(true)
  @matrix_world.decompose(@_position, @_quaternion, target)
  target
end

#initObject



11
12
13
14
15
16
17
18
19
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
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 11

def init
  if !@initted
    puts " --- INIT #{self.name}" if DEBUG

    @initted = true
    @model_view_matrix = Matrix4.new
    @normal_matrix = Matrix3.new

    add_event_listener(:removed, @renderer.method(:on_object_removed))
  end

  if geometry.nil?
    # ImmediateRenderObject
  else
    if !geometry.initted
      geometry.initted = true
      geometry.add_event_listener(:dispose, @renderer.method(:on_geometry_dispose))
      if geometry.is_a?(BufferGeometry)
        @renderer.info[:memory][:geometries] += 1
      else
        init_geometry
      end
      # TODO: when PointCloud exists
      # when PointCloud
      #   if geometry[:_opengl_vertex_buffer].nil?
      #     create_particle_buffers(geometry)
      #     init_particle_buffers(geometry, object)
      #
      #     geometry.vertices_need_update = true
      #     geometry.colors_need_update = true
      #   end
    end
  end

  if !@active
    @active = true

    add_opengl_object
    # TODO: when ImmediateRenderObject exists
    # if object.is_a? ImmediateRenderObject || object.immediate_render_callback
    #   add_buffer_immediate(@renderer.instance_variable_get(:@_opengl_objects_immediate), self)
    # end
  end
end

#init_geometryObject



111
112
113
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 111

def init_geometry
  # NOOP
end

#load_uniforms_matrices(uniforms) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 91

def load_uniforms_matrices(uniforms)
  glUniformMatrix4fv(uniforms['modelViewMatrix'],
                     1, GL_FALSE,
                     array_to_ptr_easy(@model_view_matrix.elements))

  if uniforms['normalMatrix']
    glUniformMatrix3fv(uniforms['normalMatrix'],
                       1, GL_FALSE,
                       array_to_ptr_easy(@normal_matrix.elements))
  end
end

#local_to_world(vector) ⇒ Object



138
139
140
# File 'lib/mittsu/core/object_3d.rb', line 138

def local_to_world(vector)
  vector.apply_matrix4(@matrix_world)
end

#look_at(vector) ⇒ Object



147
148
149
150
151
152
# File 'lib/mittsu/core/object_3d.rb', line 147

def look_at(vector)
  # This routine does not support objects with rotated and/or translated parent(s)
  @_m1 ||= Matrix4.new
  @_m1.look_at(vector, @position, self.up)
  @quaternion.set_from_rotation_matrix(@_m1)
end


253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/mittsu/core/object_3d.rb', line 253

def print_tree(lines=[])
  if lines.empty?
    puts self
  else
    last = !lines.last
    indent = lines[0..-2].map{|l| l ? '' : '  '}.join
    puts "#{indent}#{last ? '' : ''}#{self}"
  end
  @children.each do |child|
    child.print_tree(lines + [child != @children.last])
  end
end

#project(renderer) ⇒ Object



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
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 56

def project(renderer)
  puts " --- PROJECT #{self.name}" if DEBUG
  @renderer = renderer
  return unless visible
  init

  # TODO!!! FIXME!!!
  opengl_objects = @renderer.instance_variable_get(:@_opengl_objects)[id]

  if opengl_objects && (!frustum_culled || @renderer.object_in_frustum?(self))
    opengl_objects.each do |opengl_object|
      # TODO!!! FIXME!!!
      @renderer.send(:unroll_buffer_material, opengl_object)

      opengl_object.render = true
      if @renderer.sort_objects?
        @_vector3 ||= Vector3.new

        @_vector3.set_from_matrix_position(matrix_world)
        @_vector3.apply_projection(@renderer.proj_screen_matrix)

        opengl_object[:z] = @_vector3.z
      end
    end
  end

  project_children
end

#raycast(raycaster, intersects) ⇒ Object



244
# File 'lib/mittsu/core/object_3d.rb', line 244

def raycast(raycaster, intersects); end

#remove(*arguments) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mittsu/core/object_3d.rb', line 177

def remove(*arguments)
  if arguments.length > 1
    arguments.each do |arg|
      self.remove(arg)
    end
    return
  end
  object = arguments.first
  index = @children.index(object)
  if index
    object.parent = nil
    object.dispatch_event type: :removed
    @children.delete_at index
  end
  nil
end

#rotate_on_axis(axis, angle) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/mittsu/core/object_3d.rb', line 90

def rotate_on_axis(axis, angle)
  # rotate object on axis in object space
  # axis is assumed to be normalized
  @_q1 ||= Quaternion.new
  @_q1.set_from_axis_angle(axis, angle)
  @quaternion.multiply(@_q1)
  self
end

#rotate_x(angle) ⇒ Object



99
100
101
102
# File 'lib/mittsu/core/object_3d.rb', line 99

def rotate_x(angle)
  @_x_axis ||= Vector3.new(1, 0, 0)
  self.rotate_on_axis(@_x_axis, angle)
end

#rotate_y(angle) ⇒ Object



104
105
106
107
# File 'lib/mittsu/core/object_3d.rb', line 104

def rotate_y(angle)
  @_y_axis ||= Vector3.new(0, 1, 0)
  self.rotate_on_axis(@_y_axis, angle)
end

#rotate_z(angle) ⇒ Object



109
110
111
112
# File 'lib/mittsu/core/object_3d.rb', line 109

def rotate_z(angle)
  @_z_axis ||= Vector3.new(0, 0, 1)
  self.rotate_on_axis(@_z_axis, angle)
end

#set_rotation_from_axis_angle(axis, angle) ⇒ Object



71
72
73
74
# File 'lib/mittsu/core/object_3d.rb', line 71

def set_rotation_from_axis_angle(axis, angle)
  # assumes axis is normalized
  @quaternion.set_from_axis_angle(axis, angle)
end

#set_rotation_from_euler(euler) ⇒ Object



76
77
78
# File 'lib/mittsu/core/object_3d.rb', line 76

def set_rotation_from_euler(euler)
  @quaternion.set_from_euler(euler, true)
end

#set_rotation_from_matrix(m) ⇒ Object



80
81
82
83
# File 'lib/mittsu/core/object_3d.rb', line 80

def set_rotation_from_matrix(m)
  # assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  @quaternion.set_from_rotation_matrix(m)
end

#set_rotation_from_quaternion(q) ⇒ Object



85
86
87
88
# File 'lib/mittsu/core/object_3d.rb', line 85

def set_rotation_from_quaternion(q)
  # assumes q is normalized
  @quaternion.copy(q)
end

#setup_matrices(camera) ⇒ Object



85
86
87
88
89
# File 'lib/mittsu/renderers/opengl/core/object_3d.rb', line 85

def setup_matrices(camera)
  @model_view_matrix.multiply_matrices(camera.matrix_world_inverse, matrix_world)
  @normal_matrix.normal_matrix(@model_view_matrix)
  @model_view_matrix
end

#to_jsonObject



307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/mittsu/core/object_3d.rb', line 307

def to_json
  @_output = {
    metadata: {
      version: 4.3,
      type: 'Object',
      generator: 'ObjectExporter'
    }
  }
  @_geometries = {}
  @_materials = {}
  @_output[:object] = self.jsonify
  @_output
end

#to_sObject



266
267
268
# File 'lib/mittsu/core/object_3d.rb', line 266

def to_s
  "#{type} (#{name}) #{position}"
end

#translate_on_axis(axis, distance) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/mittsu/core/object_3d.rb', line 114

def translate_on_axis(axis, distance)
  # translate object by distance along axis in object space
  # axis is assumed to be normalized
  @_v1 ||= Vector3.new
  @_v1.copy(axis).apply_quaternion(@quaternion)
  @position.add(@_v1.multiply_scalar(distance))
  self
end

#translate_x(distance) ⇒ Object



123
124
125
126
# File 'lib/mittsu/core/object_3d.rb', line 123

def translate_x(distance)
  @_x_axis ||= Vector3.new(1, 0, 0)
  self.translate_on_axis(@_x_axis, distance)
end

#translate_y(distance) ⇒ Object



128
129
130
131
# File 'lib/mittsu/core/object_3d.rb', line 128

def translate_y(distance)
  @_y_axis ||= Vector3.new(0, 1, 0)
  self.translate_on_axis(@_y_axis, distance)
end

#translate_z(distance) ⇒ Object



133
134
135
136
# File 'lib/mittsu/core/object_3d.rb', line 133

def translate_z(distance)
  @_z_axis ||= Vector3.new(0, 0, 1)
  self.translate_on_axis(@_z_axis, distance)
end

#traverse(&callback) ⇒ Object



246
247
248
249
250
251
# File 'lib/mittsu/core/object_3d.rb', line 246

def traverse(&callback)
  callback.yield self
  @children.each do |child|
    child.traverse(&callback)
  end
end

#traverse_ancestors(&callback) ⇒ Object



278
279
280
281
282
283
# File 'lib/mittsu/core/object_3d.rb', line 278

def traverse_ancestors(&callback)
  if @parent
    callback.yield @parent
    @parent.traverse_ancestors(&callback)
  end
end

#traverse_visible(&callback) ⇒ Object



270
271
272
273
274
275
276
# File 'lib/mittsu/core/object_3d.rb', line 270

def traverse_visible(&callback)
  return unless @visible
  callback.yield self
  @children.each do |child|
    child.traverse_visible(&callback)
  end
end

#update_matrixObject



285
286
287
288
# File 'lib/mittsu/core/object_3d.rb', line 285

def update_matrix
  @matrix.compose(@position, @quaternion, @scale)
  @matrix_world_needs_update = true
end

#update_matrix_world(force = false) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/mittsu/core/object_3d.rb', line 290

def update_matrix_world(force = false)
  self.update_matrix if @matrix_auto_update
  if @matrix_world_needs_update || force
    if @parent.nil?
      @matrix_world.copy(@matrix)
    else
      @matrix_world.multiply_matrices(@parent.matrix_world, @matrix)
    end
    @matrix_world_needs_update = false
    force = true
  end
  # update children
  @children.each do |child|
    child.update_matrix_world(force)
  end
end

#world_to_local(vector) ⇒ Object



142
143
144
145
# File 'lib/mittsu/core/object_3d.rb', line 142

def world_to_local(vector)
  @_m1 ||= Matrix4.new
  vector.apply_matrix4(@_m1.get_inverse(@matrix_world))
end