Class: MagicaVoxel::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/magica_voxel/reader.rb

Overview

The Data Reader

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Reader

Returns a new instance of Reader.

Parameters:

  • the (IO)

    stream to read

Since:

  • 0.1.0



23
24
25
# File 'lib/magica_voxel/reader.rb', line 23

def initialize(io)
  @io = io
end

Class Method Details

.open(io) {|new(io)| ... } ⇒ Object

Open IO stream

Parameters:

  • the (IO|String)

    stream to read

Yields:

  • (new(io))

Since:

  • 0.1.0



14
15
16
17
18
# File 'lib/magica_voxel/reader.rb', line 14

def open(io)
  io = io.is_a?(IO) ? io : StringIO.new(io)
  yield new(io)
  io.close
end

Instance Method Details

#dictHash

Read Dictionary

Returns:

  • (Hash)

Since:

  • 0.1.0



50
51
52
# File 'lib/magica_voxel/reader.rb', line 50

def dict
  Hash[*Array.new(int32 * 2).map { string }]
end

#framesArray<MagicaVoxel::Frame>

Read Frames

Returns:

Since:

  • 0.1.0



77
78
79
# File 'lib/magica_voxel/reader.rb', line 77

def frames
  Array.new(int32).map { Frame.new(dict) }
end

#imapArray<Number>

Read Pattle Index MAP

Returns:

  • (Array<Number>)

Since:

  • 0.1.0



105
106
107
# File 'lib/magica_voxel/reader.rb', line 105

def imap
  Array.new(256).map { int32 }
end

#int32Number

Read int32

Returns:

  • (Number)

Since:

  • 0.1.0



32
33
34
# File 'lib/magica_voxel/reader.rb', line 32

def int32
  @io.read(4).unpack1('l')
end

#int32_arrayArray<Number>

Read Int32 Array

Returns:

  • (Array<Number>)

Since:

  • 0.1.0



59
60
61
# File 'lib/magica_voxel/reader.rb', line 59

def int32_array
  Array.new(int32).map { int32 }
end

#pattleArray<MagicaVoxel::Color]

Read Pattle

Returns:

Since:

  • 0.1.0



86
87
88
# File 'lib/magica_voxel/reader.rb', line 86

def pattle
  Array.new(256).map { rgba }
end

#rgbaMagicaVoxel::Color

Read RGBA

Returns:

Since:

  • 0.1.0



95
96
97
98
# File 'lib/magica_voxel/reader.rb', line 95

def rgba
  r, g, b, a = @io.read(4).unpack('CCCC')
  Color.new(r, g, b, a)
end

#shape_modelMagicaVoxel::Shape::Model

Read Shape’s Model

Returns:

Since:

  • 0.1.0



133
134
135
# File 'lib/magica_voxel/reader.rb', line 133

def shape_model
  Array.new(int32).map { Shape::Model.new(int32, dict) }
end

#stringString

Read String

Returns:

  • (String)

Since:

  • 0.1.0



41
42
43
# File 'lib/magica_voxel/reader.rb', line 41

def string
  @io.read(int32)
end

#string_arrayArray<String>

Read String Array

Returns:

  • (Array<String>)

Since:

  • 0.1.0



68
69
70
# File 'lib/magica_voxel/reader.rb', line 68

def string_array
  Array.new(int32).map { string }
end

#voxelMagicaVoxel::Voxel

Read Voxel

Returns:

Since:

  • 0.1.0



123
124
125
126
# File 'lib/magica_voxel/reader.rb', line 123

def voxel
  x, y, z, i = @io.read.unpack('cccC')
  Voxel.new(x, y, z, i)
end

#voxelsArray<MagicaVoxel::Voxel>

Read Voxels

Returns:

Since:

  • 0.1.0



114
115
116
# File 'lib/magica_voxel/reader.rb', line 114

def voxels
  Array.new(int32).map { voxel }
end