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.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mittsu/loaders/mtl_loader.rb', line 59

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



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
115
116
117
118
119
120
# File 'lib/mittsu/loaders/mtl_loader.rb', line 78

def convert(materials_info)
  return materials_info if !@options

  converted = {}

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

    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).any?(&: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
        end
      end

      covmat[lprop] = value if save
    end
  end

  converted
end

#create(material_name) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/mittsu/loaders/mtl_loader.rb', line 141

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

  @materials[material_name]
end

#get_as_arrayObject



132
133
134
135
136
137
138
139
# File 'lib/mittsu/loaders/mtl_loader.rb', line 132

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



128
129
130
# File 'lib/mittsu/loaders/mtl_loader.rb', line 128

def get_index(material_name)
  @name_lookup[material_name]
end

#load_texture(url, mapping = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/mittsu/loaders/mtl_loader.rb', line 149

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
    # loader.cross_origin = @cross_origin # TODO: ???
    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



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

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

#set_materials(materials_info) ⇒ Object



71
72
73
74
75
76
# File 'lib/mittsu/loaders/mtl_loader.rb', line 71

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