-
Notifications
You must be signed in to change notification settings - Fork 165
/
material.py
352 lines (303 loc) · 13.7 KB
/
material.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
import warnings
from fury.shaders import add_shader_callback, load, shader_to_actor
from fury.lib import VTK_OBJECT, calldata_type
class __PBRParams:
"""Helper class to manage PBR parameters.
Attributes
----------
actor_properties : vtkProperty
The actor properties.
Parameters
----------
metallic : float
Metallic or non-metallic (dielectric) shading computation value. Values
must be between 0.0 and 1.0.
roughness : float
Parameter used to specify how glossy the actor should be. Values must
be between 0.0 and 1.0.
anisotropy : float
Isotropic or anisotropic material parameter. Values must be between
0.0 and 1.0.
anisotropy_rotation : float
Rotation of the anisotropy around the normal in a counter-clockwise
fashion. Values must be between 0.0 and 1.0. A value of 1.0 means a
rotation of 2 * pi.
coat_strength : float
Strength of the coat layer. Values must be between 0.0 and 1.0 (0.0
means no clear coat will be modeled).
coat_roughness : float
Roughness of the coat layer. Values must be between 0.0 and 1.0.
base_ior : float
Index of refraction of the base material. Default is 1.5. Values must
be between 1.0 and 2.3.
coat_ior : float
Index of refraction of the coat material. Default is 1.5. Values must
be between 1.0 and 2.3.
"""
def __init__(self, actor_properties, metallic, roughness,
anisotropy, anisotropy_rotation, coat_strength,
coat_roughness, base_ior, coat_ior):
self.__actor_properties = actor_properties
self.__actor_properties.SetMetallic(metallic)
self.__actor_properties.SetRoughness(roughness)
self.__actor_properties.SetAnisotropy(anisotropy)
self.__actor_properties.SetAnisotropyRotation(
anisotropy_rotation)
self.__actor_properties.SetCoatStrength(coat_strength)
self.__actor_properties.SetCoatRoughness(coat_roughness)
self.__actor_properties.SetBaseIOR(base_ior)
self.__actor_properties.SetCoatIOR(coat_ior)
@property
def metallic(self):
return self.__actor_properties.GetMetallic()
@metallic.setter
def metallic(self, metallic):
self.__actor_properties.SetMetallic(metallic)
@property
def roughness(self):
return self.__actor_properties.GetRoughness()
@roughness.setter
def roughness(self, roughness):
self.__actor_properties.SetRoughness(roughness)
@property
def anisotropy(self):
return self.__actor_properties.GetAnisotropy()
@anisotropy.setter
def anisotropy(self, anisotropy):
self.__actor_properties.SetAnisotropy(anisotropy)
@property
def anisotropy_rotation(self):
return self.__actor_properties.GetAnisotropyRotation()
@anisotropy_rotation.setter
def anisotropy_rotation(self, anisotropy_rotation):
self.__actor_properties.SetAnisotropyRotation(anisotropy_rotation)
@property
def coat_strength(self):
return self.__actor_properties.GetCoatStrength()
@coat_strength.setter
def coat_strength(self, coat_strength):
self.__actor_properties.SetCoatStrength(coat_strength)
@property
def coat_roughness(self):
return self.__actor_properties.GetCoatRoughness()
@coat_roughness.setter
def coat_roughness(self, coat_roughness):
self.__actor_properties.SetCoatRoughness(coat_roughness)
@property
def base_ior(self):
return self.__actor_properties.GetBaseIOR()
@base_ior.setter
def base_ior(self, base_ior):
self.__actor_properties.SetBaseIOR(base_ior)
@property
def coat_ior(self):
return self.__actor_properties.GetCoatIOR()
@coat_ior.setter
def coat_ior(self, coat_ior):
self.__actor_properties.SetCoatIOR(coat_ior)
def manifest_pbr(actor, metallic=0, roughness=.5, anisotropy=0,
anisotropy_rotation=0, coat_strength=0, coat_roughness=0,
base_ior=1.5, coat_ior=2):
"""Apply VTK's Physically Based Rendering properties to the selected actor.
Parameters
----------
actor : actor
metallic : float, optional
Metallic or non-metallic (dielectric) shading computation value. Values
must be between 0.0 and 1.0.
roughness : float, optional
Parameter used to specify how glossy the actor should be. Values must
be between 0.0 and 1.0.
anisotropy : float, optional
Isotropic or anisotropic material parameter. Values must be between
0.0 and 1.0.
anisotropy_rotation : float, optional
Rotation of the anisotropy around the normal in a counter-clockwise
fashion. Values must be between 0.0 and 1.0. A value of 1.0 means a
rotation of 2 * pi.
coat_strength : float, optional
Strength of the coat layer. Values must be between 0.0 and 1.0 (0.0
means no clear coat will be modeled).
coat_roughness : float, optional
Roughness of the coat layer. Values must be between 0.0 and 1.0.
base_ior : float, optional
Index of refraction of the base material. Default is 1.5. Values must
be between 1.0 and 2.3.
coat_ior : float, optional
Index of refraction of the coat material. Default is 1.5. Values must
be between 1.0 and 2.3.
"""
try:
prop = actor.GetProperty()
try:
prop.SetInterpolationToPBR()
#pbr_params = {'metallic': metallic, 'roughness': roughness,
# 'anisotropy': anisotropy,
# 'anisotropy_rotation': anisotropy_rotation,
# 'coat_strength': coat_strength,
# 'coat_roughness': coat_roughness,
# 'base_ior': base_ior, 'coat_ior': coat_ior}
pbr_params = __PBRParams(prop, metallic, roughness, anisotropy,
anisotropy_rotation, coat_strength,
coat_roughness, base_ior, coat_ior)
#prop.SetMetallic(pbr_params['metallic'])
#prop.SetRoughness(pbr_params['roughness'])
#prop.SetAnisotropy(pbr_params['anisotropy'])
#prop.SetAnisotropyRotation(pbr_params['anisotropy_rotation'])
#prop.SetCoatStrength(pbr_params['coat_strength'])
#prop.SetCoatRoughness(pbr_params['coat_roughness'])
#prop.SetBaseIOR(pbr_params['base_ior'])
#prop.SetCoatIOR(pbr_params['coat_ior'])
return pbr_params
except AttributeError:
warnings.warn(
'PBR interpolation cannot be applied to this actor. The '
'material will not be applied.')
return None
except AttributeError:
warnings.warn('Actor does not have the attribute property. This '
'material will not be applied.')
return None
def manifest_principled(actor, subsurface=0, subsurface_color=[0, 0, 0],
metallic=0, specular=0, specular_tint=0, roughness=0,
anisotropic=0, anisotropic_direction=[0, 1, .5],
sheen=0, sheen_tint=0, clearcoat=0, clearcoat_gloss=0):
"""Apply the Principled Shading properties to the selected actor.
Parameters
----------
actor : actor
subsurface : float, optional
Subsurface scattering computation value. Values must be between 0.0 and
1.0.
subsurface_color : list, optional
Subsurface scattering RGB color where R, G and B should be in the range
[0, 1].
metallic : float, optional
Metallic or non-metallic (dielectric) shading computation value. Values
must be between 0.0 and 1.0.
specular : float, optional
Specular lighting coefficient. Value must be between 0.0 and 1.0.
specular_tint : float, optional
Specular tint coefficient value. Values must be between 0.0 and 1.0.
roughness : float, optional
Parameter used to specify how glossy the actor should be. Values must
be between 0.0 and 1.0.
anisotropic : float, optional
Anisotropy coefficient. Values must be between 0.0 and 1.0.
anisotropic_direction : list, optional
Anisotropy direction where X, Y and Z should be in the range [-1, 1].
sheen : float, optional
Sheen coefficient. Values must be between 0.0 and 1.0.
sheen_tint : float, optional
Sheen tint coefficient value. Values must be between 0.0 and 1.0.
clearcoat : float, optional
Clearcoat coefficient. Values must be between 0.0 and 1.0.
clearcoat_gloss : float, optional
Clearcoat gloss coefficient value. Values must be between 0.0 and 1.0.
Returns
-------
principled_params : dict
Dictionary containing the Principled Shading parameters.
"""
try:
prop = actor.GetProperty()
principled_params = {
'subsurface': subsurface, 'subsurface_color': subsurface_color,
'metallic': metallic, 'specular': specular,
'specular_tint': specular_tint, 'roughness': roughness,
'anisotropic': anisotropic,
'anisotropic_direction': anisotropic_direction, 'sheen': sheen,
'sheen_tint': sheen_tint, 'clearcoat': clearcoat,
'clearcoat_gloss': clearcoat_gloss
}
prop.SetSpecular(specular)
@calldata_type(VTK_OBJECT)
def uniforms_callback(_caller, _event, calldata=None):
if calldata is not None:
calldata.SetUniformf(
'subsurface', principled_params['subsurface'])
calldata.SetUniformf(
'metallic', principled_params['metallic'])
calldata.SetUniformf(
'specularTint', principled_params['specular_tint'])
calldata.SetUniformf(
'roughness', principled_params['roughness'])
calldata.SetUniformf(
'anisotropic', principled_params['anisotropic'])
calldata.SetUniformf('sheen', principled_params['sheen'])
calldata.SetUniformf(
'sheenTint', principled_params['sheen_tint'])
calldata.SetUniformf(
'clearcoat', principled_params['clearcoat'])
calldata.SetUniformf(
'clearcoatGloss', principled_params['clearcoat_gloss'])
calldata.SetUniform3f(
'subsurfaceColor', principled_params['subsurface_color'])
calldata.SetUniform3f(
'anisotropicDirection', principled_params[
'anisotropic_direction'])
add_shader_callback(actor, uniforms_callback)
fs_dec_code = load('bxdf_dec.frag')
fs_impl_code = load('bxdf_impl.frag')
shader_to_actor(actor, 'fragment', decl_code=fs_dec_code)
shader_to_actor(actor, 'fragment', impl_code=fs_impl_code,
block='light')
return principled_params
except AttributeError:
warnings.warn('Actor does not have the attribute property. This '
'material will not be applied.')
return None
def manifest_standard(actor, ambient_level=0, ambient_color=(1, 1, 1),
diffuse_level=1, diffuse_color=(1, 1, 1),
specular_level=0, specular_color=(1, 1, 1),
specular_power=1, interpolation='gouraud'):
"""Apply the standard material to the selected actor.
Parameters
----------
actor : actor
ambient_level : float, optional
Ambient lighting coefficient. Value must be between 0.0 and 1.0.
ambient_color : tuple (3,), optional
Ambient RGB color where R, G and B should be in the range [0, 1].
diffuse_level : float, optional
Diffuse lighting coefficient. Value must be between 0.0 and 1.0.
diffuse_color : tuple (3,), optional
Diffuse RGB color where R, G and B should be in the range [0, 1].
specular_level : float, optional
Specular lighting coefficient. Value must be between 0.0 and 1.0.
specular_color : tuple (3,), optional
Specular RGB color where R, G and B should be in the range [0, 1].
specular_power : float, optional
Parameter used to specify the intensity of the specular reflection.
Value must be between 0.0 and 128.0.
interpolation : string, optional
If 'flat', the actor will be shaded using flat interpolation. If
'gouraud' (default), then the shading will be calculated at the
vertex level. If 'phong', then the shading will be calculated at the
fragment level.
"""
try:
prop = actor.GetProperty()
interpolation = interpolation.lower()
if interpolation == 'flat':
prop.SetInterpolationToFlat()
elif interpolation == 'gouraud':
prop.SetInterpolationToGouraud()
elif interpolation == 'phong':
prop.SetInterpolationToPhong()
else:
message = 'Unknown interpolation. Ignoring "{}" interpolation ' \
'option and using the default ("{}") option.'
message = message.format(interpolation, 'gouraud')
warnings.warn(message)
prop.SetAmbient(ambient_level)
prop.SetAmbientColor(ambient_color)
prop.SetDiffuse(diffuse_level)
prop.SetDiffuseColor(diffuse_color)
prop.SetSpecular(specular_level)
prop.SetSpecularColor(specular_color)
prop.SetSpecularPower(specular_power)
except AttributeError:
warnings.warn('Actor does not have the attribute property. This '
'material will not be applied.')
return