Class: Three::BufferGeometry

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

Direct Known Subclasses

BoxGeometry, PlaneGeometry, SphereGeometry

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!, #mark_dirty!, #remove_dirty_dependent

Methods inherited from EventDispatcher

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

Constructor Details

#initializeBufferGeometry

Returns a new instance of BufferGeometry.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/three/core/buffer_geometry.rb', line 23

def initialize
  super()
  @id = self.class.allocate_id
  @uuid = MathUtils.generate_uuid
  @name = ""
  @type = "BufferGeometry"
  @index = nil
  @attributes = {}
  @groups = []
  @bounding_box = nil
  @bounding_sphere = nil
  @draw_range = { start: 0, count: Float::INFINITY }
  @user_data = {}
  mark_dirty!
end

Class Attribute Details

.next_idObject

Returns the value of attribute next_id.



16
17
18
# File 'lib/three/core/buffer_geometry.rb', line 16

def next_id
  @next_id
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



20
21
22
# File 'lib/three/core/buffer_geometry.rb', line 20

def attributes
  @attributes
end

#bounding_boxObject

Returns the value of attribute bounding_box.



21
22
23
# File 'lib/three/core/buffer_geometry.rb', line 21

def bounding_box
  @bounding_box
end

#bounding_sphereObject

Returns the value of attribute bounding_sphere.



21
22
23
# File 'lib/three/core/buffer_geometry.rb', line 21

def bounding_sphere
  @bounding_sphere
end

#draw_rangeObject

Returns the value of attribute draw_range.



20
21
22
# File 'lib/three/core/buffer_geometry.rb', line 20

def draw_range
  @draw_range
end

#groupsObject

Returns the value of attribute groups.



20
21
22
# File 'lib/three/core/buffer_geometry.rb', line 20

def groups
  @groups
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/three/core/buffer_geometry.rb', line 19

def id
  @id
end

#indexObject

Returns the value of attribute index.



20
21
22
# File 'lib/three/core/buffer_geometry.rb', line 20

def index
  @index
end

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/three/core/buffer_geometry.rb', line 20

def name
  @name
end

#typeObject

Returns the value of attribute type.



20
21
22
# File 'lib/three/core/buffer_geometry.rb', line 20

def type
  @type
end

#user_dataObject

Returns the value of attribute user_data.



21
22
23
# File 'lib/three/core/buffer_geometry.rb', line 21

def user_data
  @user_data
end

#uuidObject (readonly)

Returns the value of attribute uuid.



19
20
21
# File 'lib/three/core/buffer_geometry.rb', line 19

def uuid
  @uuid
end

Class Method Details

.allocate_idObject



39
40
41
42
43
# File 'lib/three/core/buffer_geometry.rb', line 39

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

Instance Method Details

#add_group(start, count, material_index = 0) ⇒ Object



83
84
85
86
87
# File 'lib/three/core/buffer_geometry.rb', line 83

def add_group(start, count, material_index = 0)
  @groups << { start: start, count: count, material_index: material_index }
  mark_dirty!(:groups)
  self
end

#clear_groupsObject



89
90
91
92
93
# File 'lib/three/core/buffer_geometry.rb', line 89

def clear_groups
  @groups.clear
  mark_dirty!(:groups)
  self
end

#compute_bounding_boxObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/three/core/buffer_geometry.rb', line 102

def compute_bounding_box
  position = get_attribute(:position)
  @bounding_box = nil
  return self unless position && position.count.positive?

  min = Vector3.new(Float::INFINITY, Float::INFINITY, Float::INFINITY)
  max = Vector3.new(-Float::INFINITY, -Float::INFINITY, -Float::INFINITY)

  position.count.times do |index|
    x = position.get_x(index)
    y = position.get_y(index)
    z = position.get_z(index)
    min.x = [min.x, x].min
    min.y = [min.y, y].min
    min.z = [min.z, z].min
    max.x = [max.x, x].max
    max.y = [max.y, y].max
    max.z = [max.z, z].max
  end

  @bounding_box = { min: min, max: max }
  self
end

#compute_bounding_sphereObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/three/core/buffer_geometry.rb', line 126

def compute_bounding_sphere
  compute_bounding_box unless @bounding_box
  position = get_attribute(:position)
  @bounding_sphere = nil
  return self unless position && @bounding_box

  center = @bounding_box[:min].clone.add(@bounding_box[:max]).multiply_scalar(0.5)
  max_radius_sq = 0

  position.count.times do |index|
    point = Vector3.new(position.get_x(index), position.get_y(index), position.get_z(index))
    max_radius_sq = [max_radius_sq, center.distance_to_squared(point)].max
  end

  @bounding_sphere = { center: center, radius: Math.sqrt(max_radius_sq) }
  self
end

#delete_attribute(name) ⇒ Object



73
74
75
76
77
# File 'lib/three/core/buffer_geometry.rb', line 73

def delete_attribute(name)
  @attributes.delete(name.to_sym)
  mark_dirty!(:attributes)
  self
end

#disposeObject



144
145
146
# File 'lib/three/core/buffer_geometry.rb', line 144

def dispose
  dispatch_event(:dispose)
end

#get_attribute(name) ⇒ Object



62
63
64
# File 'lib/three/core/buffer_geometry.rb', line 62

def get_attribute(name)
  @attributes[name.to_sym]
end

#get_indexObject



45
46
47
# File 'lib/three/core/buffer_geometry.rb', line 45

def get_index
  @index
end

#has_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/three/core/buffer_geometry.rb', line 79

def has_attribute?(name)
  @attributes.key?(name.to_sym)
end

#set_attribute(name, attribute) ⇒ Object



66
67
68
69
70
71
# File 'lib/three/core/buffer_geometry.rb', line 66

def set_attribute(name, attribute)
  @attributes[name.to_sym] = attribute
  bind_attribute_change(attribute)
  mark_dirty!(:attributes)
  self
end

#set_draw_range(start, count) ⇒ Object



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

def set_draw_range(start, count)
  @draw_range[:start] = start
  @draw_range[:count] = count
  mark_dirty!(:draw_range)
  self
end

#set_index(index) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/three/core/buffer_geometry.rb', line 49

def set_index(index)
  @index =
    if index.is_a?(Array)
      attribute_class = index.any? { |value| value > 65_535 } ? Uint32BufferAttribute : Uint16BufferAttribute
      attribute_class.new(index, 1)
    else
      index
    end
  bind_attribute_change(@index)
  mark_dirty!(:index)
  self
end

#to_hObject



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/three/core/buffer_geometry.rb', line 148

def to_h
  {
    uuid: @uuid,
    type: @type,
    name: @name,
    index: @index&.to_h,
    attributes: @attributes.transform_values(&:to_h),
    groups: @groups.map(&:dup),
    bounding_box: serialize_bounds(@bounding_box),
    bounding_sphere: serialize_sphere(@bounding_sphere)
  }
end