Class: MagicaVoxel::Chunk

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/magica_voxel/chunk.rb

Overview

The MagicaVoxel Chunk

Since:

  • 0.1.0

Direct Known Subclasses

Camera, Main, Material, Model, Node, Note, Object, Pattle, PattleMap, Size

Constant Summary collapse

TYPES =

Chunk Type Mapping

Since:

  • 0.1.0

{
  'MAIN' => 'Main',
  'SIZE' => 'Size',
  'XYZI' => 'Model',
  'RGBA' => 'Pattle',
  'nTRN' => 'Transform',
  'nGRP' => 'Group',
  'nSHP' => 'Shape',
  'MATL' => 'Material',
  'LAYR' => 'Layer',
  'rOBJ' => 'Object',
  'rCAM' => 'Camera',
  'NOTE' => 'Note',
  'IMAP' => 'PattleMap'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, children) ⇒ Chunk

Returns a new instance of Chunk.

Parameters:

  • content (String)

    content data

  • children (String)

    children data

Since:

  • 0.1.0



69
70
71
72
73
74
75
76
# File 'lib/magica_voxel/chunk.rb', line 69

def initialize(content, children)
  @children = Chunk.read(StringIO.new(children))
  Reader.open(content) do |reader|
    layout.each do |key, type|
      instance_variable_set(:"@#{key}", reader.send(type))
    end
  end
end

Class Method Details

.header(io) ⇒ Array<String|Number>

Read Chunk Header

Parameters:

  • data (IO)

    the stream to read

Returns:

  • (Array<String|Number>)

    type, content bytes, children bytes

Since:

  • 0.1.0



35
36
37
38
39
40
41
# File 'lib/magica_voxel/chunk.rb', line 35

def header(io)
  [
    io.read(4),
    io.read(4).unpack1('L'),
    io.read(4).unpack1('L')
  ]
end

.read(io) ⇒ Enumerator<Chunk>

Read Chunk

Parameters:

  • data (IO)

    the stream to read

Returns:

  • (Enumerator<Chunk>)

    the chunk object

Since:

  • 0.1.0



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/magica_voxel/chunk.rb', line 16

def read(io)
  return enum_for(:read, io) unless defined?(yield)

  until io.eof?
    type, n_content, n_children = header(io)
    klass_name = Chunk::TYPES[type] || 'Chunk'
    yield MagicaVoxel
      .const_get(klass_name)
      .new(io.read(n_content), io.read(n_children))
  end
end

Instance Method Details

#each(&block) ⇒ Object

:nodoc:

Since:

  • 0.1.0



81
82
83
# File 'lib/magica_voxel/chunk.rb', line 81

def each(&block)
  @children.each(&block)
end