Class: Three::InstancedMesh

Inherits:
Mesh show all
Defined in:
lib/three/objects/instanced_mesh.rb

Constant Summary

Constants inherited from Object3D

Object3D::DEFAULT_MATRIX_AUTO_UPDATE, Object3D::DEFAULT_MATRIX_WORLD_AUTO_UPDATE, Object3D::DEFAULT_UP

Instance Attribute Summary collapse

Attributes inherited from Mesh

#geometry, #material, #morph_target_dictionary, #morph_target_influences

Attributes inherited from Object3D

#cast_shadow, #children, #id, #layers, #matrix, #matrix_auto_update, #matrix_world, #matrix_world_auto_update, #matrix_world_needs_update, #name, #parent, #position, #quaternion, #receive_shadow, #rotation, #scale, #type, #up, #user_data, #uuid, #visible

Instance Method Summary collapse

Methods inherited from Object3D

#add, allocate_id, #clear, #dirty_dependency_changed, #get_object_by_id, #get_object_by_name, #get_object_by_property, #get_objects_by_property, #get_world_direction, #get_world_position, #get_world_quaternion, #get_world_scale, #mark_descendant_dirty!, #mark_dirty!, #remove, #remove_from_parent, #to_json, #traverse, #traverse_ancestors, #traverse_visible, #update_matrix, #update_matrix_world, #update_world_matrix

Methods included from Dirty

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

Methods inherited from EventDispatcher

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

Constructor Details

#initialize(geometry = BufferGeometry.new, material = MeshBasicMaterial.new, count = 1) ⇒ InstancedMesh

Returns a new instance of InstancedMesh.



13
14
15
16
17
18
19
20
21
# File 'lib/three/objects/instanced_mesh.rb', line 13

def initialize(geometry = BufferGeometry.new, material = MeshBasicMaterial.new, count = 1)
  super(geometry, material)
  @type = "InstancedMesh"
  @capacity = coerce_count(count)
  @count = @capacity
  @instance_matrices = Array.new(@capacity) { Matrix4.new }
  @instance_colors = nil
  mark_dirty!(:instances)
end

Instance Attribute Details

#capacityObject (readonly)

Returns the value of attribute capacity.



11
12
13
# File 'lib/three/objects/instanced_mesh.rb', line 11

def capacity
  @capacity
end

#countObject

Returns the value of attribute count.



11
12
13
# File 'lib/three/objects/instanced_mesh.rb', line 11

def count
  @count
end

#instance_colorsObject (readonly)

Returns the value of attribute instance_colors.



11
12
13
# File 'lib/three/objects/instanced_mesh.rb', line 11

def instance_colors
  @instance_colors
end

#instance_matricesObject (readonly)

Returns the value of attribute instance_matrices.



11
12
13
# File 'lib/three/objects/instanced_mesh.rb', line 11

def instance_matrices
  @instance_matrices
end

Instance Method Details

#get_color_at(index, target = Color.new) ⇒ Object



51
52
53
54
# File 'lib/three/objects/instanced_mesh.rb', line 51

def get_color_at(index, target = Color.new)
  validate_instance_index(index)
  target.copy(@instance_colors ? @instance_colors[index] : Color.new)
end

#get_matrix_at(index, target = Matrix4.new) ⇒ Object



38
39
40
41
# File 'lib/three/objects/instanced_mesh.rb', line 38

def get_matrix_at(index, target = Matrix4.new)
  validate_instance_index(index)
  target.copy(@instance_matrices[index])
end

#instance_color_needs_update!Object



61
62
63
64
# File 'lib/three/objects/instanced_mesh.rb', line 61

def instance_color_needs_update!
  mark_dirty!(:instance_colors)
  self
end

#instance_matrix_needs_update!Object



56
57
58
59
# File 'lib/three/objects/instanced_mesh.rb', line 56

def instance_matrix_needs_update!
  mark_dirty!(:instances)
  self
end

#set_color_at(index, color) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/three/objects/instanced_mesh.rb', line 43

def set_color_at(index, color)
  validate_instance_index(index)
  ensure_instance_colors
  @instance_colors[index] = coerce_color(color)
  mark_dirty!(:instance_colors)
  self
end

#set_matrix_at(index, matrix) ⇒ Object



31
32
33
34
35
36
# File 'lib/three/objects/instanced_mesh.rb', line 31

def set_matrix_at(index, matrix)
  validate_instance_index(index)
  @instance_matrices[index] = coerce_matrix(matrix)
  mark_dirty!(:instances)
  self
end

#to_hObject



66
67
68
69
70
71
72
73
# File 'lib/three/objects/instanced_mesh.rb', line 66

def to_h
  super.merge(
    count: @count,
    capacity: @capacity,
    instance_matrices: @instance_matrices.map(&:to_a),
    instance_colors: @instance_colors&.map(&:to_a)
  )
end