.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/feature_demo/quad_mesh.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_quad_mesh.py: Mesh with quads =============== This example demonstrates the use of meshes with quad faces. Quad faces are used when geometry.indices array is Nx4 instead of Nx3. This example also demonstrates per-vertex and per-face coloring. Contributed by S. Shaji .. GENERATED FROM PYTHON SOURCE LINES 12-125 .. image-sg:: /_gallery/feature_demo/images/sphx_glr_quad_mesh_001.webp :alt: quad mesh :srcset: /_gallery/feature_demo/images/sphx_glr_quad_mesh_001.webp :class: sphx-glr-single-img .. code-block:: Python import numpy as np from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx def generate_sample_quads(cols=9): pos = np.dstack(np.meshgrid(np.arange(cols), np.arange(2))).reshape(-1, 2) z = np.abs([*np.arange(-cols / 2, cols / 2), *np.arange(-cols / 2, cols / 2)]) pos = np.c_[pos, z].astype("f") n1 = np.arange(cols) n2 = np.full(cols - 1, 0) n3 = np.full(cols - 1, 1) idx = np.dstack((n1[:-1], n2, n1[:-1], n3, n1[1:], n3, n1[1:], n2)).reshape(-1, 2) indices = (idx[:, 0] + idx[:, 1] * cols).reshape(-1, 4) return pos, indices.astype(np.int32) canvas = RenderCanvas( title="Mesh Object with quads. Press 1,2 or 3 for wireframe, per vertex coloring or per face coloring" ) renderer = gfx.renderers.WgpuRenderer(canvas) # Show something scene = gfx.Scene() camera = gfx.PerspectiveCamera() controller = gfx.OrbitController(camera=camera, register_events=renderer) controller.controls["mouse3"] = ("pan", "drag", (1.0, 1.0)) # Generate Sample quads and draw them pos, indices = generate_sample_quads() colors = np.repeat(pos[:, -1] / pos[:, -1].max(), 4).reshape(-1, 4) colors[:, 2] = 1 colors[:, 3] = 1 patches = gfx.Mesh( gfx.Geometry( indices=indices, positions=pos, colors=colors, texcoords=np.linspace(0, 1, len(indices), dtype=np.float32), ), gfx.MeshBasicMaterial( color_mode="uniform", wireframe=True, wireframe_thickness=3, map=gfx.cm.magma, pick_write=True, ), ) scene.add(patches) camera.show_object(patches) # Let there be ... scene.add(gfx.AmbientLight()) light = gfx.DirectionalLight() light.local.position = (0, 0, 1) # Create a contrasting background clr = [i / 255 for i in [87, 188, 200, 255]] background = gfx.Background.from_color(clr) scene.add(background) def make_wireframe(): patches.material.wireframe = True patches.material.color_mode = "uniform" def make_vertex_color(): patches.material.wireframe = False patches.material.color_mode = "vertex" def make_face_color(): patches.material.wireframe = False patches.material.color_mode = "face" def make_face_color_map(): patches.material.wireframe = False patches.material.color_mode = "face_map" @renderer.add_event_handler("key_down") def on_key(e): if e.key == "1": make_wireframe() elif e.key == "2": make_vertex_color() elif e.key == "3": make_face_color() elif e.key == "4": make_face_color_map() @renderer.add_event_handler("click") def pick_id(event): print(event.pick_info) 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.322 seconds) .. _sphx_glr_download__gallery_feature_demo_quad_mesh.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: quad_mesh.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: quad_mesh.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: quad_mesh.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