-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaterial.rs
276 lines (245 loc) · 7.35 KB
/
material.rs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
215
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
use std::sync::Arc;
use crate::hit::HitRecord;
use crate::math::float::Float;
use crate::ray::Ray;
use crate::texture::Sample;
use crate::texture::Texture;
use crate::vec3::Vec3;
use crate::Rng;
pub trait MaterialTrait {
fn scatter<R: Rng>(
&self,
ray: &Ray,
hit_record: &HitRecord<'_>,
rng: &mut R,
) -> Option<(Ray, Vec3)>;
#[allow(unused_variables)]
fn emit(&self, u: Float, v: Float, p: &Vec3) -> Vec3 {
Vec3::zero()
}
}
#[derive(Debug, Clone)]
pub enum Material {
Lambertian(Lambertian),
Metal(Metal),
Dielectric(Dielectric),
DiffuseLight(DiffuseLight),
Isotropic(Isotropic),
}
impl Material {
pub fn lambertian(texture: Texture) -> Arc<Material> {
Arc::new(Material::Lambertian(Lambertian::new(texture)))
}
pub fn metal(albedo: Vec3, fuzz: Float) -> Arc<Material> {
Arc::new(Material::Metal(Metal::new(albedo, fuzz)))
}
pub fn dielectric(ref_idx: Float) -> Arc<Material> {
Arc::new(Material::Dielectric(Dielectric::new(ref_idx)))
}
pub fn diffuse_light(texture: Texture) -> Arc<Material> {
Arc::new(Material::DiffuseLight(DiffuseLight::new(texture)))
}
// Isotropic is only used by ConstantMedium which doesn't share it's phase function (material)
// with others so we don't need the `Arc<T>`.
pub fn isotropic(albedo: Texture) -> Material {
Material::Isotropic(Isotropic::new(albedo))
}
}
impl MaterialTrait for Material {
fn scatter<R: Rng>(
&self,
ray: &Ray,
hit_record: &HitRecord<'_>,
rng: &mut R,
) -> Option<(Ray, Vec3)> {
match self {
Material::Lambertian(lambertian) => lambertian.scatter(ray, hit_record, rng),
Material::Metal(metal) => metal.scatter(ray, hit_record, rng),
Material::Dielectric(dielectric) => dielectric.scatter(ray, hit_record, rng),
Material::DiffuseLight(diffuse_light) => diffuse_light.scatter(ray, hit_record, rng),
Material::Isotropic(isotropic) => isotropic.scatter(ray, hit_record, rng),
}
}
fn emit(&self, u: Float, v: Float, p: &Vec3) -> Vec3 {
match self {
Material::Lambertian(lambertian) => lambertian.emit(u, v, p),
Material::Metal(metal) => metal.emit(u, v, p),
Material::Dielectric(dielectric) => dielectric.emit(u, v, p),
Material::DiffuseLight(diffuse_light) => diffuse_light.emit(u, v, p),
Material::Isotropic(isotropic) => isotropic.emit(u, v, p),
}
}
}
#[derive(Debug, Clone)]
pub struct Lambertian {
texture: Texture,
}
impl Lambertian {
pub fn new(texture: Texture) -> Lambertian {
Lambertian { texture }
}
}
impl MaterialTrait for Lambertian {
fn scatter<R: Rng>(
&self,
ray: &Ray,
hit_record: &HitRecord<'_>,
rng: &mut R,
) -> Option<(Ray, Vec3)> {
let target: Vec3 = hit_record.p + hit_record.normal + random_in_unit_sphere(rng);
let scattered: Ray = Ray::new(hit_record.p, target - hit_record.p, ray.time());
Some((
scattered,
self.texture
.sample(hit_record.u, hit_record.v, &hit_record.p),
))
}
}
#[derive(Debug, Clone)]
pub struct Metal {
albedo: Vec3,
fuzz: Float,
}
impl Metal {
pub fn new(albedo: Vec3, fuzz: Float) -> Metal {
assert!(fuzz <= 1.);
Metal { albedo, fuzz }
}
}
impl MaterialTrait for Metal {
fn scatter<R: Rng>(
&self,
ray: &Ray,
hit_record: &HitRecord<'_>,
rng: &mut R,
) -> Option<(Ray, Vec3)> {
let reflected = reflect(&ray.direction().unit_vector(), &hit_record.normal);
let scattered = Ray::new(
hit_record.p,
reflected + self.fuzz * random_in_unit_sphere(rng),
ray.time(),
);
if scattered.direction().dot(&hit_record.normal) > 0.0 {
Some((scattered, self.albedo))
} else {
None
}
}
}
#[derive(Debug, Clone)]
pub struct Dielectric {
ref_idx: Float,
}
impl Dielectric {
pub fn new(ref_idx: Float) -> Dielectric {
Dielectric { ref_idx }
}
}
impl MaterialTrait for Dielectric {
fn scatter<R: Rng>(
&self,
r_in: &Ray,
hit_record: &HitRecord<'_>,
rng: &mut R,
) -> Option<(Ray, Vec3)> {
let mut refracted: Vec3 = Vec3::zero();
let reflect_prob: Float;
let reflected = reflect(&r_in.direction(), &hit_record.normal);
let attenuation = Vec3::new(1.0, 1.0, 1.0);
let (outward_normal, ni_over_nt, cosine) = if r_in.direction().dot(&hit_record.normal) > 0.0
{
(
-hit_record.normal,
self.ref_idx,
(self.ref_idx * r_in.direction().dot(&hit_record.normal)
/ r_in.direction().length()),
)
} else {
(
hit_record.normal,
1.0 / self.ref_idx,
(-r_in.direction().dot(&hit_record.normal) / r_in.direction().length()),
)
};
let refr = refract(&r_in.direction(), &outward_normal, ni_over_nt);
match refr {
Some(refr) => {
reflect_prob = schlick(cosine, self.ref_idx);
refracted = refr
}
None => reflect_prob = 1.0,
}
let direction = if rng.gen::<Float>() < reflect_prob {
reflected
} else {
refracted
};
Some((Ray::new(hit_record.p, direction, r_in.time()), attenuation))
}
}
#[derive(Debug, Clone)]
pub struct DiffuseLight {
texture: Texture,
}
impl DiffuseLight {
pub fn new(texture: Texture) -> DiffuseLight {
DiffuseLight { texture }
}
}
impl MaterialTrait for DiffuseLight {
fn scatter<R: Rng>(&self, _: &Ray, _: &HitRecord<'_>, _: &mut R) -> Option<(Ray, Vec3)> {
None
}
fn emit(&self, u: Float, v: Float, p: &Vec3) -> Vec3 {
self.texture.sample(u, v, p)
}
}
#[derive(Debug, Clone)]
pub struct Isotropic {
albedo: Texture,
}
impl Isotropic {
pub fn new(albedo: Texture) -> Isotropic {
Isotropic { albedo }
}
}
impl MaterialTrait for Isotropic {
fn scatter<R: Rng>(
&self,
ray: &Ray,
hit_record: &HitRecord,
rng: &mut R,
) -> Option<(Ray, Vec3)> {
Some((
Ray::new(hit_record.p, random_in_unit_sphere(rng), ray.time()),
self.albedo
.sample(hit_record.u, hit_record.v, &hit_record.p),
))
}
}
fn schlick(cosine: Float, ref_idx: Float) -> Float {
let mut r0 = (1.0 - ref_idx) / (1.0 + ref_idx);
r0 = r0 * r0;
(r0 + (1.0 - r0) * (1.0 - cosine).powi(5))
}
fn refract(v: &Vec3, n: &Vec3, ni_over_nt: Float) -> Option<Vec3> {
let uv = v.unit_vector();
let dt = uv.dot(n);
let discriminant = 1.0 - ni_over_nt * ni_over_nt * (1.0 - dt * dt);
if discriminant > 0.0 {
Some(ni_over_nt * (uv - *n * dt) - *n * discriminant.sqrt())
} else {
None
}
}
fn random_in_unit_sphere<R: Rng>(rng: &mut R) -> Vec3 {
loop {
let p = Vec3::new(rng.gen(), rng.gen(), rng.gen()) * 2.0 - Vec3::new(1., 1., 1.);
if p.length_squared() < 1.0 {
return p;
}
}
}
fn reflect(vector: &Vec3, normal: &Vec3) -> Vec3 {
*vector - 2.0 * vector.dot(normal) * *normal
}