{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Texture Filter on Meshes\n\nShow how to use the texture filter on meshes.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import argparse\n\nimport numpy as np\nfrom vispy import app, scene\nfrom vispy.io import imread, load_data_file, read_mesh\nfrom vispy.scene.visuals import Mesh\nfrom vispy.scene import transforms\nfrom vispy.visuals.filters import TextureFilter\n\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--shading', default='smooth',\n choices=['none', 'flat', 'smooth'],\n help=\"shading mode\")\nargs, _ = parser.parse_known_args()\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',\n size=(800, 600))\nview = canvas.central_widget.add_view()\n\nview.camera = 'arcball'\n# Adapt the depth to the scale of the mesh to avoid rendering artefacts.\nview.camera.depth_value = 10 * (vertices.max() - vertices.min())\n\nshading = None if args.shading == 'none' else args.shading\nmesh = Mesh(vertices, faces, shading=shading, color='white')\nmesh.transform = transforms.MatrixTransform()\nmesh.transform.rotate(90, (1, 0, 0))\nmesh.transform.rotate(135, (0, 0, 1))\nmesh.shading_filter.shininess = 1e+1\nview.add(mesh)\n\ntexture_filter = TextureFilter(texture, texcoords)\nmesh.attach(texture_filter)\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 mesh.update()\n\n\ndef attach_headlight(mesh, view, canvas):\n light_dir = (0, 1, 0, 0)\n mesh.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 mesh.shading_filter.light_dir = transform.map(initial_light_dir)[:3]\n\n\nattach_headlight(mesh, view, canvas)\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 }