Class: Three::Object3D

Inherits:
EventDispatcher show all
Includes:
Dirty
Defined in:
lib/three/core/object3d.rb

Direct Known Subclasses

Camera, ExternalObject3D, Group, Light, Line, Mesh, Points, Scene, Sprite

Constant Summary collapse

DEFAULT_UP =
Vector3.new(0, 1, 0)
DEFAULT_MATRIX_AUTO_UPDATE =
true
DEFAULT_MATRIX_WORLD_AUTO_UPDATE =
true

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dirty

#add_dirty_dependent, #dirty?, #dirty_dependents, #dirty_field?, #dirty_fields, #mark_clean!, #remove_dirty_dependent

Methods inherited from EventDispatcher

#add_event_listener, #dispatch_event, #has_event_listener?, #remove_event_listener

Constructor Details

#initializeObject3D

Returns a new instance of Object3D.



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
# File 'lib/three/core/object3d.rb', line 35

def initialize
  super

  @id = self.class.allocate_id
  @uuid = MathUtils.generate_uuid
  @name = ""
  @type = "Object3D"
  @parent = nil
  @children = []
  @layers = Layers.new
  @up = DEFAULT_UP.clone

  @position = Vector3.new
  @rotation = Euler.new
  @quaternion = Quaternion.new
  @scale = Vector3.new(1, 1, 1)
  @matrix = Matrix4.new
  @matrix_world = Matrix4.new
  @matrix_auto_update = DEFAULT_MATRIX_AUTO_UPDATE
  @matrix_world_auto_update = DEFAULT_MATRIX_WORLD_AUTO_UPDATE
  @matrix_world_needs_update = false
  @suppress_matrix_change = false
  @visible = true
  @cast_shadow = false
  @receive_shadow = false
  @user_data = {}

  bind_rotation_and_quaternion
  bind_transform_changes
  bind_matrix_changes
  bind_layer_changes
  mark_dirty!
end

Class Attribute Details

.next_idObject

Returns the value of attribute next_id.



23
24
25
# File 'lib/three/core/object3d.rb', line 23

def next_id
  @next_id
end

Instance Attribute Details

#cast_shadowObject

Returns the value of attribute cast_shadow.



31
32
33
# File 'lib/three/core/object3d.rb', line 31

def cast_shadow
  @cast_shadow
end

#childrenObject (readonly)

Returns the value of attribute children.



26
27
28
# File 'lib/three/core/object3d.rb', line 26

def children
  @children
end

#idObject (readonly)

Returns the value of attribute id.



26
27
28
# File 'lib/three/core/object3d.rb', line 26

def id
  @id
end

#layersObject (readonly)

Returns the value of attribute layers.



27
28
29
# File 'lib/three/core/object3d.rb', line 27

def layers
  @layers
end

#matrixObject

Returns the value of attribute matrix.



29
30
31
# File 'lib/three/core/object3d.rb', line 29

def matrix
  @matrix
end

#matrix_auto_updateObject

Returns the value of attribute matrix_auto_update.



30
31
32
# File 'lib/three/core/object3d.rb', line 30

def matrix_auto_update
  @matrix_auto_update
end

#matrix_worldObject (readonly)

Returns the value of attribute matrix_world.



29
30
31
# File 'lib/three/core/object3d.rb', line 29

def matrix_world
  @matrix_world
end

#matrix_world_auto_updateObject

Returns the value of attribute matrix_world_auto_update.



32
33
34
# File 'lib/three/core/object3d.rb', line 32

def matrix_world_auto_update
  @matrix_world_auto_update
end

#matrix_world_needs_updateObject

Returns the value of attribute matrix_world_needs_update.



32
33
34
# File 'lib/three/core/object3d.rb', line 32

def matrix_world_needs_update
  @matrix_world_needs_update
end

#nameObject

Returns the value of attribute name.



31
32
33
# File 'lib/three/core/object3d.rb', line 31

def name
  @name
end

#parentObject

Returns the value of attribute parent.



26
27
28
# File 'lib/three/core/object3d.rb', line 26

def parent
  @parent
end

#positionObject (readonly)

Returns the value of attribute position.



28
29
30
# File 'lib/three/core/object3d.rb', line 28

def position
  @position
end

#quaternionObject (readonly)

Returns the value of attribute quaternion.



28
29
30
# File 'lib/three/core/object3d.rb', line 28

def quaternion
  @quaternion
end

#receive_shadowObject

Returns the value of attribute receive_shadow.



31
32
33
# File 'lib/three/core/object3d.rb', line 31

def receive_shadow
  @receive_shadow
end

#rotationObject (readonly)

Returns the value of attribute rotation.



28
29
30
# File 'lib/three/core/object3d.rb', line 28

def rotation
  @rotation
end

#scaleObject (readonly)

Returns the value of attribute scale.



28
29
30
# File 'lib/three/core/object3d.rb', line 28

def scale
  @scale
end

#typeObject

Returns the value of attribute type.



31
32
33
# File 'lib/three/core/object3d.rb', line 31

def type
  @type
end

#upObject

Returns the value of attribute up.



31
32
33
# File 'lib/three/core/object3d.rb', line 31

def up
  @up
end

#user_dataObject

Returns the value of attribute user_data.



33
34
35
# File 'lib/three/core/object3d.rb', line 33

def user_data
  @user_data
end

#uuidObject (readonly)

Returns the value of attribute uuid.



26
27
28
# File 'lib/three/core/object3d.rb', line 26

def uuid
  @uuid
end

#visibleObject

Returns the value of attribute visible.



31
32
33
# File 'lib/three/core/object3d.rb', line 31

def visible
  @visible
end

Class Method Details

.allocate_idObject



129
130
131
132
133
# File 'lib/three/core/object3d.rb', line 129

def self.allocate_id
  id = Object3D.next_id
  Object3D.next_id += 1
  id
end

Instance Method Details

#add(*objects) ⇒ Object



135
136
137
138
# File 'lib/three/core/object3d.rb', line 135

def add(*objects)
  objects.each { |object| add_one(object) }
  self
end

#clearObject



150
151
152
# File 'lib/three/core/object3d.rb', line 150

def clear
  remove(*@children.dup)
end

#dirty_dependency_changed(_resource, _field) ⇒ Object



125
126
127
# File 'lib/three/core/object3d.rb', line 125

def dirty_dependency_changed(_resource, _field)
  mark_dirty!(:resources)
end

#get_object_by_id(id) ⇒ Object



180
181
182
# File 'lib/three/core/object3d.rb', line 180

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

#get_object_by_name(name) ⇒ Object



184
185
186
# File 'lib/three/core/object3d.rb', line 184

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

#get_object_by_property(property, value) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/three/core/object3d.rb', line 188

def get_object_by_property(property, value)
  return self if public_send(property) == value

  @children.each do |child|
    object = child.get_object_by_property(property, value)
    return object if object
  end

  nil
end

#get_objects_by_property(property, value, result = []) ⇒ Object



199
200
201
202
203
# File 'lib/three/core/object3d.rb', line 199

def get_objects_by_property(property, value, result = [])
  result << self if public_send(property) == value
  @children.each { |child| child.get_objects_by_property(property, value, result) }
  result
end

#get_world_direction(target = Vector3.new) ⇒ Object



268
269
270
271
272
# File 'lib/three/core/object3d.rb', line 268

def get_world_direction(target = Vector3.new)
  update_world_matrix(true, false)
  elements = @matrix_world.elements
  target.set(elements[8], elements[9], elements[10]).normalize
end

#get_world_position(target = Vector3.new) ⇒ Object



251
252
253
254
# File 'lib/three/core/object3d.rb', line 251

def get_world_position(target = Vector3.new)
  update_world_matrix(true, false)
  target.set_from_matrix_position(@matrix_world)
end

#get_world_quaternion(target = Quaternion.new) ⇒ Object



256
257
258
259
260
# File 'lib/three/core/object3d.rb', line 256

def get_world_quaternion(target = Quaternion.new)
  update_world_matrix(true, false)
  @matrix_world.decompose(Vector3.new, target, Vector3.new)
  target
end

#get_world_scale(target = Vector3.new) ⇒ Object



262
263
264
265
266
# File 'lib/three/core/object3d.rb', line 262

def get_world_scale(target = Vector3.new)
  update_world_matrix(true, false)
  @matrix_world.decompose(Vector3.new, Quaternion.new, target)
  target
end

#mark_descendant_dirty!Object



117
118
119
120
121
122
123
# File 'lib/three/core/object3d.rb', line 117

def mark_descendant_dirty!
  return self if dirty_fields.key?(:descendants)

  dirty_fields[:descendants] = true
  @parent&.mark_descendant_dirty!
  self
end

#mark_dirty!(field = :all) ⇒ Object



111
112
113
114
115
# File 'lib/three/core/object3d.rb', line 111

def mark_dirty!(field = :all)
  super
  @parent&.mark_descendant_dirty!
  self
end

#remove(*objects) ⇒ Object



140
141
142
143
# File 'lib/three/core/object3d.rb', line 140

def remove(*objects)
  objects.each { |object| remove_one(object) }
  self
end

#remove_from_parentObject



145
146
147
148
# File 'lib/three/core/object3d.rb', line 145

def remove_from_parent
  @parent&.remove(self)
  self
end

#to_hObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/three/core/object3d.rb', line 274

def to_h
  {
    uuid: @uuid,
    type: @type,
    name: @name,
    matrix: @matrix.to_a,
    layers: @layers.mask,
    visible: @visible,
    cast_shadow: @cast_shadow,
    receive_shadow: @receive_shadow,
    user_data: @user_data,
    children: @children.map(&:to_h)
  }
end

#to_json(*args) ⇒ Object



289
290
291
# File 'lib/three/core/object3d.rb', line 289

def to_json(*args)
  Exporters::ThreeJSONExporter.new.to_json(self, *args)
end

#traverse(&block) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/three/core/object3d.rb', line 154

def traverse(&block)
  return enum_for(:traverse) unless block

  block.call(self)
  @children.each { |child| child.traverse(&block) }
  self
end

#traverse_ancestors(&block) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/three/core/object3d.rb', line 171

def traverse_ancestors(&block)
  return enum_for(:traverse_ancestors) unless block
  return self unless @parent

  block.call(@parent)
  @parent.traverse_ancestors(&block)
  self
end

#traverse_visible(&block) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/three/core/object3d.rb', line 162

def traverse_visible(&block)
  return enum_for(:traverse_visible) unless block
  return self unless @visible

  block.call(self)
  @children.each { |child| child.traverse_visible(&block) }
  self
end

#update_matrixObject



205
206
207
208
209
210
211
212
213
# File 'lib/three/core/object3d.rb', line 205

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

#update_matrix_world(force = false) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/three/core/object3d.rb', line 215

def update_matrix_world(force = false)
  update_matrix if @matrix_auto_update

  if @matrix_world_needs_update || force
    if @matrix_world_auto_update
      if @parent
        @matrix_world.multiply_matrices(@parent.matrix_world, @matrix)
      else
        @matrix_world.copy(@matrix)
      end
    end

    @matrix_world_needs_update = false
    force = true
  end

  @children.each { |child| child.update_matrix_world(force) }
  self
end

#update_world_matrix(update_parents = false, update_children = false) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/three/core/object3d.rb', line 235

def update_world_matrix(update_parents = false, update_children = false)
  @parent&.update_world_matrix(true, false) if update_parents
  update_matrix if @matrix_auto_update

  if @matrix_world_auto_update
    if @parent
      @matrix_world.multiply_matrices(@parent.matrix_world, @matrix)
    else
      @matrix_world.copy(@matrix)
    end
  end

  @children.each { |child| child.update_world_matrix(false, true) } if update_children
  self
end