Class: Ogre::RMaterial

Inherits:
Object show all
Defined in:
lib/shattered_view/ogrerb/rmaterial.rb

Overview

Materials are parsed through erb as .rmaterials and parsed when a material is created using the create :material function of View

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rmaterial) ⇒ RMaterial

Returns a new instance of RMaterial.



43
44
45
# File 'lib/shattered_view/ogrerb/rmaterial.rb', line 43

def initialize(rmaterial)
 	@rmaterial = rmaterial
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/shattered_view/ogrerb/rmaterial.rb', line 64

def method_missing(name, *args)
  # Check if the method is a setter
  if name.to_s[-1].chr == "="
    name = name.to_s[0...-1].to_sym
    # Set the instance variable to the sent variable
    ivar = "@#{ name }".to_sym
    instance_variable_set(ivar, args[0])
  else
    begin
      return eval("@#{name}")
    rescue StandardError => bang
      raise NoMethodError, "Rmaterial could not find the #{name} attribute: #{bang.message}"
    end
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/shattered_view/ogrerb/rmaterial.rb', line 32

def name
  @name
end

#resultObject (readonly)

Returns the value of attribute result.



32
33
34
# File 'lib/shattered_view/ogrerb/rmaterial.rb', line 32

def result
  @result
end

Class Method Details

.load(file) {|rmaterial| ... } ⇒ Object

Used to create an rmaterial with a block. Returns an Ogre material

Yields:

  • (rmaterial)


36
37
38
39
40
41
# File 'lib/shattered_view/ogrerb/rmaterial.rb', line 36

def self.load(file)
 	rmaterial = self.new(file)
 	yield rmaterial
 	rmaterial.parse
 	return rmaterial.create_ogre_material
end

Instance Method Details

#create_ogre_materialObject

Loads the rmaterial from Ogre. Must call parse first.



57
58
59
60
61
62
# File 'lib/shattered_view/ogrerb/rmaterial.rb', line 57

def create_ogre_material
	unless(MaterialManager.instance.resource_exists(name.to_s))
  	MaterialManager.instance.parse_script(result, "General")
	end
	return MaterialManager.instance.get_by_name(name.to_s)
end

#evaluate(template) ⇒ Object



79
80
81
82
# File 'lib/shattered_view/ogrerb/rmaterial.rb', line 79

def evaluate(template)
  evaluator = ERB.new(template)
  return @result ||= evaluator.result(binding)
end

#parseObject

Parses through ERB to create a ogre-compatible material



48
49
50
51
52
53
54
# File 'lib/shattered_view/ogrerb/rmaterial.rb', line 48

def parse
  begin
    evaluate(File.open(@rmaterial).readlines.join(''))
  rescue Errno::ENOENT => bang
    raise Errno::ENOENT, "RMaterial for '#{name}' not found: #{bang.message}"
  end
end

#tangent_spaceObject



83
84
85
# File 'lib/shattered_view/ogrerb/rmaterial.rb', line 83

def tangent_space
  false
end