{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Shading a Mesh\n\nShow mesh filter usage for shading (lighting) a mesh and displaying a wireframe.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import argparse\n\nfrom vispy import app, scene\nfrom vispy.io import read_mesh, load_data_file\nfrom vispy.scene.visuals import Mesh\nfrom vispy.scene import transforms\nfrom vispy.visuals.filters import ShadingFilter, WireframeFilter\n\n\nparser = argparse.ArgumentParser()\ndefault_mesh = load_data_file('orig/triceratops.obj.gz')\nparser.add_argument('--mesh', default=default_mesh)\nparser.add_argument('--shininess', default=100)\nparser.add_argument('--wireframe-width', default=1)\nargs, _ = parser.parse_known_args()\n\nvertices, faces, normals, texcoords = read_mesh(args.mesh)\n\ncanvas = scene.SceneCanvas(keys='interactive', bgcolor='white')\nview = canvas.central_widget.add_view()\n\nview.camera = 'arcball'\nview.camera.depth_value = 1e3\n\n# Create a colored `MeshVisual`.\nmesh = Mesh(vertices, faces, color=(.5, .7, .5, 1))\nmesh.transform = transforms.MatrixTransform()\nmesh.transform.rotate(90, (1, 0, 0))\nmesh.transform.rotate(-45, (0, 0, 1))\nview.add(mesh)\n\n# Use filters to affect the rendering of the mesh.\nwireframe_filter = WireframeFilter(width=args.wireframe_width)\n# Note: For convenience, this `ShadingFilter` would be created automatically by\n# the `MeshVisual with, e.g. `mesh = MeshVisual(..., shading='smooth')`. It is\n# created manually here for demonstration purposes.\nshading_filter = ShadingFilter(shininess=args.shininess)\n# The wireframe filter is attached before the shading filter otherwise the\n# wireframe is not shaded.\nmesh.attach(wireframe_filter)\nmesh.attach(shading_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\nshading_states = (\n dict(shading=None),\n dict(shading='flat'),\n dict(shading='smooth'),\n)\nshading_state_index = shading_states.index(\n dict(shading=shading_filter.shading))\n\nwireframe_states = (\n dict(wireframe_only=False, faces_only=False,),\n dict(wireframe_only=True, faces_only=False,),\n dict(wireframe_only=False, faces_only=True,),\n)\nwireframe_state_index = wireframe_states.index(dict(\n wireframe_only=wireframe_filter.wireframe_only,\n faces_only=wireframe_filter.faces_only,\n))\n\n\ndef cycle_state(states, index):\n new_index = (index + 1) % len(states)\n return states[new_index], new_index\n\n\n@canvas.events.key_press.connect\ndef on_key_press(event):\n global shading_state_index\n global wireframe_state_index\n if event.key == 's':\n state, shading_state_index = cycle_state(shading_states,\n shading_state_index)\n for attr, value in state.items():\n setattr(shading_filter, attr, value)\n mesh.update()\n elif event.key == 'w':\n wireframe_filter.enabled = not wireframe_filter.enabled\n mesh.update()\n elif event.key == 'f':\n state, wireframe_state_index = cycle_state(wireframe_states,\n wireframe_state_index)\n for attr, value in state.items():\n setattr(wireframe_filter, attr, value)\n mesh.update()\n\n\ncanvas.show()\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 }