Class: Doom::Render::RayTracing::TextureAtlas

Inherits:
Object
  • Object
show all
Defined in:
lib/doom/render/ray_tracing/texture_atlas.rb

Overview

Packs palette-indexed flats and composite wall textures into one RGBA texture. Rectangle entries are [x, y, width, height, texel_w, texel_h].

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size:, textures:, flats:, palette:) ⇒ TextureAtlas

Returns a new instance of TextureAtlas.



11
12
13
14
15
16
# File 'lib/doom/render/ray_tracing/texture_atlas.rb', line 11

def initialize(size:, textures:, flats:, palette:)
  @size = size
  @textures = textures
  @flats = flats
  @palette = palette
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



9
10
11
# File 'lib/doom/render/ray_tracing/texture_atlas.rb', line 9

def size
  @size
end

Instance Method Details

#build(material_names) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/doom/render/ray_tracing/texture_atlas.rb', line 18

def build(material_names)
  pixels = "\0" * (@size * @size * 4)
  rectangles = {}
  x = y = row_height = 0
  material_names.uniq.each do |name|
    source = source_for(name)
    next unless source

    width, height, rgba = source
    if x + width > @size
      x = 0
      y += row_height
      row_height = 0
    end
    raise ArgumentError, 'ray texture atlas overflow' if y + height > @size

    height.times do |row|
      destination = (((y + row) * @size) + x) * 4
      pixels[destination, width * 4] = rgba.byteslice(row * width * 4, width * 4)
    end
    rectangles[name] = [x.to_f / @size, y.to_f / @size,
                        width.to_f / @size, height.to_f / @size,
                        width.to_f, height.to_f]
    x += width
    row_height = [row_height, height].max
  end
  [pixels, rectangles]
end