Class: Bestguigui::GLTFBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/bestguigui_lab/gltf_buffer.rb

Overview

Parse un conteneur .glb (glTF binaire, un seul fichier auto-suffisant) : en-tete (magic/version/longueur) puis des chunks JSON et BIN — le seul buffer supporte est celui-la, embarque, jamais un fichier .bin externe reference par URI. #read_accessor lit un tableau glTF (positions, indices, temps de keyframes...) depuis le chunk BIN, en suivant bufferView/accessor comme le decrit la specification.

Constant Summary collapse

COMPONENT_TYPES =

component type (glTF) => [directive String#unpack, octets]

{
    5120 => ['c', 1], 5121 => ['C', 1], 5122 => ['s<', 2],
    5123 => ['v', 2], 5125 => ['V', 4], 5126 => ['e', 4]
}.freeze
TYPE_COMPONENT_COUNTS =
{ 'SCALAR' => 1, 'VEC2' => 2, 'VEC3' => 3, 'VEC4' => 4, 'MAT4' => 16 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ GLTFBuffer

Returns a new instance of GLTFBuffer.



22
23
24
25
26
# File 'lib/bestguigui_lab/gltf_buffer.rb', line 22

def initialize(path)
    chunks = read_chunks(File.binread(path))
    @json = JSON.parse(chunks.fetch('JSON'))
    @bin = chunks['BIN']
end

Instance Attribute Details

#json ⇒ Object (readonly)

Returns the value of attribute json.



20
21
22
# File 'lib/bestguigui_lab/gltf_buffer.rb', line 20

def json
  @json
end

Instance Method Details

#read_accessor(index) ⇒ Object



28
29
30
31
32
33
# File 'lib/bestguigui_lab/gltf_buffer.rb', line 28

def read_accessor(index)
    accessor = @json['accessors'][index]
    format, size, components, stride, base = accessor_layout(accessor)

    Array.new(accessor['count']) { |i| read_element(base + (i * stride), format, size, components) }
end

#read_buffer_view(index) ⇒ Object

Sous-chaine brute du chunk BIN, pour une image embarquee (cf. GLTFModel#load_image) — pas un accessor, directement bufferView.



37
38
39
40
# File 'lib/bestguigui_lab/gltf_buffer.rb', line 37

def read_buffer_view(index)
    view = @json['bufferViews'][index]
    @bin[(view['byteOffset'] || 0), view['byteLength']]
end