Debian Codegraph @

examples/02-plot/ssao.py

raw
1 """
2 .. _ssao_example:
3
4 Surface Space Ambient Occlusion
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 Demonstrate the usage of surface space ambient occlusion.
8
9 Surface space ambient occlusion (SSAO) can approximate shadows more
10 efficiently than ray-tracing and produce similar results. Use this when you wish
11 to plot the occlusion effect that nearby meshes have on each other by blocking
12 nearby light sources.
13
14 See `Kitware: Screen-Space Ambient Occlusion
15 <https://www.kitware.com/ssao/>`_ for more details
16
17 """
18
19 # sphinx_gallery_thumbnail_number = 3
20
21 # sphinx_gallery_start_ignore
22 # the different options of ssao are more clear in static images
23 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
24 # sphinx_gallery_end_ignore
25
26 # First, let's create several cubes nearby each other
27
28 import pyvista as pv
29 from pyvista import examples
30
31 grid = pv.ImageData(dimensions=(5, 5, 5)).explode(0.2)
32
33 # %%
34 # Plot with defaults
35 # ~~~~~~~~~~~~~~~~~~
36 # Let's plot this without SSAO. Note how the lighting is identical for each
37 # cube.
38
39 pl = pv.Plotter()
40 pl.add_mesh(grid)
41 pl.show()
42
43
44 # %%
45 # Plot with SSAO
46 # ~~~~~~~~~~~~~~
47 # Now plot this with SSAO using :func:`~pyvista.Plotter.enable_ssao`. Note how adjacent
48 # cubes affect the lighting of each other to make it look less artificial.
49 #
50 # With a low ``kernel_size``, the image will be rendered quickly at the expense
51 # of quality.
52
53 pl = pv.Plotter()
54 pl.add_mesh(grid)
55 pl.enable_ssao(kernel_size=32)
56 pl.show()
57
58
59 # %%
60 # Improve the SSAO rendering
61 # ~~~~~~~~~~~~~~~~~~~~~~~~~~
62 # Here we've increased the ``kernel_size`` to improve the quality of the SSAO
63 # and also enabled SSAA anti-aliasing to smooth out any of the artifacts
64 # created from SSAO.
65
66 pl = pv.Plotter()
67 pl.add_mesh(grid)
68 pl.enable_ssao(kernel_size=128)
69 pl.enable_anti_aliasing('ssaa')
70 pl.show()
71
72
73 # %%
74 # Plot a CAD model without SSAO
75 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76 # Here's another example without SSAO. This is a CAD model of a Raspberry PI
77 # case.
78
79 mesh = examples.download_cad_model_case()
80
81 pl = pv.Plotter()
82 pl.add_mesh(mesh, smooth_shading=True, split_sharp_edges=True)
83 pl.enable_anti_aliasing('ssaa')
84 pl.camera.zoom(1.7)
85 pl.show()
86
87
88 # %%
89 # Plot with SSAO
90 # ~~~~~~~~~~~~~~
91 # Here's the same CAD model with SSAO. Note how we had to increase both
92 # ``radius`` and ``bias`` due to the relative scene size.
93 #
94 # Note that the occlusion still seems quite small. In the next example we will
95 # increase the ``radius`` to increase the effect of the occlusion.
96
97 pl = pv.Plotter()
98 pl.add_mesh(mesh, smooth_shading=True, split_sharp_edges=True)
99 pl.enable_ssao(radius=2, bias=0.5)
100 pl.enable_anti_aliasing('ssaa')
101 pl.camera.zoom(1.7)
102 pl.show()
103
104
105 # %%
106 # Increase the Radius
107 # ~~~~~~~~~~~~~~~~~~~
108 # Here we've increased the ``radius`` to the point where the case occlusion now
109 # seems realistic without it becoming overwhelming.
110
111 pl = pv.Plotter()
112 pl.add_mesh(mesh, smooth_shading=True, split_sharp_edges=True)
113 pl.enable_ssao(radius=15, bias=0.5)
114 pl.enable_anti_aliasing('ssaa')
115 pl.camera.zoom(1.7)
116 pl.show()
117 # %%
118 # .. tags:: plot