{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Instanced Mesh Visual\n\nShow usage of the InstancedMesh visual and its filters.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itertools import cycle\n\nimport numpy as np\nfrom scipy.spatial.transform import Rotation\nfrom vispy import app, scene, use\nfrom vispy.io import imread, load_data_file, read_mesh\nfrom vispy.scene.visuals import InstancedMesh\nfrom vispy.visuals.filters import InstancedShadingFilter, WireframeFilter, TextureFilter\n\n# needed for instanced rendering to work\nuse(gl='gl+')\n\n\nmesh_path = load_data_file('spot/spot.obj.gz')\ntexture_path = load_data_file('spot/spot.png')\nvertices, faces, normals, texcoords = read_mesh(mesh_path)\ntexture = np.flipud(imread(texture_path))\n\ncanvas = scene.SceneCanvas(keys='interactive', bgcolor='white', show=True)\nview = canvas.central_widget.add_view()\n\nview.camera = 'arcball'\nview.camera.depth_value = 10 * (vertices.max() - vertices.min())\n\nn_instances = 100\n\ninstance_colors = np.random.rand(n_instances, 3).astype(np.float32)\ninstance_positions = ((np.random.rand(n_instances, 3) - 0.5) * 10).astype(np.float32)\nface_colors = np.random.rand(len(faces), 3)\ninstance_transforms = Rotation.random(n_instances).as_matrix().astype(np.float32)\n\n# Create a colored `MeshVisual`.\nmesh = InstancedMesh(\n vertices,\n faces,\n instance_colors=instance_colors,\n face_colors=face_colors,\n instance_positions=instance_positions,\n instance_transforms=instance_transforms,\n parent=view.scene,\n)\n\n\nwireframe_filter = WireframeFilter(width=1)\nshading_filter = InstancedShadingFilter('smooth', shininess=1)\ntexture_filter = TextureFilter(texture, texcoords)\nmesh.attach(wireframe_filter)\nmesh.attach(shading_filter)\nmesh.attach(texture_filter)\n\n\ndef attach_headlight(view):\n light_dir = (0, 1, 0, 0)\n shading_filter.light_dir = light_dir[:3]\n initial_light_dir = view.camera.transform.imap(light_dir)\n\n @view.scene.transform.changed.connect\n def on_transform_change(event):\n transform = view.camera.transform\n shading_filter.light_dir = transform.map(initial_light_dir)[:3]\n\n\nattach_headlight(view)\n\n\nshading_cycle = cycle(['flat', None, 'smooth'])\ncolor_cycle = cycle([None, instance_colors])\nface_color_cycle = cycle([None, face_colors])\n\n\n@canvas.events.key_press.connect\ndef on_key_press(event):\n if event.key == \"t\":\n texture_filter.enabled = not texture_filter.enabled\n canvas.update()\n if event.key == 's':\n shading_filter.shading = next(shading_cycle)\n canvas.update()\n if event.key == 'c':\n mesh.instance_colors = next(color_cycle)\n canvas.update()\n if event.key == 'f':\n mesh.set_data(\n vertices=vertices,\n faces=faces,\n face_colors=next(face_color_cycle),\n )\n canvas.update()\n if event.key == 'w':\n wireframe_filter.enabled = not wireframe_filter.enabled\n canvas.update()\n\n\nif __name__ == \"__main__\":\n app.run()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.19" } }, "nbformat": 4, "nbformat_minor": 0 }