{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Shadows {#light_shadows_example}\n\nDemonstrate the usage of lights and shadows in PyVista.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nimport pyvista\nfrom pyvista import examples\n\nmesh = examples.download_dragon()\nmesh.rotate_x(90, inplace=True)\nmesh.rotate_z(120, inplace=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using two lights, plot the Stanford Dragon with shadows.\n\n::: note\n::: title\nNote\n:::\n\nVTK has known issues when rendering shadows on certain window sizes. Be\nprepared to experiment with the `window_size` parameter. An initial\nwindow size of `(1000, 1000)` seems to work well, which can be manually\nresized without issue.\n:::\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "light1 = pyvista.Light(\n position=(0, 0.2, 1.0),\n focal_point=(0, 0, 0),\n color=[1.0, 1.0, 0.9843, 1.0], # Color temp. 5400 K\n intensity=0.3,\n)\n\nlight2 = pyvista.Light(\n position=(0, 1.0, 1.0),\n focal_point=(0, 0, 0),\n color=[1.0, 0.83921, 0.6666, 1.0], # Color temp. 2850 K\n intensity=1,\n)\n\n# Add a thin box below the mesh\nbounds = mesh.bounds\nrnge = (bounds[1] - bounds[0], bounds[3] - bounds[2], bounds[5] - bounds[4])\n\nexpand = 1.0\nheight = rnge[2] * 0.05\ncenter = np.array(mesh.center)\ncenter -= [0, 0, mesh.center[2] - bounds[4] + height / 2]\n\nwidth = rnge[0] * (1 + expand)\nlength = rnge[1] * (1 + expand)\nbase_mesh = pyvista.Cube(center, width, length, height)\n\n# rotate base and mesh to get a better view\nbase_mesh.rotate_z(30, inplace=True)\nmesh.rotate_z(30, inplace=True)\n\n# create the plotter with custom lighting\npl = pyvista.Plotter(lighting=None, window_size=(800, 800))\npl.add_light(light1)\npl.add_light(light2)\npl.add_mesh(\n mesh,\n ambient=0.2,\n diffuse=0.5,\n specular=0.5,\n specular_power=90,\n smooth_shading=True,\n color='orange',\n)\npl.add_mesh(base_mesh)\npl.enable_shadows()\npl.camera.zoom(1.5)\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Show light penetrating several planes. Adjust the light intensity and\nthe `shadow_attenuation` to change how many planes the light can go\nthrough.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plotter = pyvista.Plotter(lighting=None, window_size=(800, 800))\n\n# add several planes\nfor plane_y in [2, 5, 10]:\n screen = pyvista.Plane(center=(0, plane_y, 0), direction=(0, 1, 0), i_size=5, j_size=5)\n plotter.add_mesh(screen, color='white')\n\nlight = pyvista.Light(\n position=(0, 0, 0),\n focal_point=(0, 1, 0),\n color='cyan',\n intensity=15,\n positional=True,\n cone_angle=15,\n attenuation_values=(2, 0, 0),\n)\nlight.show_actor()\n\nplotter.add_light(light)\nplotter.view_vector((1, -2, 2))\nplotter.enable_shadows()\nplotter.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, we use a lower shadow_attenuation value to demonstrate how the\nlight can travel through more planes.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plotter = pyvista.Plotter(lighting=None, window_size=(800, 800))\n\n# add several planes\nfor plane_y in [2, 5, 10]:\n screen = pyvista.Plane(center=(0, plane_y, 0), direction=(0, 1, 0), i_size=5, j_size=5)\n plotter.add_mesh(screen, color='white')\n\nlight = pyvista.Light(\n position=(0, 0, 0),\n focal_point=(0, 1, 0),\n color='cyan',\n intensity=15,\n cone_angle=15,\n shadow_attenuation=0.95,\n positional=True,\n attenuation_values=(2, 0, 0),\n)\nlight.show_actor()\n\nplotter.add_light(light)\nplotter.view_vector((1, -2, 2))\nplotter.enable_shadows()\nplotter.show()" ] } ], "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.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }