lib/mittsu/loaders/mtl_loader.rb in mittsu-0.1.1 vs lib/mittsu/loaders/mtl_loader.rb in mittsu-0.1.2
- old
+ new
@@ -1,18 +1,16 @@
module Mittsu
class MTLLoader
include EventDispatcher
- def initialize(base_url, options = {}) # TODO: cross_origin?
+ def initialize(base_url, options = {})
@base_url = base_url
@options = options
- # @cross_origin = cross_origin
end
def load(url)
loader = FileLoader.new
- # loader.cross_origin = @cross_origin
text = loader.load File.join(@base_url, url)
parse(text)
end
@@ -34,18 +32,15 @@
value = ( pos >= 0 ) ? line[pos + 1..-1] : ""
value = value.strip
if key == "newmtl"
- # New material
-
info = { name: value };
materials_info[value] = info
elsif info
if key == "ka" || key == "kd" || key == "ks"
- ss = value.split(delimiter_pattern).take(3)
- info[key] = [ss[0].to_f, ss[1].to_f, ss[2].to_f]
+ info[key] = value.split(delimiter_pattern).take(3).map(&:to_f)
else
info[key] = value
end
end
end
@@ -79,12 +74,11 @@
return materials_info if !@options
converted = {}
materials_info.each do |mn, mat|
- covmat = {}
- converted[mn] = covmat
+ covmat = converted[mn] ={}
mat.each do |prop, value|
save = true
lprop = prop.to_s.downcase
@@ -95,22 +89,22 @@
if @options && @options[:normalize_rgb]
value = [value[0] / 255.0, value[1] / 255.0, value[2] / 255.0]
end
if @options && @options[:ignore_zero_rgbs]
- if value.take(3).any?(&:zero?)
+ if value.take(3).all?(&:zero?)
# ignore
save = false
end
end
when 'd'
# According to MTL format (http://paulbourke.net/dataformats/mtl/):
# d is dissolve for current material
# factor of 1.0 is fully opaque, a factor of 0 is fully dissolved (completely transparent)
if @options && @options[:invert_transparency]
- value = 1.0 - value
+ value = 1.0 - value.to_f
end
end
covmat[lprop] = value if save
end
@@ -153,10 +147,9 @@
texture = loader.load url
else
texture = Texture.new
loader = ImageLoader.new
- # loader.cross_origin = @cross_origin # TODO: ???
image = loader.load url
texture.image = ensure_power_of_two(image)
texture.needs_update = true
end