Class: Assimp::MaterialProperty

Inherits:
FFI::Struct
  • Object
show all
Extended by:
StructAccessors
Defined in:
lib/assimp/material.rb

Constant Summary collapse

TEX_PROPERTIES =
[ MATKEY_TEXTURE,
MATKEY_UVWSRC,
MATKEY_TEXOP,
MATKEY_MAPPING,
MATKEY_TEXBLEND,
MATKEY_MAPPINGMODE_U,
MATKEY_MAPPINGMODE_V,
MATKEY_MAPPINGMODE_W,
MATKEY_TEXMAP_AXIS,
MATKEY_UVTRANSFORM,
MATKEY_TEXFLAGS ]
COLOR_PROPERTIES =
[ MATKEY_COLOR_DIFFUSE,
MATKEY_COLOR_AMBIENT,
MATKEY_COLOR_SPECULAR,
MATKEY_COLOR_EMISSIVE,
MATKEY_COLOR_TRANSPARENT,
MATKEY_COLOR_REFLECTIVE ]
STRING_PROPERTIES =
[ MATKEY_NAME,
MATKEY_GLOBAL_BACKGROUND_IMAGE,
MATKEY_TEXTURE ]
BOOL_PROPERTIES =
[ MATKEY_TWOSIDED, MATKEY_ENABLE_WIREFRAME ]
INTEGER_PROPERTIES =
BOOL_PROPERTIES +
                       [ MATKEY_UVWSRC,
MATKEY_SHADING_MODEL,
MATKEY_MAPPINGMODE_U,
MATKEY_MAPPINGMODE_V,
MATKEY_MAPPINGMODE_W,
MATKEY_BLEND_FUNC,
MATKEY_TEXFLAGS,
MATKEY_MAPPING,
MATKEY_TEXOP ]
FLOAT_PROPERTIES =
COLOR_PROPERTIES +
                     [ MATKEY_UVTRANSFORM,
MATKEY_TEXBLEND,
MATKEY_OPACITY,
MATKEY_SHININESS,
MATKEY_REFLECTIVITY,
MATKEY_REFRACTI,
MATKEY_SHININESS_STRENGTH,
MATKEY_BUMPSCALING,
MATKEY_TEXMAP_AXIS ]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StructAccessors

extended, has_ref?, struct_array_attr_accessor, struct_array_attr_checker, struct_array_attr_reader, struct_array_attr_writer, struct_attr_accessor, struct_attr_reader, struct_attr_writer, struct_ref_array_attr_accessor, struct_ref_array_attr_reader, struct_ref_array_attr_writer

Class Method Details

.property(key, val, semantic: 0, index: 0, type: nil) ⇒ Object



175
176
177
178
# File 'lib/assimp/material.rb', line 175

def self.property(key, val, semantic: 0, index: 0, type: nil)
  p = MaterialProperty::new
  p.set_property(key, val, semantic: semantic, index: index, type: type)
end

Instance Method Details

#dataObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/assimp/material.rb', line 292

def data
  case type
  when :String
    length = self[:data].read_uint
    self[:data].get_string(4, length)
  when :Integer
    if data_length == 4
      i = self[:data].read_int
      case key
      when *BOOL_PROPERTIES
        i != 0
      when MATKEY_SHADING_MODEL
        ShadingMode[i]
      when MATKEY_MAPPINGMODE_U, MATKEY_MAPPINGMODE_V, MATKEY_MAPPINGMODE_W
        TextureMapMode[i]
      when MATKEY_BLEND_FUNC
        BlendMode[i]
      when MATKEY_TEXFLAGS
        TextureFlags[i]
      when MATKEY_MAPPING
        TextureMapping[i]
      when MATKEY_TEXOP
        TextureOp[i]
      else
        i
      end
    elsif data_length > 4
      self[:data].read_array_of_int(data_length/4)
    else
      nil
    end
  when :Float
    if data_length == 4
      self[:data].read_float
    elsif data_length > 4
      if key.match("\\$clr\\.") && data_length == Color4D.size
        Color4D.new(self[:data])
      elsif key == MATKEY_TEXMAP_AXIS && data_length == Vector3D.size
        Vector3D.new(self[:data])
      elsif key == MATKEY_UVTRANSFORM && data_length == UVTransform.size
        UVTransform.new(self[:data])
      else
        self[:data].read_array_of_float(data_length/4)
      end
    else
      nil
    end
  when :Double
    if data_length == 8
      self[:data].read_double
    elsif data_length > 8
      if key.match("\\$clr\\.") && data_length == Color4D.size
        Color4D.new(self[:data])
      elsif key == MATKEY_TEXMAP_AXIS && data_length == Vector3D.size
        Vector3D.new(self[:data])
      elsif key == MATKEY_UVTRANSFORM && data_length == UVTransform.size
        UVTransform.new(self[:data])
      else
        self[:data].read_array_of_double(data_length/8)
      end
    else
      nil
    end
  when :Buffer
    self[:data].slice(0, data_length)
  else
    raise "Invalid type: #{type.inspect}!"
  end
end

#data=(val) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/assimp/material.rb', line 216

def data=(val)
  ptr = nil
  case type
  when :String
    length = val.bytesize
    ptr = FFI::MemoryPointer::new(length+4+1)
    ptr.write_uint(length)
    ptr.put_string(4, val)
  when :Integer
    if val.kind_of? Array
      ptr = FFI::MemoryPointer::new(:int, val.length)
      ptr.write_array_of_int(val)
    else
      i = nil
      case key
      when *BOOL_PROPERTIES
        if val.nil? || val == false || val == 0
          i = Assimp::FALSE
        else
          i = Assimp::TRUE
        end
      when MATKEY_SHADING_MODEL
        i = ShadingMode.to_native(val, nil)
      when MATKEY_MAPPINGMODE_U, MATKEY_MAPPINGMODE_V, MATKEY_MAPPINGMODE_W
        i = TextureMapMode.to_native(val, nil)
      when MATKEY_BLEND_FUNC
        i = BlendMode.to_native(val, nil)
      when MATKEY_TEXFLAGS
        i = TextureFlags.to_native(val, nil)
      when MATKEY_MAPPING
        i = TextureMapping.to_native(val, nil)
      when MATKEY_TEXOP
        i = TextureOp.to_native(val, nil)
      else
        i = val
      end
      ptr = FFI::MemoryPointer::new(:int)
      ptr.write_int(i)
    end
  when :Float
    if val.kind_of? Array
      ptr = FFI::MemoryPointer::new(:float, val.length)
      ptr.write_array_of_float(val)
    elsif val.kind_of? FFI::Struct
      ptr = FFI::MemoryPointer::new(val.class.size)
      ptr.write_array_of_uint8(val.pointer.read_array_of_uint8(val.class.size))
    else
      ptr = FFI::MemoryPointer::new(:float)
      ptr.write_float(val)
    end
  when :Double
    if val.kind_of? Array
      ptr = FFI::MemoryPointer::new(:double, val.length)
      ptr.write_array_of_double(val)
    elsif val.kind_of? FFI::Struct
      ptr = FFI::MemoryPointer::new(val.class.size)
      ptr.write_array_of_uint8(val.pointer.read_array_of_uint8(val.class.size))
    else
      ptr.write_double(val)
    end
  when :Buffer
    if val.kind_of? FFI::Pointer
      ptr = FFI::MemoryPointer::new(val.size)
      ptr.write_array_of_uint8(val.pointer.read_array_of_uint8(val.size))
    else
      ptr = FFI::MemoryPointer.from_string(val)
    end
  else
    raise ArgumentError::new("Invalid type: #{type.inspect}!")
  end
  @data = ptr
  self[:data] = ptr
  self.data_length = (ptr ? ptr.size : 0)
  val
end

#set_property(key, val, semantic: 0, index: 0, type: nil) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/assimp/material.rb', line 180

def set_property(key, val, semantic: 0, index: 0, type: nil)
  if type
    self.type = type
  else
    case key
    when *STRING_PROPERTIES
      self.type = :String
    when *INTEGER_PROPERTIES
      self.type = :Integer
    when *FLOAT_PROPERTIES
      if ENV["ASSIMP_DOUBLE_PRECISION"]
        self.type = :Double
      else
        self.type = :Float
      end
    else
      raise ArgumentError::new("Could not determine property type!")
    end
  end
  if semantic == 0 && TEX_PROPERTIES.include?(key)
    raise ArgumentError::new("Missing semantic for texture property!")
  elsif semantic != 0 && ! TEX_PROPERTIES.include?(key)
    raise ArgumentError::new("Semantic given for non texture property!")
  else
    self.semantic = semantic
  end
  if index != 0 && ! TEX_PROPERTIES.include?(key)
    raise ArgumentError::new("Index given for non texture property!")
  else
    self.index = index
  end
  self.key = key
  self.data = val
  self
end

#to_sObject



362
363
364
# File 'lib/assimp/material.rb', line 362

def to_s
  "#{key}: #{data}"
end