Class: Mittsu::MTLLoader

Inherits:
Object
  • Object
show all
Includes:
EventDispatcher
Defined in:
lib/mittsu/loaders/mtl_loader.rb

Defined Under Namespace

Classes: MaterialCreator

Instance Method Summary collapse

Methods included from EventDispatcher

#add_event_listener, #dispatch_event, #has_event_listener, #remove_event_listener

Constructor Details

#initialize(base_url, options = {}) ⇒ MTLLoader

Returns a new instance of MTLLoader.



5
6
7
8
# File 'lib/mittsu/loaders/mtl_loader.rb', line 5

def initialize(base_url, options = {})
  @base_url = base_url
  @options = options
end

Instance Method Details

#load(url) ⇒ Object



10
11
12
13
14
15
# File 'lib/mittsu/loaders/mtl_loader.rb', line 10

def load(url)
  loader = FileLoader.new

  text = loader.load File.join(@base_url, url)
  parse(text)
end

#parse(text) ⇒ Object



17
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
46
47
48
49
50
51
# File 'lib/mittsu/loaders/mtl_loader.rb', line 17

def parse(text)
  lines = text.split("\n")
  info = {}
  delimiter_pattern = /\s+/
  materials_info = {}

  lines.each do |line|
	line = line.strip

	next if line.empty? || line.start_with?('#')

	pos = line.index(' ')

	key = ( pos >= 0 ) ? line[0...pos] : line
	key = key.downcase

	value = ( pos >= 0 ) ? line[pos + 1..-1] : ""
	value = value.strip

	if key == "newmtl"
		info = { name: value };
		materials_info[value] = info
	elsif info
		if key == "ka" || key == "kd" || key == "ks"
			info[key] = value.split(delimiter_pattern).take(3).map(&:to_f)
		else
			info[key] = value
		end
	end
  end

  MaterialCreator.new(@base_url, @options).tap do |material_creator|
    material_creator.set_materials(materials_info)
  end
end