Debian Codegraph @

examples/04-lights/plotter_lighting.py

raw
1 """
2 .. _plotter_lighting_example:
3
4 Plotter Lighting Systems
5 ~~~~~~~~~~~~~~~~~~~~~~~~
6
7 The :class:`pyvista.Plotter` class comes with three options for the default
8 lighting system:
9
10 * a light kit consisting of a headlight and four camera lights,
11 * an illumination system containing three lights arranged around the camera,
12 * no lighting.
13
14 With meshes that don't have depth information encoded in their color the
15 importance of an appropriate lighting setup becomes paramount for accurate
16 visualization.
17
18 Light kit
19 =========
20
21 The default ``lighting='light kit'`` option recreates a lighting setup that
22 corresponds to a :vtk:`vtkLightKit`. We can check what type of lights this
23 lighting comprises:
24 """
25
26 # sphinx_gallery_thumbnail_number = 3
27 import pyvista as pv
28 from pyvista import examples
29
30 # default: light kit
31 pl = pv.Plotter()
32 light_types = [light.light_type for light in pl.renderer.lights]
33
34 # Remove from plotters so output is not produced in docs
35 pv.plotting.plotter._ALL_PLOTTERS.clear()
36
37 light_types
38
39
40 # %%
41 # Add a white terrain to the scene:
42
43 mesh = examples.download_st_helens().warp_by_scalar()
44 pl = pv.Plotter()
45 pl.add_mesh(mesh, color='white')
46 pl.show()
47
48
49 # %%
50 # Three-lights illumination
51 # =========================
52 #
53 # Switching to three-lights illumination gives a different character to the
54 # figure, in this case showing less contrast when viewing the mountain from
55 # the top, but having more contrast with views closer to the side. This becomes
56 # especially clear when exploring the figures interactively.
57
58 pl = pv.Plotter(lighting='three lights')
59 pl.add_mesh(mesh, color='white')
60 pl.show()
61
62 # %%
63 # Again we can check what kind of lights this setting uses:
64
65 pl = pv.Plotter(lighting='three lights')
66 light_types = [light.light_type for light in pl.renderer.lights]
67
68 # Remove from plotters so output is not produced in docs
69 pv.plotting.plotter._ALL_PLOTTERS.clear()
70
71 light_types
72
73
74 # %%
75 # Custom lighting
76 # ===============
77 #
78 # We can introduce our own lighting from scratch by disabling any
79 # lighting on plotter initialization. Adding a single scene light to a
80 # scene will often result in ominous visuals due to objects having
81 # larger regions in shadow.
82
83 pl = pv.Plotter(lighting='none', window_size=(1000, 1000))
84 pl.add_mesh(mesh, color='white', smooth_shading=True)
85 light = pv.Light()
86 light.set_direction_angle(30, -20)
87 pl.add_light(light)
88 pl.show()
89
90
91 # %%
92 # Custom lighting with shadows
93 # ============================
94 #
95 # Here, we ``enable_shadows`` to enhance the effect that the lighting
96 # angle has. It has a subtle, but realistic effect. Notice the sharp
97 # shadows due to the mountaintop.
98
99 pl = pv.Plotter(lighting='none', window_size=(1000, 1000))
100 pl.add_mesh(mesh, color='white', smooth_shading=True)
101 light = pv.Light()
102 light.set_direction_angle(20, -20)
103 pl.add_light(light)
104 pl.enable_shadows()
105 pl.show()
106 # %%
107 # .. tags:: lights