Class: Rgltf::Accessor

Inherits:
Properties::Base show all
Defined in:
lib/rgltf/properties/accessor.rb,
lib/rgltf/properties/accessor.rb

Defined Under Namespace

Classes: Sparse

Constant Summary collapse

COMPONENT_TYPES =
AccessorReader::COMPONENT_TYPES
ELEMENT_COUNTS =
AccessorReader::ELEMENT_COUNTS

Instance Attribute Summary collapse

Attributes inherited from Properties::Base

#extensions, #extras, #index, #name, #owner_type, #source_json

Instance Method Summary collapse

Methods inherited from Properties::Base

#extension, #parse_extensions!

Constructor Details

#initialize(json, document:, index:) ⇒ Accessor

Returns a new instance of Accessor.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rgltf/properties/accessor.rb', line 11

def initialize(json, document:, index:)
  super(json, document:, index:, owner_type: :accessor)
  @buffer_view = optional_reference(document, :buffer_views, json['bufferView'], "accessors/#{index}/bufferView")
  @byte_offset = json.fetch('byteOffset', 0)
  @component_type = COMPONENT_TYPES.fetch(json.fetch('componentType'))[:sym]
  @normalized = json.fetch('normalized', false)
  @count = json.fetch('count')
  @type = json.fetch('type')
  ELEMENT_COUNTS.fetch(@type)
  @min = json['min']
  @max = json['max']
  @sparse = json['sparse'] && Sparse.new(json['sparse'], document:, accessor_index: index)
rescue KeyError => e
  raise FormatError, "invalid accessor #{index}: #{e.message}"
end

Instance Attribute Details

#buffer_viewObject (readonly)

Returns the value of attribute buffer_view.



8
9
10
# File 'lib/rgltf/properties/accessor.rb', line 8

def buffer_view
  @buffer_view
end

#byte_offsetObject (readonly)

Returns the value of attribute byte_offset.



8
9
10
# File 'lib/rgltf/properties/accessor.rb', line 8

def byte_offset
  @byte_offset
end

#component_typeObject (readonly)

Returns the value of attribute component_type.



8
9
10
# File 'lib/rgltf/properties/accessor.rb', line 8

def component_type
  @component_type
end

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/rgltf/properties/accessor.rb', line 8

def count
  @count
end

#maxObject (readonly)

Returns the value of attribute max.



8
9
10
# File 'lib/rgltf/properties/accessor.rb', line 8

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



8
9
10
# File 'lib/rgltf/properties/accessor.rb', line 8

def min
  @min
end

#normalizedObject (readonly)

Returns the value of attribute normalized.



8
9
10
# File 'lib/rgltf/properties/accessor.rb', line 8

def normalized
  @normalized
end

#sparseObject (readonly)

Returns the value of attribute sparse.



8
9
10
# File 'lib/rgltf/properties/accessor.rb', line 8

def sparse
  @sparse
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/rgltf/properties/accessor.rb', line 8

def type
  @type
end

Instance Method Details

#each_elementObject



52
53
54
55
56
57
58
59
# File 'lib/rgltf/properties/accessor.rb', line 52

def each_element
  return enum_for(:each_element) unless block_given?

  to_a.each do |element|
    element.is_a?(Array) ? yield(*element) : yield(element)
  end
  self
end

#elementsObject



61
62
63
# File 'lib/rgltf/properties/accessor.rb', line 61

def elements
  enum_for(:each_element)
end

#nested_propertiesObject



81
82
83
# File 'lib/rgltf/properties/accessor.rb', line 81

def nested_properties
  sparse ? [sparse] : []
end

#packedObject



27
28
29
# File 'lib/rgltf/properties/accessor.rb', line 27

def packed
  @packed ||= AccessorReader.packed(self)
end

#packed_as_u32Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/rgltf/properties/accessor.rb', line 65

def packed_as_u32
  unless type == 'SCALAR' && %i[u8 u16 u32].include?(component_type)
    raise UnsupportedError, 'packed_as_u32 requires a SCALAR unsigned integer accessor'
  end

  @packed_as_u32 ||= begin
    directive = AccessorReader::COMPONENTS_BY_SYMBOL.fetch(component_type).fetch(:pack)
    packed.unpack("#{directive}*").pack('L<*').freeze
  end
end

#release!Object



76
77
78
79
# File 'lib/rgltf/properties/accessor.rb', line 76

def release!
  @packed = nil
  @packed_as_u32 = nil
end

#to_aObject



48
49
50
# File 'lib/rgltf/properties/accessor.rb', line 48

def to_a
  AccessorReader.unpack(self, packed)
end

#vertex_formatObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rgltf/properties/accessor.rb', line 31

def vertex_format
  component_count = ELEMENT_COUNTS.fetch(type)
  return nil unless component_count.between?(1, 4)

  prefix = if normalized
             { i8: 'snorm8', u8: 'unorm8', i16: 'snorm16', u16: 'unorm16' }[component_type]
           else
             { i8: 'sint8', u8: 'uint8', i16: 'sint16', u16: 'uint16', u32: 'uint32',
               f32: 'float32' }[component_type]
           end
  return nil unless prefix

  return nil if %i[i8 u8 i16 u16].include?(component_type) && ![2, 4].include?(component_count)

  component_count == 1 ? prefix.to_sym : :"#{prefix}x#{component_count}"
end