Class: Ra::Material

Inherits:
Object
  • Object
show all
Defined in:
lib/ra/material.rb

Overview

A material is used to define the properties of an object that impact the color applied. For example:

material = Ra::Material.new(
 base: Ra::Color.uniform(0.5),
 ambient: 0.2,
 diffuse: 0.5,
 specular: 0.7,
 shininess: 200,
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base:, ambient: 0.0, diffuse: 0.8, reflective: 0.0, specular: 0.2, shininess: 80) ⇒ Material

Returns a new instance of Material.

Parameters:

  • base (Ra::Color, Ra::Pattern:::Base)
  • ambient (Float) (defaults to: 0.0)

    between 0.0 and 1.0

  • diffuse (Float) (defaults to: 0.8)

    between 0.0 and 1.0

  • reflective (Float) (defaults to: 0.0)

    between 0.0 and 1.0

  • specular (Float) (defaults to: 0.2)

    between 0.0 and 1.0

  • shininess (Integer) (defaults to: 80)

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ra/material.rb', line 44

def initialize(base:, ambient: 0.0, diffuse: 0.8, reflective: 0.0, specular: 0.2, shininess: 80)
  raise ArgumentError, "ambient=#{ambient} must be between 0 and 1" unless ambient.between?(0, 1)
  raise ArgumentError, "ambient=#{diffuse} must be between 0 and 1" unless diffuse.between?(0, 1)
  raise ArgumentError, "ambient=#{reflective} must be between 0 and 1" unless reflective.between?(0, 1)
  raise ArgumentError, "specular=#{specular} must be between 0 and 1" unless specular.between?(0, 1)

  @base = base
  @ambient = ambient
  @diffuse = diffuse
  @reflective = reflective
  @specular = specular
  @shininess = shininess
end

Instance Attribute Details

#ambientFloat

Returns:

  • (Float)


20
21
22
# File 'lib/ra/material.rb', line 20

def ambient
  @ambient
end

#baseRa::Color, Ra::Pattern::Base



16
17
18
# File 'lib/ra/material.rb', line 16

def base
  @base
end

#diffuseFloat

Returns:

  • (Float)


24
25
26
# File 'lib/ra/material.rb', line 24

def diffuse
  @diffuse
end

#reflectiveFloat

Returns:

  • (Float)


28
29
30
# File 'lib/ra/material.rb', line 28

def reflective
  @reflective
end

#shininessInteger

Returns:

  • (Integer)


36
37
38
# File 'lib/ra/material.rb', line 36

def shininess
  @shininess
end

#specularFloat

Returns:

  • (Float)


32
33
34
# File 'lib/ra/material.rb', line 32

def specular
  @specular
end

Instance Method Details

#color(point:) ⇒ Ra::Color

Parameters:

  • point (Vector)

Returns:



60
61
62
63
64
# File 'lib/ra/material.rb', line 60

def color(point:)
  return base if base.is_a?(Color)

  base.color(point:)
end