Module: Rgltf::AccessorReader

Defined in:
lib/rgltf/accessor_reader.rb

Constant Summary collapse

COMPONENT_TYPES =
{
  5120 => { sym: :i8, size: 1, pack: 'c', min: -128, max: 127 },
  5121 => { sym: :u8, size: 1, pack: 'C', min: 0, max: 255 },
  5122 => { sym: :i16, size: 2, pack: 's<', min: -32_768, max: 32_767 },
  5123 => { sym: :u16, size: 2, pack: 'S<', min: 0, max: 65_535 },
  5125 => { sym: :u32, size: 4, pack: 'L<', min: 0, max: 4_294_967_295 },
  5126 => { sym: :f32, size: 4, pack: 'e' }
}.freeze
COMPONENTS_BY_SYMBOL =
COMPONENT_TYPES.to_h { |number, info| [info[:sym], info.merge(number:)] }.freeze
ELEMENT_COUNTS =
{
  'SCALAR' => 1, 'VEC2' => 2, 'VEC3' => 3, 'VEC4' => 4,
  'MAT2' => 4, 'MAT3' => 9, 'MAT4' => 16
}.freeze
MATRIX_DIMENSIONS =
{ 'MAT2' => 2, 'MAT3' => 3, 'MAT4' => 4 }.freeze

Class Method Summary collapse

Class Method Details

.align4(value) ⇒ Object



138
139
140
# File 'lib/rgltf/accessor_reader.rb', line 138

def align4(value)
  (value + 3) & ~3
end

.append_element(output, bytes, start, type, component_size, storage_size) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rgltf/accessor_reader.rb', line 84

def append_element(output, bytes, start, type, component_size, storage_size)
  dimension = MATRIX_DIMENSIONS[type]
  unless dimension && component_size < 4
    output << bytes.byteslice(start, storage_size)
    return
  end

  column_size = dimension * component_size
  column_stride = align4(column_size)
  dimension.times { |column| output << bytes.byteslice(start + (column * column_stride), column_size) }
end

.apply_sparse(base, accessor, element_size) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rgltf/accessor_reader.rb', line 96

def apply_sparse(base, accessor, element_size)
  sparse = accessor.sparse
  indices_info = COMPONENTS_BY_SYMBOL.fetch(sparse.indices.component_type)
  index_bytes = extract(
    sparse.indices.buffer_view,
    sparse.indices.byte_offset,
    sparse.count,
    'SCALAR',
    indices_info[:size],
    indices_info[:size]
  )
  indices = index_bytes.unpack("#{indices_info[:pack]}*")

  component_size = COMPONENTS_BY_SYMBOL.fetch(accessor.component_type)[:size]
  values = extract(
    sparse.values.buffer_view,
    sparse.values.byte_offset,
    sparse.count,
    accessor.type,
    component_size,
    element_size
  )

  output = base.dup
  indices.each_with_index do |target, sparse_index|
    raise FormatError, "sparse accessor index #{target} is out of range" if target >= accessor.count

    output[target * element_size, element_size] = values.byteslice(sparse_index * element_size, element_size)
  end
  output
end

.extract(view, accessor_offset, count, type, component_size, output_element_size) ⇒ Object

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rgltf/accessor_reader.rb', line 60

def extract(view, accessor_offset, count, type, component_size, output_element_size)
  storage_size = storage_element_size(type, component_size)
  stride = view.byte_stride || storage_size
  start = view.byte_offset + accessor_offset
  required = count.zero? ? 0 : (stride * (count - 1)) + storage_size
  if accessor_offset + required > view.byte_length
    raise FormatError, "accessor data exceeds bufferView #{view.index}"
  end

  bytes = view.buffer.bytes
  raise FormatError, "bufferView #{view.index} exceeds its buffer" if start + required > bytes.bytesize

  if stride == output_element_size && storage_size == output_element_size
    return bytes.byteslice(start, output_element_size * count).dup
  end

  output = String.new(capacity: output_element_size * count, encoding: Encoding::BINARY)
  count.times do |element_index|
    element_start = start + (element_index * stride)
    append_element(output, bytes, element_start, type, component_size, storage_size)
  end
  output
end

.normalize(value, type) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/rgltf/accessor_reader.rb', line 128

def normalize(value, type)
  case type
  when :i8 then [value / 127.0, -1.0].max
  when :u8 then value / 255.0
  when :i16 then [value / 32_767.0, -1.0].max
  when :u16 then value / 65_535.0
  else value
  end
end

.packed(accessor) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rgltf/accessor_reader.rb', line 22

def packed(accessor)
  info = COMPONENTS_BY_SYMBOL.fetch(accessor.component_type)
  count = ELEMENT_COUNTS.fetch(accessor.type)
  element_size = info[:size] * count
  base = if accessor.buffer_view
           extract(
             accessor.buffer_view,
             accessor.byte_offset,
             accessor.count,
             accessor.type,
             info[:size],
             element_size
           )
         else
           "\0".b * (element_size * accessor.count)
         end

  return base.freeze unless accessor.sparse

  apply_sparse(base, accessor, element_size).freeze
end

.storage_element_size(type, component_size) ⇒ Object



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

def storage_element_size(type, component_size)
  dimension = MATRIX_DIMENSIONS[type]
  return ELEMENT_COUNTS.fetch(type) * component_size unless dimension && component_size < 4

  aligned_column_size = align4(dimension * component_size)
  aligned_column_size * dimension
end

.unpack(accessor, bytes) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/rgltf/accessor_reader.rb', line 44

def unpack(accessor, bytes)
  info = COMPONENTS_BY_SYMBOL.fetch(accessor.component_type)
  values = bytes.unpack("#{info[:pack]}*")
  values.map! { |value| normalize(value, accessor.component_type) } if accessor.normalized
  count = ELEMENT_COUNTS.fetch(accessor.type)
  count == 1 ? values : values.each_slice(count).to_a
end