.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/volume_render1.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__gallery_feature_demo_volume_render1.py: Volume Rendering 1 ================== Render a volume. Shift-click to draw white blobs inside the volume. .. GENERATED FROM PYTHON SOURCE LINES 7-61 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_volume_render1_001.webp :alt: volume render1 :srcset: /_gallery/feature_demo/images/sphx_glr_volume_render1_001.webp :class: sphx-glr-single-img .. code-block:: Python import imageio.v3 as iio import numpy as np from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx canvas = RenderCanvas() renderer = gfx.renderers.WgpuRenderer(canvas) scene = gfx.Scene() voldata = iio.imread("imageio:stent.npz").astype(np.float32) tex = gfx.Texture(voldata, dim=3) vol = gfx.Volume( gfx.Geometry(grid=tex), gfx.VolumeMipMaterial(clim=(0, 2000), map=gfx.cm.cividis, pick_write=True), ) slice = gfx.Volume( gfx.Geometry(grid=tex), gfx.VolumeSliceMaterial(plane=(0, 0, 1, 0), clim=(0, 2000)), ) scene.add(vol, slice) for ob in (slice, vol): ob.local.position = [-0.5 * i for i in voldata.shape[::-1]] camera = gfx.PerspectiveCamera(70, 16 / 9) camera.show_object(scene, view_dir=(-1, -1, -1), up=(0, 0, 1)) controller = gfx.OrbitController(camera, register_events=renderer) @vol.add_event_handler("pointer_down") def handle_event(event): if "Shift" not in event.modifiers: return info = event.pick_info if "index" in info: x, y, z = (max(1, int(i)) for i in info["index"]) print("Picking", x, y, z) tex.data[z - 1 : z + 1, y - 1 : y + 1, x - 1 : x + 1] = 2000 tex.update_range((x - 1, y - 1, z - 1), (3, 3, 3)) def animate(): renderer.render(scene, camera) canvas.request_draw() if __name__ == "__main__": canvas.request_draw(animate) loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.813 seconds) .. _sphx_glr_download__gallery_feature_demo_volume_render1.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: volume_render1.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: volume_render1.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: volume_render1.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_ .. only:: html Interactive example ------------------- Try this example in your browser using Pyodide. Might not work with all examples and all devices. Check the output and your browser's console for details. .. raw:: html