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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use crate::tracer::{ onb::Onb, Color };
use crate::{ Normal, Direction, Float, Vec2 };
/// Configurable parameters for a microsurface
#[derive(Copy, Clone)]
pub struct MicrofacetConfig {
/// Roughness of the surface (α) [0,1]
pub roughness: Float,
/// Refraction index of the material >= 1.0
pub refraction_idx: Float,
/// Ratio of how metallic the material is [0,1]
pub metallicity: Float,
/// Transparency of the material
pub transparent: bool,
}
impl MicrofacetConfig {
pub fn new(
roughness: Float,
refraction_idx: Float,
metallicity: Float,
transparent: bool
) -> Self {
assert!((0.0..=1.0).contains(&roughness));
assert!((0.0..=1.0).contains(&metallicity));
assert!(refraction_idx >= 1.0);
Self {
roughness: roughness.max(1e-5),
refraction_idx,
metallicity,
transparent,
}
}
}
/// Defines a distribution of normals for a microfacet. `Float` parameter is the
/// roughness (α) of the surface.
#[derive(Copy, Clone)]
pub enum MfDistribution {
/// Walter et al. 2007
Ggx(MicrofacetConfig),
/// Beckmann et al. 1987
Beckmann(MicrofacetConfig),
}
impl MfDistribution {
pub fn new(
roughness: Float,
refraction_idx: Float,
metallicity: Float,
transparent: bool
) -> Self {
Self::Ggx(MicrofacetConfig::new(roughness, refraction_idx, metallicity, transparent))
}
/// Is the material transparent?
pub fn is_transparent(&self) -> bool {
self.get_config().transparent
}
/// might need tuning, send ratio that emittance is multiplied with?
pub fn is_specular(&self) -> bool {
self.is_transparent() || self.get_config().roughness < 0.01
}
/// Does the material have delta scattering distribution?
pub fn is_delta(&self) -> bool {
self.get_config().roughness < 1e-2
}
/// Gets the refraction index
pub fn get_rfrct_idx(&self) -> Float {
self.get_config().refraction_idx
}
/// Get roughness from config
pub fn get_roughness(&self) -> Float {
self.get_config().roughness
}
/// Getter, better way to do this?
fn get_config(&self) -> &MicrofacetConfig {
match self {
Self::Ggx(cfg) | Self::Beckmann(cfg) => cfg,
}
}
/// Disney diffuse (Burley 2012) with renormalization to conserve energy
/// as done in Frostbite (Lagarde et al. 2014)
pub fn disney_diffuse(
&self,
no_dot_v: Float,
no_dot_wh: Float,
no_dot_wi: Float
) -> Float {
let roughness2 = self.get_config().roughness.powi(2);
let energy_bias = 0.5 * roughness2;
let fd90 = energy_bias + 2.0 * no_dot_wh.powi(2) * roughness2;
let view_scatter = 1.0 + (fd90 - 1.0) * (1.0 - no_dot_v).powi(5);
let light_scatter = 1.0 + (fd90 - 1.0) * (1.0 - no_dot_wi).powi(5);
let energy_factor = 1.0 + roughness2 * (1.0 / 1.51 - 1.0);
view_scatter * light_scatter * energy_factor
}
/// The microfacet distribution function.
///
/// # Distributions
/// * Beckmann - exp(-tan^2(θ) / α^2) / (π * α^2 * cos^4(θ))
/// * GGX - α^2 / (π * (cos^4(θ) * (α^2 - 1.0) + 1.0)^2)
///
/// # Arguments
/// * `wh` - Microsurface normal
/// * `no` - Macrosurface normal
pub fn d(&self, wh: Normal, no: Normal) -> Float {
match self {
Self::Ggx(cfg) => {
let cos2_theta = wh.dot(no).powi(2);
if cos2_theta < crate::EPSILON {
0.0
} else {
let roughness2 = cfg.roughness * cfg.roughness;
roughness2
/ (crate::PI * (1.0 - cos2_theta * (1.0 - roughness2)).powi(2))
}
}
Self::Beckmann(cfg) => {
let cos2_theta = wh.dot(no).powi(2);
if cos2_theta < crate::EPSILON {
0.0
} else {
let roughness2 = cfg.roughness * cfg.roughness;
let tan2_theta = (1.0 - cos2_theta) / cos2_theta;
(-tan2_theta / roughness2).exp()
/ (crate::PI * roughness2 * cos2_theta.powi(2))
}
}
}
}
/// Fresnel term with Schlick's approximation
pub fn f(&self, wo: Direction, wh: Normal, color: Color) -> Color {
let eta = self.get_config().refraction_idx;
let metallicity = self.get_config().metallicity;
let f0 = (eta - 1.0) / (eta + 1.0);
let f0 = Color::splat(f0 * f0).lerp(color, metallicity);
let wo_dot_wh = wo.dot(wh).abs();
f0 + (Color::WHITE - f0) * (1.0 - wo_dot_wh).powi(5)
}
/// Shadow-masking term. Used to make sure that only microfacets that are
/// visible from `v` direction are considered. Uses the method described
/// in Chapter 8.4.3 of PBR due to Heitz et al. 2013.
///
/// # Arguments
/// * `v` - View direction
/// * `wi` - Direction of ray away from the point of impact
/// * `wh` - Microsurface normal
/// * `no` - Macrosurface normal
pub fn g(&self, v: Direction, wi: Direction, wh: Normal, no: Normal) -> Float {
// signum to fix refraction
let chi = wh.dot(no).signum() * v.dot(wh) / v.dot(no);
if chi < crate::EPSILON {
0.0
} else {
1.0 / (1.0 + self.lambda(v, no) + self.lambda(wi, no))
}
}
pub fn g1(&self, v: Direction, wh: Normal, no: Normal) -> Float {
// signum to fix refraction
let chi = wh.dot(no).signum() * v.dot(wh) / v.dot(no);
if chi < crate::EPSILON {
0.0
} else {
1.0 / (1.0 + self.lambda(v, no))
}
}
/// Lambda function used in the definition of the shadow-masking term.
/// Beckmann with polynomial approximation and GGX exactly. PBR Chapter 8.4.3
///
/// # Arguments
/// * `w` - Direction to consider
/// * `no` - Macrosurface normal
fn lambda(&self, w: Direction, no: Normal) -> Float {
match self {
Self::Ggx(cfg) => {
let cos2_theta = w.dot(no).powi(2);
if cos2_theta < crate::EPSILON {
0.0
} else {
let tan2_theta = (1.0 - cos2_theta) / cos2_theta;
let roughness2 = cfg.roughness * cfg.roughness;
((1.0 + roughness2 * tan2_theta).sqrt() - 1.0) / 2.0
}
}
Self::Beckmann(cfg) => {
let cos2_theta = w.dot(no).powi(2);
if cos2_theta < crate::EPSILON {
0.0
} else {
let tan2_theta = ((1.0 - cos2_theta) / cos2_theta).abs();
let a = 1.0 / (cfg.roughness * tan2_theta);
if a >= 1.6 {
0.0
} else {
(1.0 - 1.259 * a + 0.396 * a * a)
/ (3.535 * a + 2.181 * a * a)
}
}
}
}
}
/// Probability to do importance sampling from NDF. Estimate based on
/// the Fresnel term.
pub fn probability_ndf_sample(&self, albedo: Color) -> Float {
let cfg = self.get_config();
let f0 = (cfg.refraction_idx - 1.0) / (cfg.refraction_idx + 1.0);
let f0 = f0 * f0;
(1.0 - cfg.metallicity) * f0 + cfg.metallicity * albedo.mean()
}
/// Probability that `wh` got sampled
pub fn sample_normal_pdf(
&self,
wh: Normal,
v: Direction,
no: Normal
) -> Float {
let pdf = match self {
Self::Beckmann(..) => {
let wh_dot_no = wh.dot(no);
self.d(wh, no) * wh_dot_no
}
Self::Ggx(..) => {
let wh_dot_v = wh.dot(v);
let no_dot_v = no.dot(v);
self.g1(v, wh, no) * self.d(wh, no) * wh_dot_v / no_dot_v
}
};
pdf.max(0.0)
}
/// Sampling microfacet normals per distribution for importance sampling.
/// `v` in shading space.
pub fn sample_normal(&self, v: Direction, rand_sq: Vec2) -> Normal {
match self {
Self::Ggx(cfg) => {
// Heitz 2018 or
// https://schuttejoe.github.io/post/ggximportancesamplingpart2/
let roughness = cfg.roughness;
// Map the GGX ellipsoid to a hemisphere
let v_stretch = Direction::new(
v.x * roughness,
v.y * roughness,
v.z
).normalize();
// ONB basis of the hemisphere configuration
let hemi_basis = Onb::new(v_stretch);
// compute a point on the disk
let a = 1.0 / (1.0 + v_stretch.z);
let r = rand_sq.x.sqrt();
let phi = if rand_sq.y < a {
crate::PI * rand_sq.y / a
} else {
crate::PI + crate::PI * (rand_sq.y - a) / (1.0 - a)
};
let x = r * phi.cos();
let y = if rand_sq.y < a {
r * phi.sin()
} else {
r * phi.sin() * v_stretch.z
};
// compute normal in hemisphere configuration
let wm = Normal::new(
x,
y,
(1.0 - x*x - y*y).max(0.0).sqrt(),
);
let wm = hemi_basis.to_world(wm);
// move back to ellipsoid
Normal::new(
roughness * wm.x,
roughness * wm.y,
wm.z.max(0.0)
).normalize()
}
Self::Beckmann(cfg) => {
let roughness2 = cfg.roughness * cfg.roughness;
let theta = (-roughness2 * (1.0 - rand_sq.y).ln()).sqrt().atan();
let phi = 2.0 * crate::PI * rand_sq.x;
Normal::new(
theta.sin() * phi.cos(),
theta.sin() * phi.sin(),
theta.cos(),
)
}
}
}
}