-
Notifications
You must be signed in to change notification settings - Fork 0
/
BRDF.py
436 lines (307 loc) · 13 KB
/
BRDF.py
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import mitsuba as mi
import drjit as dr
import torch
from tools.mitsuba_tools import m2t
from MicrofacetDistribution import BeckmannDistribution, TrowbridgeReitzDistribution
import math
mi.set_variant("cuda_ad_rgb")
class BRDF:
"""
Base class for BRDFs
"""
def f(self, wo, wi, active):
""" Evaluate the BRDF
Args:
wo: outgoing ray
wi: incident ray
Returns:
f: (torch.Tensor [N, 3]) the value of the distribution function for the given pair of directions
"""
pass
def sample_f(self, wo, sample, active):
""" Sample the BRDF and return the sampled direction, the value of the distribution function, and the corresponding PDF
Args:
wo: outgoing ray
sample: a sample from a uniform distribution
Returns:
wi: (mi.Vector3f) the sampled (incident) direction
pdf: (torch.Tensor [N, 3]) the corresponding PDF
f: (torch.Tensor [N, 3]) the value of the distribution function
"""
pass
def PDF(self, wo, wi, active):
""" Evaluate the PDF for the given pair of directions
Args:
wo: outgoing ray
wi: incident ray
Returns:
pdf (torch.Tensor [N, 3]): the corresponding PDF
"""
pass
def eval_pdf(self, wo, wi, active):
""" use for evaluate wi sampled from Emitter
Args:
wo:
wi:
active:
Returns:
value: f(wo,wi) * cos_term
pdf:
"""
cos_theta_o = mi.Frame3f.cos_theta(wo)
cos_theta_i = mi.Frame3f.cos_theta(wi)
active = active & (cos_theta_i > 0) & (cos_theta_o > 0)
value = self.f(wo, wi, active) * m2t(cos_theta_i)
pdf = self.PDF(wo, wi, active)
# print(f"value: {value.max().item():.2f}, {value.min().item():.2f}")
# print(f"pdf: {pdf.max().item():.2f}, {pdf.min().item():.2f}")
# print('*'*20)
value = torch.where(m2t(active).bool(), value, torch.zeros_like(value))
return value, pdf
def sample(self, wo, sample, sample1d, active):
""" use for sample wi for path tracing
Args:
wo:
wi:
sample:
active:
Returns:
wi:
pdf
value: f(wo,wi) * cos_term / pdf
"""
cos_theta_o = mi.Frame3f.cos_theta(wo)
active = active & (cos_theta_o > 0)
wi, pdf, f = self.sample_f(wo, sample, sample1d, active)
cos_theta_i = mi.Frame3f.cos_theta(wi)
active = active & (cos_theta_i > 0)
inv_pdf = 1 / pdf.clamp_min(1e-4)
inv_pdf[pdf == 0] = 0
value = f * inv_pdf * m2t(cos_theta_i)
value = torch.where(m2t(active).bool(), value, torch.zeros_like(value))
value[value.isnan()] = 0
return wi, pdf, value
class LambertianReflection(BRDF):
"""
Lambertian BRDF
"""
def __init__(self, Rd: torch.Tensor):
self.Rd = Rd # roughness
def f(self, wo: mi.Vector3f, wi: mi.Vector3f, active=mi.Bool(True)):
f = self.Rd * dr.inv_pi
cos_theta_i = mi.Frame3f.cos_theta(wi)
cos_theta_o = mi.Frame3f.cos_theta(wo)
active = active & (cos_theta_i > 0) & (cos_theta_o > 0)
f = torch.where(m2t(active).bool(), f, torch.zeros_like(f))
f[f.isnan()] = 0
f = f.clamp_min(0)
return f
def sample_f(self, wo: mi.Vector3f, sample: mi.Point2f, sample1d: mi.Point1f, active=mi.Bool(True)):
wi = mi.warp.square_to_cosine_hemisphere(sample)
pdf = self.PDF(wo, wi, active)
f = self.f(wo, wi, active)
return wi, pdf, f
@torch.no_grad()
def PDF(self, wo: mi.Vector3f, wi: mi.Vector3f, active=mi.Bool(True)):
pdf = mi.warp.square_to_cosine_hemisphere_pdf(wi)
pdf = pdf.torch().unsqueeze(1)
cos_theta_i = mi.Frame3f.cos_theta(wi)
cos_theta_o = mi.Frame3f.cos_theta(wo)
active = active & (cos_theta_i > 0) & (cos_theta_o > 0)
pdf = torch.where(m2t(active).bool(), pdf, torch.zeros_like(pdf))
pdf[pdf.isnan()] = 0
pdf = pdf.clamp_min(0)
return pdf
class MicrofacetReflection(BRDF):
"""
Torrance–Sparrow microfacet model
"""
def __init__(self, Rs, roughness_x, roughness_y=None, distr_type='Beckmann'):
self.Rs = Rs # reflectance of the surface at normal incidence
# if roughness_y is None:
# roughness_y = roughness_x
assert distr_type in ['Beckmann', 'GGX']
if distr_type == 'Beckmann':
# self.distribution = BeckmannDistribution(roughness_x, roughness_y)
self.distribution = BeckmannDistribution(roughness_x)
elif distr_type == 'GGX':
# self.distribution = TrowbridgeReitzDistribution(roughness_x, roughness_y)
self.distribution = TrowbridgeReitzDistribution(roughness_x)
# Schlick approximation for Fresnel reflectance
def Fresnel_Schlick(self, cosTheta):
Rs = self.Rs
F = Rs + (1 - Rs) * (1 - cosTheta) ** 5
return F
def f(self, wo, wi, active):
wh = dr.normalize(wo + wi)
F = self.Fresnel_Schlick(m2t(dr.dot(wi, wh)))
D = self.distribution.D(wh)
G = self.distribution.G(wo, wi, wh)
# print(f"F: {F.max().item():.2f}, {F.min().item():.2f}")
# print(f"D: {D.max().item():.2f}, {D.min().item():.2f}")
# print(f"G: {G.max().item():.2f}, {G.min().item():.2f}")
cos_theta_o = mi.Frame3f.cos_theta(wo)
cos_theta_i = mi.Frame3f.cos_theta(wi)
f = F * D * G
f = f / (4 * m2t(cos_theta_o) * m2t(cos_theta_i)).clamp_min(1e-4) # clip
active = active & (cos_theta_i > 0) & (cos_theta_o > 0)
f[D == 0.0] = 0.0 # wh=[0,0,0]
f = torch.where(m2t(active).bool(), f, torch.zeros_like(f))
f[f.isnan()] = 0
f = f.clamp_min(0)
return f
def sample_f(self, wo, sample, sample1d, active):
wh, pdf = self.distribution.Sample_wh(wo, sample)
wi = mi.reflect(wo, wh)
pdf = torch.where(m2t(active).bool(), pdf, torch.zeros_like(pdf))
pdf = pdf / (4 * m2t(dr.dot(wo, wh))).clamp_min(1e-4)
f = self.f(wo, wi, active)
# Ensure that this is a valid sample
f[pdf == 0] = 0
f[m2t(mi.Frame3f.cos_theta(wi) <= 0).bool()] = 0
return wi, pdf, f
@torch.no_grad()
def PDF(self, wo, wi, active):
wh = dr.normalize(wo + wi)
pdf = self.distribution.PDF(wo, wh) / (4 * m2t(dr.dot(wo, wh)).clamp_min(1e-4))
cos_theta_i = mi.Frame3f.cos_theta(wi)
cos_theta_o = mi.Frame3f.cos_theta(wo)
cos_theta_ih = dr.dot(wi, wh)
cos_theta_oh = dr.dot(wo, wh)
active = active & (cos_theta_i > 0) & (cos_theta_o > 0) & (cos_theta_ih > 0) & (cos_theta_oh > 0)
pdf = torch.where(m2t(active).bool(), pdf, torch.zeros_like(pdf))
pdf[pdf.isnan()] = 0
pdf = pdf.clamp_min(0)
return pdf
class Phong(BRDF):
def __init__(self, Rs: torch.Tensor, n: torch.Tensor):
self.Rs = Rs
self.n = n * 200
def f(self, wo, wi, active):
n = self.n
alpha = dr.dot(wi, mi.reflect(wo)).torch()[:, None]
alpha = torch.clip(alpha, 0, None)
# alpha = torch.nan_to_num(alpha, nan=0, posinf=0, neginf=0) # wi can be nan
f = (n + 2) * (1 / (2 * dr.pi)) * torch.pow(alpha, n) * self.Rs
cos_theta_i = mi.Frame3f.cos_theta(wi)
cos_theta_o = mi.Frame3f.cos_theta(wo)
active = active & (cos_theta_i > 0) & (cos_theta_o > 0)
f = torch.where(m2t(active).bool(), f, torch.zeros_like(f))
return f
def sample_f(self, wo, sample, sample1d, active):
n = self.n
n_mi = mi.Float(n.flatten().float())
R = mi.reflect(wo)
sinAlpha = dr.sqrt(1 - dr.power(sample.y, 2 / (n_mi + 1)))
cosAlpha = dr.power(sample.y, 1 / (n_mi + 1))
phi = (2.0 * dr.pi) * sample.x
localDir = mi.Vector3f(sinAlpha * dr.cos(phi),
sinAlpha * dr.sin(phi),
cosAlpha)
wi = mi.Frame3f(R).to_world(localDir)
pdf = self.PDF(wo, wi, active)
f = self.f(wo, wi, active)
return wi, pdf, f
@torch.no_grad()
def PDF(self, wo, wi, active):
n = self.n
alpha = dr.dot(wi, mi.reflect(wo)).torch()[:, None]
alpha = torch.clip(alpha, 0, None)
# alpha = torch.nan_to_num(alpha, nan=0, posinf=0, neginf=0) # wi can be nan
pdf = (n + 1) * (1 / (2 * dr.pi)) * torch.pow(alpha, n)
cos_theta_i = mi.Frame3f.cos_theta(wi)
cos_theta_o = mi.Frame3f.cos_theta(wo)
active = active & (cos_theta_i > 0) & (cos_theta_o > 0)
pdf = torch.where(m2t(active).bool(), pdf, torch.zeros_like(pdf))
return pdf
class GlossyBRDF(BRDF):
"""
mixed BRDF
"""
def __init__(self, Rd: torch.Tensor, Rs, roughness_x, roughness_y=None, specular_type='GGX'):
assert specular_type in ['GGX', 'Phong']
self.LambertianReflection = LambertianReflection(Rd)
# roughness_x in [0.02, 1]
if specular_type == 'GGX':
self.SpecularReflection = MicrofacetReflection(Rs, roughness_x, roughness_y, specular_type)
else:
self.SpecularReflection = Phong(Rs, (5 / roughness_x)) # n = 4 / roughness, in [1, 247]
self.ratio = 0.5
def f(self, wo, wi, active):
f = self.LambertianReflection.f(wo, wi, active) + self.SpecularReflection.f(wo, wi, active)
return f
def sample_f(self, wo, sample, sample1d, active=mi.Bool(True)):
wi, _, _ = self.LambertianReflection.sample_f(wo, sample, sample1d, active)
wi2, _, _ = self.SpecularReflection.sample_f(wo, sample, sample1d, active)
wi = dr.select(sample1d < self.ratio, wi2, wi)
pdf = self.PDF(wo, wi, active)
f = self.f(wo, wi, active)
return wi, pdf, f
@torch.no_grad()
def PDF(self, wo, wi, active=mi.Bool(True)):
pdf = (1 - self.ratio) * self.LambertianReflection.PDF(wo, wi, active) + self.ratio * self.SpecularReflection.PDF(wo, wi, active)
return pdf
class GlossyBRDF_cws(BRDF):
"""
mixed BRDF
"""
def __init__(self, Rd: torch.Tensor, Rs, roughness_x, roughness_y=None, specular_type='GGX'):
assert specular_type in ['GGX', 'Phong']
self.LambertianReflection = LambertianReflection(Rd)
# roughness_x in [0.02, 1]
if specular_type == 'GGX':
self.SpecularReflection = MicrofacetReflection(Rs, roughness_x, roughness_y, specular_type)
else:
self.SpecularReflection = Phong(Rs, (5 / roughness_x)) # n = 4 / roughness, in [1, 247]
self.ratio = 0.5
def f(self, wo, wi, active):
f = self.LambertianReflection.f(wo, wi, active) + self.SpecularReflection.f(wo, wi, active)
return f
def sample_f(self, wo, sample, sample1d, active=mi.Bool(True)):
wi = mi.warp.square_to_cosine_hemisphere(sample)
f = self.f(wo, wi, active)
pdf = self.PDF(wo, wi, active)
return wi, pdf, f
@torch.no_grad()
def PDF(self, wo, wi, active=mi.Bool(True)):
pdf = mi.warp.square_to_cosine_hemisphere_pdf(wi)
pdf = pdf.torch().unsqueeze(1)
cos_theta_i = mi.Frame3f.cos_theta(wi)
cos_theta_o = mi.Frame3f.cos_theta(wo)
active = active & (cos_theta_i > 0) & (cos_theta_o > 0)
pdf = torch.where(m2t(active).bool(), pdf, torch.zeros_like(pdf))
pdf[pdf.isnan()] = 0
pdf = pdf.clamp_min(0)
return pdf
class GlossyBRDF_random(BRDF):
"""
mixed BRDF
"""
def __init__(self, Rd: torch.Tensor, Rs, roughness_x, roughness_y=None, specular_type='GGX'):
assert specular_type in ['GGX', 'Phong']
self.LambertianReflection = LambertianReflection(Rd)
# roughness_x in [0.02, 1]
if specular_type == 'GGX':
self.SpecularReflection = MicrofacetReflection(Rs, roughness_x, roughness_y, specular_type)
else:
self.SpecularReflection = Phong(Rs, (5 / roughness_x)) # n = 4 / roughness, in [1, 247]
self.ratio = 0.5
def f(self, wo, wi, active):
f = self.LambertianReflection.f(wo, wi, active) + self.SpecularReflection.f(wo, wi, active)
return f
def sample_f(self, wo, sample, sample1d, active=mi.Bool(True)):
wi = mi.warp.square_to_uniform_hemisphere(sample)
f = self.f(wo, wi, active)
pdf = self.PDF(wo, wi, active)
return wi, pdf, f
@torch.no_grad()
def PDF(self, wo, wi, active=mi.Bool(True)):
pdf = mi.warp.square_to_uniform_hemisphere_pdf(wi)
pdf = pdf.torch().unsqueeze(1)
cos_theta_i = mi.Frame3f.cos_theta(wi)
cos_theta_o = mi.Frame3f.cos_theta(wo)
active = active & (cos_theta_i > 0) & (cos_theta_o > 0)
pdf = torch.where(m2t(active).bool(), pdf, torch.zeros_like(pdf))
pdf[pdf.isnan()] = 0
pdf = pdf.clamp_min(0)
return pdf