Class: Mittsu::Object3D

Inherits:
Object
  • Object
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

Constructor Details

#initializeObject3D

Returns a new instance of Object3D.



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

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.



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

def cast_shadow
  @cast_shadow
end

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#frustum_culledObject

Returns the value of attribute frustum_culled.



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

def frustum_culled
  @frustum_culled
end

#geometryObject

Returns the value of attribute geometry.



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

def geometry
  @geometry
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#matrixObject

Returns the value of attribute matrix.



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

def matrix
  @matrix
end

#matrix_auto_updateObject

Returns the value of attribute matrix_auto_update.



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

def matrix_auto_update
  @matrix_auto_update
end

#matrix_worldObject

Returns the value of attribute matrix_world.



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

def matrix_world
  @matrix_world
end

#matrix_world_needs_updateObject

Returns the value of attribute matrix_world_needs_update.



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

def matrix_world_needs_update
  @matrix_world_needs_update
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#positionObject

Returns the value of attribute position.



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

def position
  @position
end

#quaternionObject

Returns the value of attribute quaternion.



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

def quaternion
  @quaternion
end

#receive_shadowObject

Returns the value of attribute receive_shadow.



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

def receive_shadow
  @receive_shadow
end

#render_orderObject

Returns the value of attribute render_order.



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

def render_order
  @render_order
end

#rotationObject

Returns the value of attribute rotation.



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

def rotation
  @rotation
end

#rotation_auto_updateObject

Returns the value of attribute rotation_auto_update.



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

def rotation_auto_update
  @rotation_auto_update
end

#scaleObject

Returns the value of attribute scale.



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

def scale
  @scale
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#upObject

Returns the value of attribute up.



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

def up
  @up
end

#user_dataObject

Returns the value of attribute user_data.



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

def user_data
  @user_data
end

#uuidObject (readonly)

Returns the value of attribute uuid.



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

def uuid
  @uuid
end

#visibleObject

Returns the value of attribute visible.



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

def visible
  @visible
end

Instance Method Details

#add(*arguments) ⇒ Object



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

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



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

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

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



314
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 314

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

#get_object_by_id(id) ⇒ Object



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

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

#get_object_by_name(name) ⇒ Object



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

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

#get_object_by_property(name, value) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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

#implementation(renderer) ⇒ Object



339
340
341
# File 'lib/mittsu/core/object_3d.rb', line 339

def implementation(renderer)
  @_implementation ||= renderer.create_implementation(self)
end

#local_to_world(vector) ⇒ Object



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

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

#look_at(vector) ⇒ Object



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

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


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

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



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

def raycast; end

#remove(*arguments) ⇒ Object



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

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



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

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



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

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

#rotate_y(angle) ⇒ Object



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

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

#rotate_z(angle) ⇒ Object



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

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



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

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



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

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

#set_rotation_from_matrix(m) ⇒ Object



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

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



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

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

#to_jsonObject



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

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

#to_sObject



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

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

#translate_on_axis(axis, distance) ⇒ Object



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

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



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

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

#translate_y(distance) ⇒ Object



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

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

#translate_z(distance) ⇒ Object



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

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

#traverse(&callback) ⇒ Object



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

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

#traverse_ancestors(&callback) ⇒ Object



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

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

#traverse_visible(&callback) ⇒ Object



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

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

#update_matrixObject



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

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

#update_matrix_world(force = false) ⇒ Object



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

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



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

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