Debian Codegraph @

examples/04-lights/beam_shape.py

raw
1 """
2 .. _beam_shape_example:
3
4 Beam Shape
5 ~~~~~~~~~~
6
7 The default directional lights are infinitely distant point sources, for which
8 the only geometric customization option is the choice of beam direction defined
9 by the light's position and focal point. Positional lights, however, have more
10 options for beam customization.
11
12 Consider two hemispheres:
13 """
14
15 # sphinx_gallery_thumbnail_number = 5
16 import pyvista as pv
17
18 pl = pv.Plotter()
19
20 hemi = pv.Sphere().clip()
21 hemi.translate((-1, 0, 0), inplace=True)
22 pl.add_mesh(hemi, color='cyan', smooth_shading=True)
23
24 hemi = hemi.rotate_z(180, inplace=False)
25 pl.add_mesh(hemi, color='cyan', smooth_shading=True)
26
27 pl.show()
28
29
30 # %%
31 # We can see that the default lighting does a very good job of articulating the
32 # shape of the hemispheres.
33 #
34 # Let's shine a directional light on them, positioned between the hemispheres and
35 # oriented along their centers:
36
37 pl = pv.Plotter(lighting='none')
38
39 hemi = pv.Sphere().clip()
40 hemi.translate((-1, 0, 0), inplace=True)
41 pl.add_mesh(hemi, color='cyan', smooth_shading=True)
42
43 hemi = hemi.rotate_z(180, inplace=False)
44 pl.add_mesh(hemi, color='cyan', smooth_shading=True)
45
46 light = pv.Light(position=(0, 0, 0), focal_point=(-1, 0, 0))
47 pl.add_light(light)
48
49 pl.show()
50
51
52 # %%
53 # Both hemispheres have their surface lit on the side that faces the light.
54 # This is consistent with the point source positioned at infinity, directed from
55 # the light's nominal position toward the focal point.
56 #
57 # Now let's change the light to a positional light (but not a spotlight):
58
59 pl = pv.Plotter(lighting='none')
60
61 hemi = pv.Sphere().clip()
62 hemi.translate((-1, 0, 0), inplace=True)
63 pl.add_mesh(hemi, color='cyan', smooth_shading=True)
64
65 hemi = hemi.rotate_z(180, inplace=False)
66 pl.add_mesh(hemi, color='cyan', smooth_shading=True)
67
68 light = pv.Light(position=(0, 0, 0), focal_point=(-1, 0, 0))
69 light.positional = True
70 light.cone_angle = 90
71 pl.add_light(light)
72
73 pl.show()
74
75
76 # %%
77 # Now the inner surface of both hemispheres is lit. A positional light with a
78 # cone angle of 90 degrees (or more) acts as a point source located at the
79 # light's nominal position. It could still display attenuation, see the
80 # :ref:`attenuation_example` example.
81 #
82 # Switching to a spotlight (i.e. a positional light with a cone angle less
83 # than 90 degrees) will enable beam shaping using the :py:attr:`pyvista.Light.exponent`
84 # property. Let's put our hemispheres side by side for this, and put a light in
85 # the center of each: one spotlight, one merely positional.
86
87 pl = pv.Plotter(lighting='none')
88
89 hemi = pv.Sphere().clip()
90 pl.add_mesh(hemi, color='cyan', smooth_shading=True)
91
92 offset = 1.5
93 hemi = hemi.translate((0, offset, 0), inplace=False)
94 pl.add_mesh(hemi, color='cyan', smooth_shading=True)
95
96 # non-spot positional light in the center of the first hemisphere
97 light = pv.Light(position=(0, 0, 0), focal_point=(-1, 0, 0))
98 light.positional = True
99 light.cone_angle = 90
100 # add attenuation to reduce cross-talk between the lights
101 light.attenuation_values = (0, 0, 2)
102 pl.add_light(light)
103
104 # spotlight in the center of the second hemisphere
105 light = pv.Light(position=(0, offset, 0), focal_point=(-1, offset, 0))
106 light.positional = True
107 light.cone_angle = 89.9
108 # add attenuation to reduce cross-talk between the lights
109 light.attenuation_values = (0, 0, 2)
110 pl.add_light(light)
111
112 pl.show()
113
114
115 # %%
116 # Even though the two lights only differ by a fraction of a degree in cone angle,
117 # the beam shaping effect enabled for spotlights causes a marked difference in
118 # the result.
119 #
120 # Once we have a spotlight we can change its :py:attr:`pyvista.Light.exponent`
121 # to make the beam shape sharper or broader. Three spotlights with varying
122 # sharpness:
123
124 pl = pv.Plotter(lighting='none')
125 hemi_template = pv.Sphere().clip()
126
127 centers = [(0, 0, 0), (0, 1.5, 0), (0, 1.5 * 0.5, 1.5 * 3**0.5 / 2)]
128 exponents = [1, 0.3, 5]
129
130 for center, exponent in zip(centers, exponents, strict=True):
131 hemi = hemi_template.copy()
132 hemi.translate(center, inplace=True)
133 pl.add_mesh(hemi, color='cyan', smooth_shading=True)
134
135 # spotlight in the center of the hemisphere, shining into it
136 focal_point = center[0] - 1, center[1], center[2]
137 light = pv.Light(position=center, focal_point=focal_point)
138 light.positional = True
139 light.cone_angle = 89.9
140 light.exponent = exponent
141 # add attenuation to reduce cross-talk between the lights
142 light.attenuation_values = (0, 0, 2)
143 pl.add_light(light)
144
145 pl.show()
146
147
148 # %%
149 # The spotlight with exponent 0.3 is practically uniform, and the one with
150 # exponent 5 is visibly focused along the axis of the light.
151 #
152 # .. tags:: lights