Debian Codegraph @

examples/04-lights/light_actors.py

raw
1 """
2 .. _light_actors_example:
3
4 Light Actors
5 ~~~~~~~~~~~~
6
7 Positional lights in PyVista have customizable beam shapes, see the
8 :ref:`beam_shape_example` example. Spotlights are special in
9 the sense that they are unidirectional lights with a finite position,
10 so they can be visualized using a cone.
11
12 This is exactly the purpose of a :vtk:`vtkLightActor`, the
13 functionality of which can be enabled for spotlights:
14 """
15
16 # sphinx_gallery_thumbnail_number = 1
17 import numpy as np
18 import pyvista as pv
19 from pyvista import examples
20
21 cow = examples.download_cow()
22 cow.rotate_x(90, inplace=True)
23 pl = pv.Plotter(lighting='none', window_size=(1000, 1000))
24 pl.add_mesh(cow, color='white')
25 floor = pv.Plane(center=(*cow.center[:2], cow.bounds[-2]), i_size=30, j_size=25)
26 pl.add_mesh(floor, color='green')
27
28 UFO = pv.Light(position=(0, 0, 10), focal_point=(0, 0, 0), color='white')
29 UFO.positional = True
30 UFO.cone_angle = 40
31 UFO.exponent = 10
32 UFO.intensity = 3
33 UFO.show_actor()
34 pl.add_light(UFO)
35
36 # enable shadows to better demonstrate lighting
37 pl.enable_shadows()
38
39 pl.camera_position = pv.CameraPosition(
40 position=(28, 30, 22), focal_point=(0.77, 0, -0.44), viewup=(0, 0, 1)
41 )
42 pl.show()
43
44
45 # %%
46 # Light actors can be very useful when designing complex scenes where
47 # spotlights are involved in lighting.
48
49 pl = pv.Plotter(lighting='none')
50 plane = pv.Plane(i_size=4, j_size=4)
51 pl.add_mesh(plane, color='white')
52
53 rot120 = np.array([[-0.5, -np.sqrt(3) / 2, 0], [np.sqrt(3) / 2, -0.5, 0], [0, 0, 1]])
54
55 position = (-1.5, -1.5, 3)
56 focus = (-0.5, -0.5, 0)
57 colors = ['red', 'lime', 'blue']
58 for color in colors:
59 position = rot120 @ position
60 focus = rot120 @ focus
61 light = pv.Light(position=position, focal_point=focus, color=color)
62 light.positional = True
63 light.cone_angle = 15
64 light.show_actor()
65 pl.add_light(light)
66
67 pl.show()
68
69
70 # %%
71 # One thing to watch out for is that the light actors are represented such that
72 # their cone has a fixed height. This implies that for very large cone angles
73 # we typically end up with enormous light actors, in which case setting a manual
74 # camera position before rendering is usually a good idea. Increasing the first
75 # example's cone angle and omitting the manual camera positioning exemplifies
76 # the problem:
77
78 pl = pv.Plotter(lighting='none')
79 pl.add_mesh(cow, color='white')
80 floor = pv.Plane(center=(*cow.center[:2], cow.bounds[-2]), i_size=30, j_size=25)
81 pl.add_mesh(floor, color='green')
82
83 UFO = pv.Light(position=(0, 0, 10), focal_point=(0, 0, 0), color='white')
84 UFO.positional = True
85 UFO.cone_angle = 89
86 UFO.exponent = 10
87 UFO.intensity = 3
88 UFO.show_actor()
89 pl.add_light(UFO)
90
91 pl.show()
92 # %%
93 # .. tags:: lights