def test_add_shader_callback(): cube = generate_cube_with_effect() showm = window.ShowManager() showm.scene.add(cube) class Timer(object): idx = 0.0 timer = Timer() def timer_callback(obj, event): # nonlocal timer, showm timer.idx += 1.0 showm.render() if timer.idx > 90: showm.exit() def my_cbk(_caller, _event, calldata=None): program = calldata if program is not None: try: program.SetUniformf("time", timer.idx) except ValueError: pass add_shader_callback(cube, my_cbk) showm.initialize() showm.add_timer_callback(True, 100, timer_callback) showm.start() arr = window.snapshot(showm.scene, offscreen=True) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 1)
def test_shader_callback(): cone = vtk.vtkConeSource() coneMapper = vtk.vtkPolyDataMapper() coneMapper.SetInputConnection(cone.GetOutputPort()) actor = vtk.vtkActor() actor.SetMapper(coneMapper) test_values = [] def callbackLow(_caller, _event, calldata=None): program = calldata if program is not None: test_values.append(0) id_observer = fs.add_shader_callback( actor, callbackLow, 0) with pytest.raises(Exception): fs.add_shader_callback(actor, callbackLow, priority='str') mapper = actor.GetMapper() mapper.RemoveObserver(id_observer) scene = window.Scene() scene.add(actor) window.snapshot(scene) assert len(test_values) == 0 test_values = [] def callbackHigh(_caller, _event, calldata=None): program = calldata if program is not None: test_values.append(999) def callbackMean(_caller, _event, calldata=None): program = calldata if program is not None: test_values.append(500) fs.add_shader_callback( actor, callbackHigh, 999) fs.add_shader_callback( actor, callbackLow, 0) id_mean = fs.add_shader_callback( actor, callbackMean, 500) # check the priority of each call window.snapshot(scene) assert np.abs([ test_values[0]-999, test_values[1]-500, test_values[2]-0]).sum() == 0 # check if the correct observer was removed mapper.RemoveObserver(id_mean) test_values = [] window.snapshot(scene) assert np.abs([ test_values[0]-999, test_values[1]-0]).sum() == 0
############################################################################### # The shader callback will update the color of our utah pot via the update of # the timer variable. def shader_callback(_caller, _event, calldata=None): program = calldata global timer if program is not None: try: program.SetUniformf("time", timer) except ValueError: pass add_shader_callback(utah, shader_callback) ############################################################################### # Let's add a textblock to the scene with a custom message tb = ui.TextBlock2D() tb.message = "Hello Shaders" ############################################################################### # Show Manager # # Now that all the elements have been initialised, we add them to the show # manager. current_size = (1024, 720) showm = window.ShowManager(scene, size=current_size, reset_camera=False)
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 = import_fury_shader('bxdf_dec.frag') fs_impl_code = import_fury_shader('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 test_add_shader_callback(): cube = generate_cube_with_effect() showm = window.ShowManager() showm.scene.add(cube) class Timer(object): idx = 0.0 timer = Timer() def timer_callback(obj, event): # nonlocal timer, showm timer.idx += 1.0 showm.render() if timer.idx > 90: showm.exit() def my_cbk(_caller, _event, calldata=None): program = calldata if program is not None: try: program.SetUniformf("time", timer.idx) except ValueError: pass add_shader_callback(cube, my_cbk) showm.initialize() showm.add_timer_callback(True, 100, timer_callback) showm.start() arr = window.snapshot(showm.scene, offscreen=True) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 1) cone_actor = actor.cone(np.array([[0, 0, 0]]), np.array([[0, 1, 0]]), (0, 0, 1)) test_values = [] def callbackLow(_caller, _event, calldata=None): program = calldata if program is not None: test_values.append(0) id_observer = add_shader_callback(cone_actor, callbackLow, 0) with pytest.raises(Exception): add_shader_callback(cone_actor, callbackLow, priority='str') mapper = cone_actor.GetMapper() mapper.RemoveObserver(id_observer) scene = window.Scene() scene.add(cone_actor) arr1 = window.snapshot(scene, size=(200, 200)) assert len(test_values) == 0 test_values = [] def callbackHigh(_caller, _event, calldata=None): program = calldata if program is not None: test_values.append(999) def callbackMean(_caller, _event, calldata=None): program = calldata if program is not None: test_values.append(500) add_shader_callback(cone_actor, callbackHigh, 999) add_shader_callback(cone_actor, callbackLow, 0) id_mean = add_shader_callback(cone_actor, callbackMean, 500) # check the priority of each call arr2 = window.snapshot(scene, size=(200, 200)) assert np.abs( [test_values[0] - 999, test_values[1] - 500, test_values[2] - 0]).sum() == 0 # check if the correct observer was removed mapper.RemoveObserver(id_mean) test_values = [] arr3 = window.snapshot(scene, size=(200, 200)) assert np.abs([test_values[0] - 999, test_values[1] - 0]).sum() == 0