-
Notifications
You must be signed in to change notification settings - Fork 0
/
material.go
132 lines (109 loc) · 2.69 KB
/
material.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
121
122
123
124
125
126
127
128
129
130
131
132
package raytracing
import (
"math"
"math/rand"
)
type Material interface {
scatter(hr hitRecord) (ray, bool)
attenuation() Color
}
type lambertian struct {
albedo Color
}
func NewLambertian(c Color) Material {
return lambertian{albedo: c}
}
func (l lambertian) scatter(hr hitRecord) (ray, bool) {
phi := 2 * math.Pi * rand.Float64()
z := 2*rand.Float64() - 1
r := (1 - z*z)
random := NewVector(r*math.Cos(phi), r*math.Sin(phi), z)
return newRay(hr.point, hr.normal.add(random)), true
}
func (l lambertian) attenuation() Color {
return l.albedo
}
type metal struct {
albedo Color
fuzziness float64
}
func NewMetal(c Color, f float64) Material {
if f <= 0 {
f = 0
}
if f >= 1 {
f = 1
}
return metal{albedo: c, fuzziness: f}
}
func (m metal) scatter(hr hitRecord) (ray, bool) {
f := randomInUnitSphere().mul(m.fuzziness)
reflected := reflect(hr.incident.direction, hr.normal).add(f)
if reflected.dot(hr.normal) < 0 {
return ray{}, false
}
return newRay(hr.point, reflected), true
}
func randomInUnitSphere() Vector {
x, y, z := 0.0, 0.0, 0.0
for true {
x = 2*rand.Float64() - 1
y = 2*rand.Float64() - 1
z = 2*rand.Float64() - 1
if x*x+y*y+z*z < 1 {
break
}
}
return NewVector(x, y, z)
}
func (m metal) attenuation() Color {
return m.albedo
}
type dielectric struct {
refractiveIndex float64
}
func NewDielectric(idx float64) Material {
return dielectric{refractiveIndex: idx}
}
func (d dielectric) scatter(hr hitRecord) (ray, bool) {
var relIdx float64
if hr.incident.direction.dot(hr.normal) < 0 {
relIdx = 1.0 / d.refractiveIndex
} else {
relIdx = d.refractiveIndex
}
in := hr.incident.direction
orthogonal := in.sub(hr.normal.mul(in.dot(hr.normal))).mul(relIdx)
if in.norm() < orthogonal.norm() {
reflected := reflect(hr.incident.direction, hr.normal)
return newRay(hr.point, reflected), true
}
if rand.Float64() < schlick(in, hr.normal, relIdx) {
reflected := reflect(hr.incident.direction, hr.normal)
return newRay(hr.point, reflected), true
}
var parallel Vector
if hr.incident.direction.dot(hr.normal) < 0 {
parallel = hr.normal.mul(-math.Sqrt(in.norm() - orthogonal.norm()))
} else {
parallel = hr.normal.mul(math.Sqrt(in.norm() - orthogonal.norm()))
}
refracted := orthogonal.add(parallel)
return newRay(hr.point, refracted), true
}
func (d dielectric) attenuation() Color {
return NewColor(1, 1, 1)
}
func reflect(in Vector, normal Vector) Vector {
parallel := normal.mul(in.neg().dot(normal))
return in.add(parallel.mul(2))
}
func schlick(in, normal Vector, relIdx float64) float64 {
cos := in.dot(normal) / in.length()
if cos < 0 {
cos = -cos
}
r := (relIdx - 1) / (relIdx + 1)
r0 := r * r
return r0 + (1-r0)*math.Pow(1-cos, 5)
}