Class: Mittsu::Object3D

Inherits:
HashObject show all
Includes:
EventDispatcher
Defined in:
lib/mittsu/core/object_3d.rb

Direct Known Subclasses

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

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

Methods inherited from HashObject

#[], #[]=, #delete

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

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

  @uuid = SecureRandom.uuid

  @name = ''
  @type = 'Object3D'

  @children = []

  @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 = {}
end

Instance Attribute Details

#cast_shadowObject

Returns the value of attribute cast_shadow.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def cast_shadow
  @cast_shadow
end

#childrenObject

Returns the value of attribute children.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def children
  @children
end

#frustum_culledObject

Returns the value of attribute frustum_culled.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def frustum_culled
  @frustum_culled
end

#geometryObject

Returns the value of attribute geometry.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

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

#matrixObject

Returns the value of attribute matrix.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def matrix
  @matrix
end

#matrix_auto_updateObject

Returns the value of attribute matrix_auto_update.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def matrix_auto_update
  @matrix_auto_update
end

#matrix_worldObject

Returns the value of attribute matrix_world.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def matrix_world
  @matrix_world
end

#matrix_world_needs_updateObject

Returns the value of attribute matrix_world_needs_update.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def matrix_world_needs_update
  @matrix_world_needs_update
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def name
  @name
end

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def parent
  @parent
end

#positionObject

Returns the value of attribute position.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def position
  @position
end

#quaternionObject

Returns the value of attribute quaternion.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def quaternion
  @quaternion
end

#receive_shadowObject

Returns the value of attribute receive_shadow.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def receive_shadow
  @receive_shadow
end

#render_orderObject

Returns the value of attribute render_order.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def render_order
  @render_order
end

#rotationObject

Returns the value of attribute rotation.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def rotation
  @rotation
end

#rotation_auto_updateObject

Returns the value of attribute rotation_auto_update.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def rotation_auto_update
  @rotation_auto_update
end

#scaleObject

Returns the value of attribute scale.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

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.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def up
  @up
end

#user_dataObject

Returns the value of attribute user_data.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

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.



9
10
11
# File 'lib/mittsu/core/object_3d.rb', line 9

def visible
  @visible
end

Instance Method Details

#add(*arguments) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/mittsu/core/object_3d.rb', line 148

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

#apply_matrix(matrix) ⇒ Object



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

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

#clone(object = Object3D.new, recursive = true) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/mittsu/core/object_3d.rb', line 315

def clone(object = Object3D.new, recursive = true)
  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

#get_object_by_id(id) ⇒ Object



188
189
190
# File 'lib/mittsu/core/object_3d.rb', line 188

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

#get_object_by_name(name) ⇒ Object



192
193
194
# File 'lib/mittsu/core/object_3d.rb', line 192

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

#get_object_by_property(name, value) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/mittsu/core/object_3d.rb', line 196

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



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

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



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

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



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

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



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

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



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

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

#local_to_world(vector) ⇒ Object



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

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

#look_at(vector) ⇒ Object



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

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


247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/mittsu/core/object_3d.rb', line 247

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

#raycastObject



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

def raycast; end

#remove(*arguments) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/mittsu/core/object_3d.rb', line 171

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



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

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



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

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

#rotate_y(angle) ⇒ Object



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

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

#rotate_z(angle) ⇒ Object



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

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

#set_rotation_from_axis_angle(axis, angle) ⇒ Object



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

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



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

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

#set_rotation_from_matrix(m) ⇒ Object



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

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



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

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

#to_jsonObject



301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/mittsu/core/object_3d.rb', line 301

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

#to_sObject



260
261
262
# File 'lib/mittsu/core/object_3d.rb', line 260

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

#translate_on_axis(axis, distance) ⇒ Object



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

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



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

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

#translate_y(distance) ⇒ Object



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

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

#translate_z(distance) ⇒ Object



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

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

#traverse(&callback) ⇒ Object



240
241
242
243
244
245
# File 'lib/mittsu/core/object_3d.rb', line 240

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

#traverse_ancestors(&callback) ⇒ Object



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

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

#traverse_visible(&callback) ⇒ Object



264
265
266
267
268
269
270
# File 'lib/mittsu/core/object_3d.rb', line 264

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

#update_matrixObject



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

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

#update_matrix_world(force = false) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/mittsu/core/object_3d.rb', line 284

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



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

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