.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/picking_color.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_picking_color.py: Color Picking ============= Example showing picking the color from the scene. Depending on the object being clicked, more detailed picking info is available. .. GENERATED FROM PYTHON SOURCE LINES 8-57 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_picking_color_001.webp :alt: picking color :srcset: /_gallery/feature_demo/images/sphx_glr_picking_color_001.webp :class: sphx-glr-single-img .. code-block:: Python import numpy as np import imageio.v3 as iio from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx import pylinalg as la canvas = RenderCanvas() renderer = gfx.renderers.WgpuRenderer(canvas) scene = gfx.Scene() dark_gray = np.array((169, 167, 168, 255)) / 255 light_gray = np.array((100, 100, 100, 255)) / 255 background = gfx.Background.from_color(light_gray, dark_gray) scene.add(background) im = iio.imread("imageio:astronaut.png").astype(np.float32) / 255 tex = gfx.Texture(im, dim=2) geometry = gfx.box_geometry(200, 200, 200) material = gfx.MeshBasicMaterial(map=tex, pick_write=True) cube = gfx.Mesh(geometry, material) scene.add(cube) # camera = gfx.OrthographicCamera(300, 300) camera = gfx.PerspectiveCamera(70, 16 / 9) camera.show_object(cube, scale=1.5) @renderer.add_event_handler("pointer_down") def handle_event(event): info = event.pick_info for key, val in info.items(): print(key, "=", val) def animate(): rot = la.quat_from_euler((0.005, 0.01), order="XY") cube.local.rotation = la.quat_mul(rot, cube.local.rotation) 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.399 seconds) .. _sphx_glr_download__gallery_feature_demo_picking_color.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: picking_color.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: picking_color.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: picking_color.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