Class: Mittsu::Material

Inherits:
Object
  • Object
show all
Includes:
EventDispatcher
Defined in:
lib/mittsu/materials/material.rb,
lib/mittsu/renderers/opengl/materials/material.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventDispatcher

#add_event_listener, #dispatch_event, #has_event_listener, #remove_event_listener

Constructor Details

#initializeMaterial

Returns a new instance of Material.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mittsu/materials/material.rb', line 17

def initialize
  super
  @id = (@@id ||= 1).tap { @@id += 1 }

  @uuid = SecureRandom.uuid

  @name = ''
  @type = 'Material'

  @side = FrontSide

  @opacity = 1.0
  @transparent = false

  @blending = NormalBlending

  @blend_src = SrcAlphaFactor
  @blend_dst = OneMinusSrcAlphaFactor
  @blend_equation = AddEquation
  @blend_src_alpha = nil
  @blend_dst_alpha = nil
  @blend_equation_alpha = nil

  @depth_test = true
  @depth_write = true

  @color_write = true

  @polygon_offset = false
  @polygon_offset_factor = 0
  @polygon_offset_units = 0

  @alpha_test = 0

  # TODO: remove this maybe???
  @overdraw = 0 # Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer

  @visible = true

  @_needs_update = true
end

Instance Attribute Details

#alpha_mapObject

Returns the value of attribute alpha_map.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def alpha_map
  @alpha_map
end

#alpha_testObject

Returns the value of attribute alpha_test.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def alpha_test
  @alpha_test
end

#attributesObject

Returns the value of attribute attributes.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def attributes
  @attributes
end

#blend_dstObject

Returns the value of attribute blend_dst.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def blend_dst
  @blend_dst
end

#blend_dst_alphaObject

Returns the value of attribute blend_dst_alpha.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def blend_dst_alpha
  @blend_dst_alpha
end

#blend_equationObject

Returns the value of attribute blend_equation.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def blend_equation
  @blend_equation
end

#blend_equation_alphaObject

Returns the value of attribute blend_equation_alpha.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def blend_equation_alpha
  @blend_equation_alpha
end

#blend_srcObject

Returns the value of attribute blend_src.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def blend_src
  @blend_src
end

#blend_src_alphaObject

Returns the value of attribute blend_src_alpha.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def blend_src_alpha
  @blend_src_alpha
end

#blendingObject

Returns the value of attribute blending.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def blending
  @blending
end

#bump_mapObject

Returns the value of attribute bump_map.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def bump_map
  @bump_map
end

#colorObject

Returns the value of attribute color.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def color
  @color
end

#color_writeObject

Returns the value of attribute color_write.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def color_write
  @color_write
end

#combineObject

Returns the value of attribute combine.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def combine
  @combine
end

#default_attribute_valuesObject

Returns the value of attribute default_attribute_values.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def default_attribute_values
  @default_attribute_values
end

#definesObject

Returns the value of attribute defines.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def defines
  @defines
end

#depth_testObject

Returns the value of attribute depth_test.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def depth_test
  @depth_test
end

#depth_writeObject

Returns the value of attribute depth_write.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def depth_write
  @depth_write
end

#env_mapObject

Returns the value of attribute env_map.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def env_map
  @env_map
end

#fogObject

Returns the value of attribute fog.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def fog
  @fog
end

#fragment_shaderObject

Returns the value of attribute fragment_shader.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def fragment_shader
  @fragment_shader
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/mittsu/materials/material.rb', line 7

def id
  @id
end

#light_mapObject

Returns the value of attribute light_map.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def light_map
  @light_map
end

#lightsObject

Returns the value of attribute lights.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def lights
  @lights
end

#mapObject

Returns the value of attribute map.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def map
  @map
end

#metalObject

Returns the value of attribute metal.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def metal
  @metal
end

#morph_normalsObject

Returns the value of attribute morph_normals.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def morph_normals
  @morph_normals
end

#morph_targetsObject

Returns the value of attribute morph_targets.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def morph_targets
  @morph_targets
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def name
  @name
end

#normal_mapObject

Returns the value of attribute normal_map.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def normal_map
  @normal_map
end

#opacityObject

Returns the value of attribute opacity.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def opacity
  @opacity
end

#overdrawObject

Returns the value of attribute overdraw.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def overdraw
  @overdraw
end

#polygon_offsetObject

Returns the value of attribute polygon_offset.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def polygon_offset
  @polygon_offset
end

#polygon_offset_factorObject

Returns the value of attribute polygon_offset_factor.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def polygon_offset_factor
  @polygon_offset_factor
end

#polygon_offset_unitsObject

Returns the value of attribute polygon_offset_units.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def polygon_offset_units
  @polygon_offset_units
end

#programObject

Returns the value of attribute program.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def program
  @program
end

#reflectivityObject

Returns the value of attribute reflectivity.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def reflectivity
  @reflectivity
end

#refraction_ratioObject

Returns the value of attribute refraction_ratio.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def refraction_ratio
  @refraction_ratio
end

#shaderObject (readonly)

Returns the value of attribute shader.



10
11
12
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 10

def shader
  @shader
end

#shadingObject

Returns the value of attribute shading.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def shading
  @shading
end

#shadow_passObject

TODO: init_shader for these material-types MeshDepthMaterial => :depth, # TODO… MeshNormalMaterial => :normal, # TODO… LineDashedMaterial => :dashed, # TODO… PointCloudMaterial => :particle_basic # TODO…



9
10
11
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 9

def shadow_pass
  @shadow_pass
end

#sideObject

Returns the value of attribute side.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def side
  @side
end

#size_attenuationObject

Returns the value of attribute size_attenuation.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def size_attenuation
  @size_attenuation
end

#skinningObject

Returns the value of attribute skinning.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def skinning
  @skinning
end

#specular_mapObject

Returns the value of attribute specular_map.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def specular_map
  @specular_map
end

#transparentObject

Returns the value of attribute transparent.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def transparent
  @transparent
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/mittsu/materials/material.rb', line 7

def type
  @type
end

#uniformsObject

Returns the value of attribute uniforms.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def uniforms
  @uniforms
end

#uniforms_listObject (readonly)

Returns the value of attribute uniforms_list.



10
11
12
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 10

def uniforms_list
  @uniforms_list
end

#uuidObject (readonly)

Returns the value of attribute uuid.



7
8
9
# File 'lib/mittsu/materials/material.rb', line 7

def uuid
  @uuid
end

#vertex_colorsObject

Returns the value of attribute vertex_colors.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def vertex_colors
  @vertex_colors
end

#vertex_shaderObject

Returns the value of attribute vertex_shader.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def vertex_shader
  @vertex_shader
end

#visibleObject

Returns the value of attribute visible.



9
10
11
# File 'lib/mittsu/materials/material.rb', line 9

def visible
  @visible
end

#wireframeObject

Returns the value of attribute wireframe.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def wireframe
  @wireframe
end

#wrap_aroundObject

Returns the value of attribute wrap_around.



13
14
15
# File 'lib/mittsu/materials/material.rb', line 13

def wrap_around
  @wrap_around
end

Instance Method Details

#clear_custom_attributesObject



45
46
47
48
49
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 45

def clear_custom_attributes
  attributes.each do |attribute|
    attribute.needs_update = false
  end
end

#clone(material = Material.new) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mittsu/materials/material.rb', line 110

def clone(material = Material.new)
  material.name = @name
  material.side = @side
  material.opacity = @opacity
  material.transparent = @transparent
  material.blending = @blending
  material.blend_src = @blend_src
  material.blend_dst = @blend_dst
  material.blend_equation = @blend_equation
  material.blend_src_alpha = @blend_src_alpha
  material.blend_dst_alpha = @blend_dst_alpha
  material.blend_equation_alpha = @blend_equation_alpha
  material.depth_test = @depth_test
  material.depth_write = @depth_write
  material.color_write = @color_write
  material.polygon_offset = @polygon_offset
  material.polygon_offset_factor = @polygon_offset_factor
  material.polygon_offset_units = @polygon_offset_units
  material.alpha_test = @alpha_test
  material.overdraw = @overdraw
  material.visible = @visible
end

#custom_attributes_dirty?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 51

def custom_attributes_dirty?
  attributes.each do |attribute|
    return true if attribute.needs_update
  end
  false
end

#disposeObject



137
138
139
# File 'lib/mittsu/materials/material.rb', line 137

def dispose
  dispatch_event type: :dispose
end

#init(lights, fog, object, renderer) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 12

def init(lights, fog, object, renderer)
  @renderer = renderer

  add_event_listener(:dispose, @renderer.method(:on_material_dispose))

  init_shader

  self.program = find_or_create_program(lights, fog, object)

  count_supported_morph_attributes(program.attributes)

  @uniforms_list = get_uniforms_list
end

#needs_camera_position_uniform?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 62

def needs_camera_position_uniform?
  env_map
end

#needs_face_normals?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 41

def needs_face_normals?
  shading == FlatShading
end

#needs_lights?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 70

def needs_lights?
  lights
end

#needs_update=(value) ⇒ Object



63
64
65
66
# File 'lib/mittsu/materials/material.rb', line 63

def needs_update=(value)
  update if value
  @_needs_update = value
end

#needs_update?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/mittsu/materials/material.rb', line 59

def needs_update?
  @_needs_update
end

#needs_view_matrix_uniform?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 66

def needs_view_matrix_uniform?
  skinning
end

#refresh_uniforms(_) ⇒ Object



58
59
60
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 58

def refresh_uniforms(_)
  # NOOP
end

#set(renderer) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mittsu/renderers/opengl/materials/material.rb', line 26

def set(renderer)
  @renderer = renderer

  if transparent
    @renderer.state.set_blending(blending, blend_equation, blend_src, blend_dst, blend_equation_alpha, blend_src_alpha, blend_dst_alpha)
  else
    @renderer.state.set_blending(NoBlending)
  end

  @renderer.state.set_depth_test(depth_test)
  @renderer.state.set_depth_write(depth_write)
  @renderer.state.set_color_write(color_write)
  @renderer.state.set_polygon_offset(polygon_offset, polygon_offset_factor, polygon_offset_units)
end

#set_values(values = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mittsu/materials/material.rb', line 68

def set_values(values = nil)
  return if values.nil?

  values.each do |(key, new_value)|
    if new_value.nil?
      puts "WARNING: Mittsu::Material: #{key} parameter is undefined"
      next
    end

    if has_property? key
      current_value = get_property(key)

      if current_value.is_a? Color
        current_value.set(new_value)
      elsif current_value.is_a?(Vector3) && new_value.is_a?(Vector3)
        current_value.copy(new_value)
      else
        set_property(key, new_value)
      end
    end
  end
end

#to_jsonObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mittsu/materials/material.rb', line 91

def to_json
  output = {
    metadata: {
      version: 4.2,
      type: 'material',
      generator: 'MaterialExporter'
    },
    uuid: @uuid,
    type: @type
  }

  output[:name] = @name if !@name.nil? && !@name.empty?

  output[:opacity] = @opacity if @opacity < 1.0
  output[:transparent] = @transparent if @transparent
  output[:wireframe] = @wireframe if @wireframe
  output
end

#updateObject



133
134
135
# File 'lib/mittsu/materials/material.rb', line 133

def update
  dispatch_event type: :update
end