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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use crate::{
Normal, Direction, Point,
Float, Vec2, rand_utils
};
use crate::tracer::{
bxdfs, microfacet::MfDistribution, Color,
object::Sampleable, onb::Onb, ray::Ray
};
#[cfg(test)]
mod pdf_tests;
/// Assumes that each generation and evaluation has same starting point.
pub trait Pdf {
/// Generates a random direction according to the sampling strategy
///
/// # Arguments
/// * `rand_sq` - Random point on the unit square.
fn sample_direction(&self, rand_sq: Vec2) -> Option<Direction>;
/// Computes the probability of the given direction w.r.t solid angle.
///
/// # Arguments
/// * `ri` - Ray to compute probability for
/// * `swap_dir` - Do we swap `wi` and `v`.
/// Only makes a difference in non-symmetric PDFs (MFD refraction).
fn value_for(&self, ri: &Ray, swap_dir: bool) -> Float;
}
/// Cosine weighed hemisphere sampling
pub struct CosPdf {
uvw: Onb,
}
impl CosPdf {
pub fn new(ns: Normal) -> Self {
let uvw = Onb::new(ns);
Self { uvw }
}
}
impl Pdf for CosPdf {
fn sample_direction(&self, rand_sq: Vec2) -> Option<Direction> {
Some(self.uvw.to_world(rand_utils::square_to_cos_hemisphere(rand_sq)))
}
fn value_for(&self, ri: &Ray, _swap_dir: bool) -> Float {
let wi = ri.dir;
let cos_theta = self.uvw.w.dot(wi);
if cos_theta > 0.0 {
cos_theta / crate::PI
} else {
0.0
}
}
}
/// Randomly samples a direction towards a point on the object that is visible
pub struct ObjectPdf<'a> {
/// Object to do sampling from
object: &'a dyn Sampleable,
/// Point from where the object should be visible
xo: Point,
}
impl<'a> ObjectPdf<'a> {
pub fn new(object: &'a dyn Sampleable, xo: Point) -> Self {
Self { object, xo }
}
}
impl Pdf for ObjectPdf<'_> {
fn sample_direction(&self, rand_sq: Vec2) -> Option<Direction> {
Some( self.object.sample_towards(self.xo, rand_sq) )
}
fn value_for(&self, ri: &Ray, _swap_dir: bool) -> Float {
let (p, hi) = self.object.sample_towards_pdf(ri);
if let Some(hi) = hi {
// convert area measure to solid angle measure
// other fields of hit might be in local instance coordinates
let xi = hi.p;
let ni = hi.ng;
let wi = ri.dir;
p * self.xo.distance_squared(xi) / ni.dot(wi).abs()
} else {
0.0
}
}
}
/// Delta distribution PDF. Always samples the same ray. For glass/mirror.
pub struct DeltaPdf {
wi: Direction,
}
impl DeltaPdf {
pub fn new(wi: Direction) -> Self {
Self { wi }
}
}
impl Pdf for DeltaPdf {
fn sample_direction(&self, _rand_sq: Vec2) -> Option<Direction> {
Some( self.wi )
}
// symmetric
fn value_for(&self, ri: &Ray, _swap_dir: bool) -> Float {
let wi = ri.dir;
if wi.dot(self.wi) >= 1.0 - crate::EPSILON {
1.0
} else {
0.0
}
}
}
/// PDF for volumetric mediums
pub struct VolumetricPdf {
/// ONB for view direction
uvw: Onb,
/// Scattering parameter
g: Float,
}
impl VolumetricPdf {
pub fn new(v: Direction, g: Float) -> Self {
Self {
g,
uvw: Onb::new(v),
}
}
}
impl Pdf for VolumetricPdf {
fn sample_direction(&self, rand_sq: Vec2) -> Option<Direction> {
let cos_theta = if self.g.abs() < 1e-3 {
1.0 - 2.0 * rand_sq.x
} else {
let g2 = self.g * self.g;
let fract = (1.0 - g2) / (1.0 - self.g + 2.0 * self.g * rand_sq.x);
(1.0 + g2 - fract * fract) / (2.0 * self.g)
};
let sin_theta = (1.0 - cos_theta * cos_theta).max(0.0).sqrt();
let phi = 2.0 * crate::PI * rand_sq.y;
let wi = self.uvw.to_world(Direction::new(
sin_theta * phi.cos(),
sin_theta * phi.sin(),
cos_theta
));
Some( wi )
}
// symmetric
fn value_for(&self, ri: &Ray, _swap_dir: bool) -> Float {
let wi = ri.dir;
let cos_theta = wi.dot(self.uvw.w);
let denom = 1.0 + self.g * self.g + 2.0 * self.g * cos_theta;
(1.0 - self.g * self.g)
/ (4.0 * crate::PI * denom * denom.max(0.0).sqrt())
}
}
/// PDF for microfacet distribution.
pub struct MfdPdf {
/// Direction from point of impact to viewer
v: Direction,
/// Macrosurface shading normal. Same hemisphere as `v`.
ns: Normal,
/// Macrosurface geometric normal. Points outside of surface.
ng: Normal,
/// Probability to sample ray from NDF
ndf_sample_prob: Float,
/// ONB for macrosurface normal
uvw: Onb,
/// The microfacet distribution of the surface
mfd: MfDistribution,
}
impl MfdPdf {
pub fn new(
v: Direction,
ns: Normal,
ng: Normal,
albedo: Color,
mfd: MfDistribution
) -> Self {
// refraction needs v and wh to be in same hemisphere so we do this
let ns = if v.dot(ng) < 0.0 { -ns } else { ns };
let uvw = Onb::new(ns);
Self {
v,
uvw,
ndf_sample_prob: mfd.probability_ndf_sample(albedo),
ns,
ng,
mfd,
}
}
/// Samples randomly from the hemisphere with cos weighing
fn sample_cos_hemisphere(&self, rand_sq: Vec2) -> Option<Direction> {
Some(
self.uvw.to_world(rand_utils::square_to_cos_hemisphere(rand_sq))
)
}
/// Samples a random microfacet normal and mirrors direction around it
fn sample_ndf_scatter(&self, rand_sq: Vec2) -> Option<Direction> {
let local_v = self.uvw.to_local(self.v);
let local_wh = self.mfd.sample_normal(local_v, rand_sq).normalize();
let local_wi = bxdfs::reflect(local_v, local_wh);
if local_wi.z <= 0.0 {
// bad sample, do something else?
None
} else {
Some( self.uvw.to_world(local_wi) )
}
}
/// Samples a random microfacet normal and refracts direction around it
fn sample_ndf_refract(&self, rand_sq: Vec2) -> Option<Direction> {
let local_v = self.uvw.to_local(self.v);
let local_wh = self.mfd.sample_normal(local_v, rand_sq).normalize();
let wh = self.uvw.to_world(local_wh).normalize();
let inside = self.ng.dot(self.v) < 0.0;
let eta_ratio = if inside {
self.mfd.get_rfrct_idx()
} else {
1.0 / self.mfd.get_rfrct_idx()
};
Some( bxdfs::refract(eta_ratio, self.v, wh) )
}
/// PDF for NDF scattering
fn sample_ndf_scatter_pdf(&self, wh: Normal) -> Float {
let wh_dot_v = self.v.dot(wh);
// probability to sample wh w.r.t. to v.
// wh and v always same hemisphere. ns flipped to same in constructor.
self.mfd.sample_normal_pdf(wh, self.v, self.ns)
// jacobian
/ (4.0 * wh_dot_v)
}
/// PDF for hemisphere cos sampling
fn sample_cos_hemisphere_pdf(&self, wi: Direction) -> Float {
let cos_theta = self.ns.dot(wi);
if cos_theta > 0.0 {
cos_theta / crate::PI
} else {
0.0
}
}
/// PDF for NDF refraction. Non-symmetric
fn sample_ndf_refract_pdf(&self, wi: Direction, swap_dir: bool) -> Float {
let (v, wi) = if swap_dir { (wi, self.v) } else { (self.v, wi) };
let v_inside = self.ng.dot(v) < 0.0;
let wi_inside = self.ng.dot(wi) < 0.0;
if v_inside == wi_inside {
let wh = (v + wi).normalize();
let wh_dot_v = wh.dot(v);
let sin2_to = 1.0 - wh_dot_v * wh_dot_v;
let sin2_ti = sin2_to * self.mfd.get_rfrct_idx().powi(2);
if v_inside && sin2_ti > 1.0 {
let wh_dot_v = wh.dot(v);
self.mfd.sample_normal_pdf(wh, v, self.ns)
/ (4.0 * wh_dot_v)
} else {
// wi and v same hemisphere but not total internal reflection.
// impossible
0.0
}
} else {
let eta_ratio = if v_inside {
1.0 / self.mfd.get_rfrct_idx()
} else {
self.mfd.get_rfrct_idx()
};
let wh = -(v + wi * eta_ratio).normalize();
let wh_dot_wi = wi.dot(wh);
let wh_dot_v = wh.dot(v);
if wh_dot_wi * wh_dot_v > 0.0 {
// same hemisphere w.r.t wh, zero probability for refraction
0.0
} else {
// wh and ns need to be in same hemisphere, hemisphere of v makes
// no difference.
let wh = if self.ns.dot(wh) < 0.0 { -wh } else { wh };
self.mfd.sample_normal_pdf(wh, v, self.ns)
// jacobian
* (eta_ratio * eta_ratio * wh_dot_wi).abs()
/ (wh_dot_v + eta_ratio * wh_dot_wi).powi(2)
}
}
}
}
impl Pdf for MfdPdf {
/// Sample microsurface normal from the distribution. Mirror direction from
/// camera around the normal. GGX uses VNDF sampling, Beckmann NDF sampling.
/// Importance sample with hemisphere scattering / refraction.
fn sample_direction(&self, rand_sq: Vec2) -> Option<Direction> {
if rand_utils::rand_float() < self.ndf_sample_prob {
// NDF sample
self.sample_ndf_scatter(rand_sq)
} else if !self.mfd.is_transparent() {
// Opaque materials sample hemisphere
self.sample_cos_hemisphere(rand_sq)
} else {
// Transparent materials refract
self.sample_ndf_refract(rand_sq)
}
}
fn value_for(&self, ri: &Ray, swap_dir: bool) -> Float {
let wi = ri.dir;
let wh = (self.v + wi).normalize();
// probability to sample wh w.r.t. to v
let ndf_pdf = self.sample_ndf_scatter_pdf(wh);
// refraction / cos hemisphere sample probability
let hr_pdf = if !self.mfd.is_transparent() {
self.sample_cos_hemisphere_pdf(wi)
} else {
self.sample_ndf_refract_pdf(wi, swap_dir)
};
self.ndf_sample_prob * ndf_pdf
+ (1.0 - self.ndf_sample_prob) * hr_pdf
}
}