Class: Mittsu::GLTFExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/mittsu/gltf/exporter.rb

Constant Summary collapse

COMPONENT_TYPES =
{
  # 8 bit
  byte: 5120,
  unsigned_byte: 5121,
  # 16 bit
  short: 5122,
  unsigned_short: 5123,
  # 32 bit
  unsigned_int: 5125,
  float: 5126
}.freeze
GPU_BUFFER_TYPES =
{
  array_buffer: 34962,
  element_array_buffer: 34963
}
ELEMENT_TYPES =
[
  "SCALAR",
  "VEC2",
  "VEC3",
  "VEC4",
  "MAT2",
  "MAT3",
  "MAT4"
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GLTFExporter

Returns a new instance of GLTFExporter.



33
34
35
36
37
38
39
40
41
# File 'lib/mittsu/gltf/exporter.rb', line 33

def initialize(options = {})
  @node_indexes = []
  @nodes = []
  @buffers = []
  @meshes = []
  @buffer_views = []
  @accessors = []
  @binary_buffer = nil
end

Instance Method Details

#export(object, filename, mode: :ascii) ⇒ Object Also known as: parse



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mittsu/gltf/exporter.rb', line 43

def export(object, filename, mode: :ascii)
  initialize
  object.traverse do |obj|
    @node_indexes << add_mesh(obj, mode: mode) if obj.is_a? Mittsu::Mesh
  end
  json = Jbuilder.new do |json|
    json.asset do
      json.generator "Mittsu-GLTF"
      json.version "2.0"
    end
    json.scene 0
    json.scenes [{
      nodes: @node_indexes
    }]
    json.nodes { json.array! @nodes }
    json.meshes { json.array! @meshes }
    json.buffers { json.array! @buffers }
    json.bufferViews { json.array! @buffer_views }
    json.accessors { json.array! @accessors }
  end.target!
  case mode
  when :ascii
    File.write(filename, json)
  when :binary
    File.open(filename, "wb") do |file|
      size = 12 +
        8 + json.length + padding_required(json, stride: 4) +
        8 + @binary_buffer.length + padding_required(@binary_buffer, stride: 4)
      file.write("glTF")
      file.write([2, size].pack("L<*"))
      write_chunk(file, :json, json)
      write_chunk(file, :binary, @binary_buffer)
    end
  else
    raise ArgumentError "Invalid output mode #{mode}"
  end
end