-
Notifications
You must be signed in to change notification settings - Fork 26
/
sg_rendering.py
318 lines (258 loc) · 10.2 KB
/
sg_rendering.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
from typing import Tuple
import numpy as np
import tensorflow as tf
import nn_utils.math_utils as math_utils
class SgRenderer(tf.keras.layers.Layer):
def __init__(
self,
eval_background: bool = False,
compress_sharpness: bool = False,
compress_amplitude: bool = False,
**kwargs
):
super(SgRenderer, self).__init__(**kwargs)
self.eval_background = eval_background
self.compress_sharpness = compress_sharpness
self.compress_amplitude = compress_amplitude
@tf.function
def call(
self,
sg_illuminations: tf.Tensor,
basecolor: tf.Tensor,
metallic: tf.Tensor,
roughness: tf.Tensor,
normal: tf.Tensor,
alpha: tf.Tensor,
view_dir: tf.Tensor,
):
with tf.name_scope("Renderer"):
lin_basecolor = math_utils.srgb_to_linear(basecolor)
diffuse = lin_basecolor * (1 - metallic) # Only diffuse is metallic is 0
# Interpolate between 0.04 base reflectivity where non-metallic
# and specular color (from basecolor)
specular = math_utils.mix(
tf.ones_like(lin_basecolor) * 0.04, lin_basecolor, metallic
)
normal = tf.where(normal == tf.zeros_like(normal), view_dir, normal)
diffuse = tf.expand_dims(diffuse, 1)
specular = tf.expand_dims(specular, 1)
roughness = tf.expand_dims(roughness, 1)
normal = tf.expand_dims(math_utils.normalize(normal), 1)
view_dir = tf.expand_dims(math_utils.normalize(view_dir), 1)
env = None
if self.eval_background:
# Evaluate everything for the environment
env = self._sg_evaluate(sg_illuminations, view_dir)
# And sum all contributions
env = tf.reduce_sum(env, 1)
# Evaluate BRDF
brdf = self._brdf_eval(
sg_illuminations, diffuse, specular, roughness, normal, view_dir,
)
# And sum the contributions
brdf = tf.reduce_sum(brdf, 1)
if self.eval_background:
if len(alpha.shape) == 1:
alpha = tf.expand_dims(alpha, 1)
alpha = tf.clip_by_value(alpha, 0, 1)
return tf.nn.relu(brdf * alpha + env * (1 - alpha))
else:
return tf.nn.relu(brdf)
@tf.function
def _brdf_eval(
self,
sg_illuminations: tf.Tensor,
diffuse: tf.Tensor,
specular: tf.Tensor,
roughness: tf.Tensor,
normal: tf.Tensor,
view_dir: tf.Tensor,
):
with tf.name_scope("BRDF_evaluation"):
v = view_dir
diff = diffuse
spec = specular
norm = normal
rogh = roughness
ndf = self._distribution_term(norm, rogh)
warped_ndf = self._sg_warp_distribution(ndf, v)
_, warpDir, _ = self._extract_sg_components(warped_ndf)
ndl = math_utils.saturate(math_utils.dot(norm, warpDir))
ndv = math_utils.saturate(math_utils.dot(norm, v))
h = math_utils.normalize(warpDir + v)
ldh = math_utils.saturate(math_utils.dot(warpDir, h))
diffuse_eval = self._evaluate_diffuse(sg_illuminations, diff, norm) # * ndl
specular_eval = self._evaluate_specular(
sg_illuminations, spec, rogh, warped_ndf, ndl, ndv, ldh
)
tf.debugging.check_numerics(
diffuse_eval,
"output diffuse_eval: {}".format(tf.math.is_nan(diffuse_eval)),
)
tf.debugging.check_numerics(
specular_eval,
"output specular_eval: {}".format(tf.math.is_nan(specular_eval)),
)
return diffuse_eval + specular_eval
@tf.function
def _evaluate_diffuse(
self, sg_illuminations: tf.Tensor, diffuse: tf.Tensor, normal: tf.Tensor
) -> tf.Tensor:
with tf.name_scope("Diffuse"):
diff = diffuse / np.pi
_, s_axis, s_sharpness = self._extract_sg_components(sg_illuminations)
mudn = math_utils.saturate(math_utils.dot(s_axis, normal))
c0 = 0.36
c1 = 1.0 / (4.0 * c0)
eml = math_utils.safe_exp(-s_sharpness)
em2l = eml * eml
rl = tf.math.reciprocal_no_nan(s_sharpness)
scale = 1.0 + 2.0 * em2l - rl
bias = (eml - em2l) * rl - em2l
x = math_utils.safe_sqrt(1.0 - scale)
x0 = c0 * mudn
x1 = c1 * x
n = x0 + x1
y_cond = tf.less_equal(tf.abs(x0), x1)
y_true = n * (n / tf.maximum(x, 1e-6))
y_false = mudn
y = tf.where(y_cond, y_true, y_false)
res = scale * y + bias
res = res * self._sg_integral(sg_illuminations) * diff
return res
@tf.function
def _evaluate_specular(
self,
sg_illuminations: tf.Tensor,
specular: tf.Tensor,
roughness: tf.Tensor,
warped_ndf: tf.Tensor,
ndl: tf.Tensor,
ndv: tf.Tensor,
ldh: tf.Tensor,
) -> tf.Tensor:
with tf.name_scope("Specular"):
a2 = math_utils.saturate(roughness * roughness, 1e-3)
with tf.name_scope("Distribution"):
D = self._sg_inner_product(warped_ndf, sg_illuminations)
with tf.name_scope("Geometry"):
G = self._ggx(a2, ndl) * self._ggx(a2, ndv)
with tf.name_scope("Fresnel"):
powTerm = tf.pow(1.0 - ldh, 5)
F = specular + (1.0 - specular) * powTerm
output = D * G * F * ndl
return tf.nn.relu(output)
@tf.function
def _ggx(self, a2: tf.Tensor, ndx: tf.Tensor) -> tf.Tensor:
with tf.name_scope("Geometric"):
return tf.math.reciprocal_no_nan(
ndx + math_utils.safe_sqrt(a2 + (1 - a2) * ndx * ndx)
)
@tf.function
def _distribution_term(self, d: tf.Tensor, roughness: tf.Tensor) -> tf.Tensor:
with tf.name_scope("Distribution"):
a2 = math_utils.saturate(roughness * roughness, 1e-3)
ret = self._stack_sg_components(
math_utils.to_vec3(tf.math.reciprocal_no_nan(np.pi * a2)),
d,
2.0 / tf.maximum(a2, 1e-6),
)
return ret
@tf.function
def _sg_warp_distribution(self, ndfs: tf.Tensor, v: tf.Tensor) -> tf.Tensor:
with tf.name_scope("WarpDistribution"):
ndf_amplitude, ndf_axis, ndf_sharpness = self._extract_sg_components(ndfs)
ret = tf.concat(
[
ndf_amplitude,
math_utils.reflect(-v, ndf_axis),
tf.math.divide_no_nan(
ndf_sharpness,
(4.0 * math_utils.saturate(math_utils.dot(ndf_axis, v), 1e-4)),
),
],
-1,
)
return ret
@tf.function
def _stack_sg_components(self, amplitude, axis, sharpness):
return tf.concat(
[
math_utils.safe_log(amplitude)
if self.compress_amplitude
else amplitude,
axis,
math_utils.safe_log(math_utils.saturate(sharpness, 0.5, 30))
if self.compress_sharpness
else sharpness,
],
-1,
)
@tf.function
def _extract_sg_components(
self, sg: tf.Tensor
) -> Tuple[tf.Tensor, tf.Tensor, tf.Tensor]:
with tf.name_scope("SG_Extract"):
s_amplitude = (
math_utils.safe_exp(sg[..., 0:3])
if self.compress_amplitude
else sg[..., 0:3]
)
s_axis = sg[..., 3:6]
s_sharpness = (
math_utils.safe_exp(sg[..., 6:7])
if self.compress_amplitude
else sg[..., 6:7]
)
return (
tf.abs(s_amplitude),
math_utils.normalize(s_axis),
math_utils.saturate(s_sharpness, 0.5, 30),
)
@tf.function
def _sg_integral(self, sg: tf.Tensor) -> tf.Tensor:
with tf.name_scope("SG_Integral"):
s_amplitude, _, s_sharpness = self._extract_sg_components(sg)
expTerm = 1.0 - math_utils.safe_exp(-2.0 * s_sharpness)
return 2 * np.pi * tf.math.divide_no_nan(s_amplitude, s_sharpness) * expTerm
@tf.function
def _sg_inner_product(self, sg1: tf.Tensor, sg2: tf.Tensor) -> tf.Tensor:
with tf.name_scope("SG_InnerProd"):
s1_amplitude, s1_axis, s1_sharpness = self._extract_sg_components(sg1)
s2_amplitude, s2_axis, s2_sharpness = self._extract_sg_components(sg2)
umLength = math_utils.magnitude(
s1_sharpness * s1_axis + s2_sharpness * s2_axis
)
expo = (
math_utils.safe_exp(umLength - s1_sharpness - s2_sharpness)
* s1_amplitude
* s2_amplitude
)
other = 1.0 - math_utils.safe_exp(-2.0 * umLength)
return tf.math.divide_no_nan(2.0 * np.pi * expo * other, umLength)
@tf.function
def _sg_evaluate(self, sg: tf.Tensor, d: tf.Tensor) -> tf.Tensor:
with tf.name_scope("SG_Evaluate"):
s_amplitude, s_axis, s_sharpness = self._extract_sg_components(sg)
cosAngle = math_utils.dot(d, s_axis)
return s_amplitude * math_utils.safe_exp(s_sharpness * (cosAngle - 1.0))
@tf.function
def visualize_fit(self, shape, sgs: tf.Tensor):
if len(sgs.shape) == 4:
if tf.shape(sgs)[0] == 1:
sgs = sgs[0]
print(sgs.shape)
shape = (
shape[0],
shape[1],
3,
)
output = tf.zeros([shape[0] * shape[1], shape[2]], dtype=tf.float32)
uvs = math_utils.shape_to_uv(shape[0], shape[1])
d = math_utils.uv_to_direction(uvs)
d = tf.reshape(d, [-1, 1, 3])
for i in range(sgs.shape[1]):
sg = sgs[:, i : i + 1]
output += self._sg_evaluate(sg, d)[:, 0]
output = tf.reshape(output, shape)
return output