Class: Mittsu::MTLLoader::MaterialCreator

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

Instance Method Summary collapse

Constructor Details

#initialize(base_url, options = nil) ⇒ MaterialCreator

Returns a new instance of MaterialCreator.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mittsu/loaders/mtl_loader.rb', line 54

def initialize(base_url, options = nil)
  @base_url = base_url
  @options = options
  @material_info = {}
  @materials = {}
  @materials_array = []
  @name_lookup = {}

  @side = (@options || {}).fetch(:side, FrontSide)
  @wrap = (@options || {}).fetch(:wrap, RepeatWrapping)
end

Instance Method Details

#convert(materials_info) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mittsu/loaders/mtl_loader.rb', line 73

def convert(materials_info)
  return materials_info if !@options

  converted = {}

  materials_info.each do |mn, mat|
    covmat = converted[mn] ={}

    mat.each do |prop, value|
      save = true
      lprop = prop.to_s.downcase

      case lprop
      when 'kd', 'ka', 'ks'
        # Diffuse color, (color under white light) using RGB values

        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).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.to_f
        end
      end

      covmat[lprop] = value if save
    end
  end

  converted
end

#create(material_name) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/mittsu/loaders/mtl_loader.rb', line 135

def create(material_name)
  if @materials[material_name].nil?
    create_material(material_name)
  end

  @materials[material_name]
end

#get_as_arrayObject



126
127
128
129
130
131
132
133
# File 'lib/mittsu/loaders/mtl_loader.rb', line 126

def get_as_array
  @materials_info.keys.each_with_index do |mn, index|
    @materials_array[index] = create mn
    @name_lookup[mn] = index
  end

  @materials_array
end

#get_index(material_name) ⇒ Object



122
123
124
# File 'lib/mittsu/loaders/mtl_loader.rb', line 122

def get_index(material_name)
  @name_lookup[material_name]
end

#load_texture(url, mapping = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/mittsu/loaders/mtl_loader.rb', line 143

def load_texture(url, mapping = nil)
  loader = Loader::Handlers.get(url)

  if !loader.nil?
    texture = loader.load url
  else
    texture = Texture.new

    loader = ImageLoader.new
    image = loader.load url

    texture.image = ensure_power_of_two(image)
    texture.needs_update = true
  end

  texture.mapping = mapping unless mapping.nil?

  texture
end

#preloadObject



116
117
118
119
120
# File 'lib/mittsu/loaders/mtl_loader.rb', line 116

def preload
  @materials_info.each_key do |mn|
    create mn
  end
end

#set_materials(materials_info) ⇒ Object



66
67
68
69
70
71
# File 'lib/mittsu/loaders/mtl_loader.rb', line 66

def set_materials(materials_info)
  @materials_info = convert materials_info
  @materials = {}
  @materials_array = []
  @name_lookup = {}
end