.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/13_shaders/viz_shader.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_13_shaders_viz_shader.py: ============= Varying Color ============= This example shows how to use shaders to generate a shaded output. We will demonstrate how to load polydata then use a custom shader calls to render a custom shaded model. First, a bunch of imports. .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: Python from fury import io, ui, utils, window from fury.data.fetcher import fetch_viz_models, read_viz_models from fury.shaders import add_shader_callback, shader_to_actor .. GENERATED FROM PYTHON SOURCE LINES 18-19 Let's download and load the model .. GENERATED FROM PYTHON SOURCE LINES 19-25 .. code-block:: Python fetch_viz_models() model = read_viz_models('utah.obj') .. rst-class:: sphx-glr-script-out .. code-block:: none Dataset is already in place. If you want to fetch it again please first remove the folder /Users/skoudoro/.fury/models .. GENERATED FROM PYTHON SOURCE LINES 26-29 Let's start by loading the polydata of choice. For this example we use the standard utah teapot model. currently supported formats include OBJ, VTK, FIB, PLY, STL and XML .. GENERATED FROM PYTHON SOURCE LINES 30-37 .. code-block:: Python utah = io.load_polydata(model) utah = utils.get_polymapper_from_polydata(utah) utah = utils.get_actor_from_polymapper(utah) mapper = utah.GetMapper() .. GENERATED FROM PYTHON SOURCE LINES 38-41 To change the default shader we add a shader replacement. Specify vertex shader using vtkShader.Vertex Specify fragment shader using vtkShader.Fragment .. GENERATED FROM PYTHON SOURCE LINES 41-67 .. code-block:: Python vertex_shader_code_decl = """ out vec4 myVertexVC; """ vertex_shader_code_impl = """ myVertexVC = vertexMC; """ fragment_shader_code_decl = """ uniform float time; varying vec4 myVertexVC; """ fragment_shader_code_impl = """ vec2 iResolution = vec2(1024,720); vec2 uv = myVertexVC.xy/iResolution; vec3 col = 0.5 + 0.5 * cos((time/30) + uv.xyx + vec3(0, 2, 4)); fragOutput0 = vec4(col, fragOutput0.a); """ shader_to_actor( utah, 'vertex', impl_code=vertex_shader_code_impl, decl_code=vertex_shader_code_decl ) shader_to_actor(utah, 'fragment', decl_code=fragment_shader_code_decl) shader_to_actor(utah, 'fragment', impl_code=fragment_shader_code_impl, block='light') .. GENERATED FROM PYTHON SOURCE LINES 68-69 Let's create a scene. .. GENERATED FROM PYTHON SOURCE LINES 69-75 .. code-block:: Python scene = window.Scene() global timer timer = 0 .. GENERATED FROM PYTHON SOURCE LINES 76-77 The timer will call this user defined callback every 30 milliseconds. .. GENERATED FROM PYTHON SOURCE LINES 77-86 .. code-block:: Python def timer_callback(obj, event): global timer timer += 1.0 showm.render() scene.azimuth(5) .. GENERATED FROM PYTHON SOURCE LINES 87-89 The shader callback will update the color of our utah pot via the update of the timer variable. .. GENERATED FROM PYTHON SOURCE LINES 89-102 .. code-block:: Python 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) .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 103-104 Let's add a textblock to the scene with a custom message .. GENERATED FROM PYTHON SOURCE LINES 104-108 .. code-block:: Python tb = ui.TextBlock2D() tb.message = 'Hello Shaders' .. GENERATED FROM PYTHON SOURCE LINES 109-113 Show Manager Now that all the elements have been initialised, we add them to the show manager. .. GENERATED FROM PYTHON SOURCE LINES 113-128 .. code-block:: Python current_size = (1024, 720) showm = window.ShowManager(scene, size=current_size, reset_camera=False) showm.add_timer_callback(True, 30, timer_callback) scene.add(utah) scene.add(tb) interactive = False if interactive: showm.start() window.record(showm.scene, size=current_size, out_path='viz_shader.png') .. image-sg:: /auto_examples/13_shaders/images/sphx_glr_viz_shader_001.png :alt: viz shader :srcset: /auto_examples/13_shaders/images/sphx_glr_viz_shader_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.216 seconds) .. _sphx_glr_download_auto_examples_13_shaders_viz_shader.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_shader.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_shader.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_