From 1fdcb582e3a3c70e4a155513eba3fc1c2855383a Mon Sep 17 00:00:00 2001 From: Javier Guaje Date: Fri, 28 Jan 2022 14:08:07 -0500 Subject: [PATCH] Added helper class to manage PBR properties. --- docs/examples/viz_pbr_interactive.py | 57 +++++------ fury/material.py | 147 ++++++++++++++++++++++++--- 2 files changed, 156 insertions(+), 48 deletions(-) diff --git a/docs/examples/viz_pbr_interactive.py b/docs/examples/viz_pbr_interactive.py index e1e8400af..1e7c68a5e 100644 --- a/docs/examples/viz_pbr_interactive.py +++ b/docs/examples/viz_pbr_interactive.py @@ -22,21 +22,18 @@ def change_slice_metallic(slider): - global pbr_params, sphere - pbr_params['metallic'] = slider.value - sphere.GetProperty().SetMetallic(pbr_params['metallic']) + global pbr_params + pbr_params.metallic = slider.value def change_slice_roughness(slider): - global pbr_params, sphere - pbr_params['roughness'] = slider.value - sphere.GetProperty().SetRoughness(pbr_params['roughness']) + global pbr_params + pbr_params.roughness = slider.value def change_slice_anisotropy(slider): - global pbr_params, sphere - pbr_params['anisotropy'] = slider.value - sphere.GetProperty().SetAnisotropy(pbr_params['anisotropy']) + global pbr_params + pbr_params.anisotropy = slider.value def change_slice_anisotropy_direction_x(slider): @@ -61,34 +58,28 @@ def change_slice_anisotropy_direction_z(slider): def change_slice_anisotropy_rotation(slider): - global pbr_params, sphere - pbr_params['anisotropy_rotation'] = slider.value - sphere.GetProperty().SetAnisotropyRotation( - pbr_params['anisotropy_rotation']) + global pbr_params + pbr_params.anisotropy_rotation = slider.value def change_slice_coat_strength(slider): - global pbr_params, sphere - pbr_params['coat_strength'] = slider.value - sphere.GetProperty().SetCoatStrength(pbr_params['coat_strength']) + global pbr_params + pbr_params.coat_strength = slider.value def change_slice_coat_roughness(slider): - global pbr_params, sphere - pbr_params['coat_roughness'] = slider.value - sphere.GetProperty().SetCoatRoughness(pbr_params['coat_roughness']) + global pbr_params + pbr_params.coat_roughness = slider.value def change_slice_base_ior(slider): - global pbr_params, sphere - pbr_params['base_ior'] = slider.value - sphere.GetProperty().SetBaseIOR(pbr_params['base_ior']) + global pbr_params + pbr_params.base_ior = slider.value def change_slice_coat_ior(slider): - global pbr_params, sphere - pbr_params['coat_ior'] = slider.value - sphere.GetProperty().SetCoatIOR(pbr_params['coat_ior']) + global pbr_params + pbr_params.coat_ior = slider.value """ @@ -227,22 +218,22 @@ def win_callback(obj, event): """ slider_slice_metallic = ui.LineSlider2D( - initial_value=pbr_params['metallic'], max_value=1, length=195, + initial_value=pbr_params.metallic, max_value=1, length=195, text_template='{value:.1f}') slider_slice_roughness = ui.LineSlider2D( - initial_value=pbr_params['roughness'], max_value=1, length=195, + initial_value=pbr_params.roughness, max_value=1, length=195, text_template='{value:.1f}') slider_slice_anisotropy = ui.LineSlider2D( - initial_value=pbr_params['anisotropy'], max_value=1, length=195, + initial_value=pbr_params.anisotropy, max_value=1, length=195, text_template='{value:.1f}') slider_slice_anisotropy_rotation = ui.LineSlider2D( - initial_value=pbr_params['anisotropy_rotation'], max_value=1, length=195, + initial_value=pbr_params.anisotropy_rotation, max_value=1, length=195, text_template='{value:.1f}') slider_slice_coat_strength = ui.LineSlider2D( - initial_value=pbr_params['coat_strength'], max_value=1, length=195, + initial_value=pbr_params.coat_strength, max_value=1, length=195, text_template='{value:.1f}') slider_slice_coat_roughness = ui.LineSlider2D( - initial_value=pbr_params['coat_roughness'], max_value=1, length=195, + initial_value=pbr_params.coat_roughness, max_value=1, length=195, text_template='{value:.1f}') """ @@ -268,10 +259,10 @@ def win_callback(obj, event): """ slider_slice_base_ior = ui.LineSlider2D( - initial_value=pbr_params['base_ior'], min_value=1, max_value=2.3, + initial_value=pbr_params.base_ior, min_value=1, max_value=2.3, length=195, text_template='{value:.02f}') slider_slice_coat_ior = ui.LineSlider2D( - initial_value=pbr_params['coat_ior'], min_value=1, max_value=2.3, + initial_value=pbr_params.coat_ior, min_value=1, max_value=2.3, length=195, text_template='{value:.02f}') """ diff --git a/fury/material.py b/fury/material.py index 54744a79d..98b69afb4 100644 --- a/fury/material.py +++ b/fury/material.py @@ -5,6 +5,120 @@ 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): @@ -44,21 +158,24 @@ def manifest_pbr(actor, metallic=0, roughness=.5, anisotropy=0, 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} - - 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']) + #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(