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.



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
61
62
63
64
# File 'lib/three/core/object3d.rb', line 34

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
  @visible = true
  @cast_shadow = false
  @receive_shadow = false
  @user_data = {}

  bind_rotation_and_quaternion
  bind_transform_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.



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

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 (readonly)

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.



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

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.



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

def matrix_world_auto_update
  @matrix_world_auto_update
end

#matrix_world_needs_updateObject

Returns the value of attribute matrix_world_needs_update.



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

def matrix_world_needs_update
  @matrix_world_needs_update
end

#nameObject

Returns the value of attribute name.



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

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.



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

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.



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

def type
  @type
end

#upObject

Returns the value of attribute up.



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

def up
  @up
end

#user_dataObject

Returns the value of attribute user_data.



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

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.



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

def visible
  @visible
end

Class Method Details

.allocate_idObject



114
115
116
117
118
# File 'lib/three/core/object3d.rb', line 114

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

Instance Method Details

#add(*objects) ⇒ Object



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

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

#clearObject



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

def clear
  remove(*@children.dup)
end

#dirty_dependency_changed(_resource, _field) ⇒ Object



110
111
112
# File 'lib/three/core/object3d.rb', line 110

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

#get_object_by_id(id) ⇒ Object



165
166
167
# File 'lib/three/core/object3d.rb', line 165

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

#get_object_by_name(name) ⇒ Object



169
170
171
# File 'lib/three/core/object3d.rb', line 169

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

#get_object_by_property(property, value) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/three/core/object3d.rb', line 173

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



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

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



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

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



232
233
234
235
# File 'lib/three/core/object3d.rb', line 232

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



237
238
239
240
241
# File 'lib/three/core/object3d.rb', line 237

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



243
244
245
246
247
# File 'lib/three/core/object3d.rb', line 243

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



102
103
104
105
106
107
108
# File 'lib/three/core/object3d.rb', line 102

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



96
97
98
99
100
# File 'lib/three/core/object3d.rb', line 96

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

#remove(*objects) ⇒ Object



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

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

#remove_from_parentObject



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

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

#to_hObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/three/core/object3d.rb', line 255

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



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

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

#traverse(&block) ⇒ Object



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

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

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

#traverse_ancestors(&block) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/three/core/object3d.rb', line 156

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



147
148
149
150
151
152
153
154
# File 'lib/three/core/object3d.rb', line 147

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



190
191
192
193
194
# File 'lib/three/core/object3d.rb', line 190

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

#update_matrix_world(force = false) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/three/core/object3d.rb', line 196

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



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

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