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
use std::sync::Arc;
use light_arena::Allocator;
use geometry::Intersection;
use bxdf::{BxDF, BSDF, TorranceSparrow, Lambertian};
use bxdf::microfacet::Beckmann;
use bxdf::fresnel::Dielectric;
use material::Material;
use texture::Texture;
pub struct Plastic {
diffuse: Arc<Texture + Send + Sync>,
gloss: Arc<Texture + Send + Sync>,
roughness: Arc<Texture + Send + Sync>,
}
impl Plastic {
pub fn new(diffuse: Arc<Texture + Send + Sync>,
gloss: Arc<Texture + Send + Sync>,
roughness: Arc<Texture + Send + Sync>) -> Plastic
{
Plastic {
diffuse: diffuse.clone(),
gloss: gloss.clone(),
roughness: roughness.clone()
}
}
}
impl Material for Plastic {
fn bsdf<'a, 'b, 'c>(&self, hit: &Intersection<'a, 'b>,
alloc: &'c Allocator) -> BSDF<'c> where 'a: 'c
{
let diffuse = self.diffuse.sample_color(hit.dg.u, hit.dg.v, hit.dg.time);
let gloss = self.gloss.sample_color(hit.dg.u, hit.dg.v, hit.dg.time);
let roughness = self.roughness.sample_f32(hit.dg.u, hit.dg.v, hit.dg.time);
let mut num_bxdfs = 0;
if !diffuse.is_black() {
num_bxdfs += 1;
}
if !gloss.is_black() {
num_bxdfs += 1;
}
let bxdfs = alloc.alloc_slice::<&BxDF>(num_bxdfs);
let mut i = 0;
if !diffuse.is_black() {
bxdfs[i] = alloc.alloc(Lambertian::new(&diffuse));
i += 1;
}
if !gloss.is_black() {
let fresnel = alloc.alloc(Dielectric::new(1.0, 1.5));
let microfacet = alloc.alloc(Beckmann::new(roughness));
bxdfs[i] = alloc.alloc(TorranceSparrow::new(&gloss, fresnel, microfacet));
}
BSDF::new(bxdfs, 1.0, &hit.dg)
}
}