| 1 |
""" |
| 2 |
.. _shadows_example: |
| 3 |
|
| 4 |
Shadows |
| 5 |
~~~~~~~ |
| 6 |
|
| 7 |
Demonstrate the usage of lights and shadows in PyVista with :class:`~pyvista.Light` |
| 8 |
objects and the :meth:`~pyvista.Plotter.enable_shadows` plotting method. |
| 9 |
""" |
| 10 |
|
| 11 |
import numpy as np |
| 12 |
|
| 13 |
# sphinx_gallery_thumbnail_number = 2 |
| 14 |
import pyvista as pv |
| 15 |
from pyvista import examples |
| 16 |
|
| 17 |
mesh = examples.download_dragon() |
| 18 |
mesh.rotate_x(90, inplace=True) |
| 19 |
mesh.rotate_z(120, inplace=True) |
| 20 |
|
| 21 |
|
| 22 |
# %% |
| 23 |
# Using two lights, plot the Stanford Dragon with shadows. |
| 24 |
# |
| 25 |
# .. Note:: |
| 26 |
# VTK has known issues when rendering shadows on certain window |
| 27 |
# sizes. Be prepared to experiment with the ``window_size`` |
| 28 |
# parameter. An initial window size of ``(1000, 1000)`` seems to |
| 29 |
# work well, which can be manually resized without issue. |
| 30 |
|
| 31 |
light1 = pv.Light( |
| 32 |
position=(0, 0.2, 1.0), |
| 33 |
focal_point=(0, 0, 0), |
| 34 |
color=[1.0, 1.0, 0.9843, 1.0], # Color temp. 5400 K |
| 35 |
intensity=0.3, |
| 36 |
) |
| 37 |
|
| 38 |
light2 = pv.Light( |
| 39 |
position=(0, 1.0, 1.0), |
| 40 |
focal_point=(0, 0, 0), |
| 41 |
color=[1.0, 0.83921, 0.6666, 1.0], # Color temp. 2850 K |
| 42 |
intensity=1, |
| 43 |
) |
| 44 |
|
| 45 |
# Add a thin box below the mesh |
| 46 |
bnds = mesh.bounds |
| 47 |
x_size, y_size, z_size = mesh.bounds_size |
| 48 |
|
| 49 |
expand = 1.0 |
| 50 |
height = z_size * 0.05 |
| 51 |
center = np.array(mesh.center) |
| 52 |
center -= [0, 0, mesh.center[2] - bnds.z_min + height / 2] |
| 53 |
width = x_size * (1 + expand) |
| 54 |
length = y_size * (1 + expand) |
| 55 |
base_mesh = pv.Cube(center=center, x_length=width, y_length=length, z_length=height) |
| 56 |
|
| 57 |
# rotate base and mesh to get a better view |
| 58 |
base_mesh.rotate_z(30, inplace=True) |
| 59 |
mesh.rotate_z(30, inplace=True) |
| 60 |
|
| 61 |
# create the plotter with custom lighting |
| 62 |
pl = pv.Plotter(lighting=None, window_size=(800, 800)) |
| 63 |
pl.add_light(light1) |
| 64 |
pl.add_light(light2) |
| 65 |
pl.add_mesh( |
| 66 |
mesh, |
| 67 |
ambient=0.2, |
| 68 |
diffuse=0.5, |
| 69 |
specular=0.5, |
| 70 |
specular_power=90, |
| 71 |
smooth_shading=True, |
| 72 |
color='orange', |
| 73 |
) |
| 74 |
pl.add_mesh(base_mesh) |
| 75 |
pl.enable_shadows() |
| 76 |
pl.camera.zoom(1.5) |
| 77 |
pl.show() |
| 78 |
|
| 79 |
|
| 80 |
# %% |
| 81 |
# Show light penetrating several planes. Adjust the light intensity |
| 82 |
# and the ``shadow_attenuation`` to change how many planes the |
| 83 |
# light can go through. |
| 84 |
|
| 85 |
pl = pv.Plotter(lighting=None, window_size=(800, 800)) |
| 86 |
|
| 87 |
# add several planes |
| 88 |
for plane_y in [2, 5, 10]: |
| 89 |
screen = pv.Plane(center=(0, plane_y, 0), direction=(0, 1, 0), i_size=5, j_size=5) |
| 90 |
pl.add_mesh(screen, color='white') |
| 91 |
|
| 92 |
light = pv.Light( |
| 93 |
position=(0, 0, 0), |
| 94 |
focal_point=(0, 1, 0), |
| 95 |
color='cyan', |
| 96 |
intensity=15, |
| 97 |
positional=True, |
| 98 |
cone_angle=15, |
| 99 |
attenuation_values=(2, 0, 0), |
| 100 |
) |
| 101 |
light.show_actor() |
| 102 |
|
| 103 |
pl.add_light(light) |
| 104 |
pl.view_vector((1, -2, 2)) |
| 105 |
pl.enable_shadows() |
| 106 |
pl.show() |
| 107 |
|
| 108 |
|
| 109 |
# %% |
| 110 |
# Here, we use a lower shadow_attenuation value to demonstrate how the |
| 111 |
# light can travel through more planes. |
| 112 |
|
| 113 |
pl = pv.Plotter(lighting=None, window_size=(800, 800)) |
| 114 |
|
| 115 |
# add several planes |
| 116 |
for plane_y in [2, 5, 10]: |
| 117 |
screen = pv.Plane(center=(0, plane_y, 0), direction=(0, 1, 0), i_size=5, j_size=5) |
| 118 |
pl.add_mesh(screen, color='white') |
| 119 |
|
| 120 |
light = pv.Light( |
| 121 |
position=(0, 0, 0), |
| 122 |
focal_point=(0, 1, 0), |
| 123 |
color='cyan', |
| 124 |
intensity=15, |
| 125 |
cone_angle=15, |
| 126 |
shadow_attenuation=0.95, |
| 127 |
positional=True, |
| 128 |
attenuation_values=(2, 0, 0), |
| 129 |
) |
| 130 |
light.show_actor() |
| 131 |
|
| 132 |
pl.add_light(light) |
| 133 |
pl.view_vector((1, -2, 2)) |
| 134 |
pl.enable_shadows() |
| 135 |
pl.show() |
| 136 |
# %% |
| 137 |
# .. tags:: lights |