Debian Codegraph @

examples/04-lights/attenuation.py

raw
1 """
2 .. _attenuation_example:
3
4 Attenuation
5 ~~~~~~~~~~~
6
7 This example shows how use :attr:`~pyvista.Light.attenuation_values`.
8
9 Attenuation is the phenomenon of light's intensity being gradually dampened as
10 it propagates through a medium. In PyVista positional lights can show attenuation.
11 The quadratic attenuation model uses three parameters to describe attenuation:
12 a constant, a linear and a quadratic parameter. These parameters
13 describe the decrease of the beam intensity as a function of the distance, `I(r)`.
14 In a broad sense the constant, linear and quadratic components correspond to
15 `I(r) = 1`, `I(r) = 1/r` and `I(r) = 1/r^2` decay of the intensity with distance
16 from the point source. In all cases a larger attenuation value (of a given kind)
17 means stronger dampening (weaker light at a given distance).
18
19 So the constant attenuation parameter corresponds roughly to a constant intensity
20 component. The linear and the quadratic attenuation parameters correspond to intensity
21 components that decay with distance from the source. For the same parameter value the
22 quadratic attenuation produces a beam that is shorter in range than that produced
23 by linear attenuation.
24
25 Three spotlights with three different attenuation profiles each:
26 """
27
28 # sphinx_gallery_thumbnail_number = 3
29 import pyvista as pv
30
31 pl = pv.Plotter(lighting='none')
32 billboard = pv.Plane(direction=(1, 0, 0), i_size=6, j_size=6)
33 pl.add_mesh(billboard, color='white')
34
35 all_attenuation_values = [(1, 0, 0), (0, 2, 0), (0, 0, 2)]
36 offsets = [-2, 0, 2]
37 for attenuation_values, offset in zip(all_attenuation_values, offsets, strict=True):
38 light = pv.Light(
39 position=(0.1, offset, 2), focal_point=(0.1, offset, 1), color='cyan'
40 )
41 light.positional = True
42 light.cone_angle = 20
43 light.intensity = 15
44 light.attenuation_values = attenuation_values
45 pl.add_light(light)
46
47 pl.view_yz()
48 pl.show()
49
50
51 # %%
52 # It's not too obvious but it's visible that the rightmost light with quadratic
53 # attenuation has a shorter range than the middle one with linear attenuation.
54 # Although it seems that even the leftmost light with constant attenuation loses
55 # its brightness gradually, this partly has to do with the fact that we sliced
56 # the light beams very close to their respective axes, meaning that light hits
57 # the surface in a very small angle. Altering the scene such that the lights
58 # are further away from the plane changes this:
59
60 pl = pv.Plotter(lighting='none')
61 billboard = pv.Plane(direction=(1, 0, 0), i_size=6, j_size=6)
62 pl.add_mesh(billboard, color='white')
63
64 all_attenuation_values = [(1, 0, 0), (0, 2, 0), (0, 0, 2)]
65 offsets = [-2, 0, 2]
66 for attenuation_values, offset in zip(all_attenuation_values, offsets, strict=True):
67 light = pv.Light(
68 position=(0.5, offset, 3), focal_point=(0.5, offset, 1), color='cyan'
69 )
70 light.positional = True
71 light.cone_angle = 20
72 light.intensity = 15
73 light.attenuation_values = attenuation_values
74 pl.add_light(light)
75
76 pl.view_yz()
77 pl.show()
78
79 # %%
80 # Now the relationship of the three kinds of attenuation seems clearer.
81 #
82 # For a more practical comparison, let's look at planes that are perpendicular
83 # to the axis of each light (making use of the fact that shadowing between
84 # objects is not handled by default):
85
86 pl = pv.Plotter(lighting='none')
87
88 # loop over three lights with three kinds of attenuation
89 all_attenuation_values = [(2, 0, 0), (0, 2, 0), (0, 0, 2)]
90 light_offsets = [-6, 0, 6]
91 for attenuation_values, light_x in zip(
92 all_attenuation_values, light_offsets, strict=True
93 ):
94 # loop over three perpendicular planes for each light
95 for plane_y in [2, 5, 10]:
96 screen = pv.Plane(
97 center=(light_x, plane_y, 0), direction=(0, 1, 0), i_size=5, j_size=5
98 )
99 pl.add_mesh(screen, color='white')
100
101 light = pv.Light(position=(light_x, 0, 0), focal_point=(light_x, 1, 0), color='cyan')
102 light.positional = True
103 light.cone_angle = 15
104 light.intensity = 5
105 light.attenuation_values = attenuation_values
106 light.show_actor()
107 pl.add_light(light)
108
109 pl.view_vector((1, -2, 2))
110 pl.show()
111 # %%
112 # .. tags:: lights