| 1 |
""" |
| 2 |
.. _depth_peeling_example: |
| 3 |
|
| 4 |
Depth Peeling |
| 5 |
~~~~~~~~~~~~~ |
| 6 |
Depth peeling is a technique to correctly render translucent geometry. This is |
| 7 |
not enabled by default in :attr:`pyvista.global_theme |
| 8 |
<pyvista.plotting.themes.Theme>` as some operating systems and versions of VTK |
| 9 |
have issues with this routine. |
| 10 |
|
| 11 |
For this example, we will showcase the difference that depth peeling |
| 12 |
provides. See :func:`~pyvista.Plotter.enable_depth_peeling`. |
| 13 |
|
| 14 |
""" |
| 15 |
|
| 16 |
# sphinx_gallery_thumbnail_number = 2 |
| 17 |
import pyvista as pv |
| 18 |
from pyvista import examples |
| 19 |
|
| 20 |
# %% |
| 21 |
centers = [(0, 0, 0), (1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0)] |
| 22 |
radii = [1, 0.5, 0.5, 0.5, 0.5] |
| 23 |
|
| 24 |
spheres = pv.MultiBlock() |
| 25 |
for i, c in enumerate(centers): |
| 26 |
spheres.append(pv.Sphere(center=c, radius=radii[i])) |
| 27 |
|
| 28 |
# %% |
| 29 |
dargs = dict(opacity=0.5, color='red', smooth_shading=True) |
| 30 |
|
| 31 |
pl = pv.Plotter(shape=(1, 2)) |
| 32 |
|
| 33 |
pl.add_mesh(spheres, **dargs) |
| 34 |
pl.enable_depth_peeling(10) |
| 35 |
pl.add_text('Depth Peeling') |
| 36 |
|
| 37 |
pl.subplot(0, 1) |
| 38 |
pl.add_text('Standard') |
| 39 |
pl.add_mesh(spheres.copy(), **dargs) |
| 40 |
|
| 41 |
pl.link_views() |
| 42 |
pl.camera_position = pv.CameraPosition( |
| 43 |
position=(11.7, 4.7, -4.33), focal_point=(0.0, 0.0, 0.0), viewup=(0.3, 0.07, 0.9) |
| 44 |
) |
| 45 |
pl.show() |
| 46 |
|
| 47 |
# %% |
| 48 |
# The following room surfaces example mesh, provided courtesy of |
| 49 |
# `Sam Potter <https://github.com/sampotter>`_ has coincident topology and |
| 50 |
# depth rendering helps correctly render those geometries when a global |
| 51 |
# opacity value is used. |
| 52 |
|
| 53 |
room = examples.download_room_surface_mesh() |
| 54 |
|
| 55 |
pl = pv.Plotter(shape=(1, 2)) |
| 56 |
|
| 57 |
pl.enable_depth_peeling(number_of_peels=4, occlusion_ratio=0) |
| 58 |
pl.add_mesh(room, opacity=0.5, color='lightblue') |
| 59 |
pl.add_text('Depth Peeling') |
| 60 |
|
| 61 |
pl.subplot(0, 1) |
| 62 |
pl.add_text('Standard') |
| 63 |
pl.add_mesh(room.copy(), opacity=0.5, color='lightblue') |
| 64 |
|
| 65 |
pl.link_views() |
| 66 |
pl.camera_position = pv.CameraPosition( |
| 67 |
position=(43.6, 49.5, 19.8), focal_point=(0.0, 2.25, 0.0), viewup=(-0.57, 0.70, -0.42) |
| 68 |
) |
| 69 |
|
| 70 |
pl.show() |
| 71 |
|
| 72 |
|
| 73 |
# %% |
| 74 |
# And here is another example when rendering many translucent contour |
| 75 |
# surfaces. |
| 76 |
|
| 77 |
mesh = examples.download_brain().resample(0.5, anti_aliasing=True) |
| 78 |
contours = mesh.contour(5) |
| 79 |
cmap = 'viridis_r' |
| 80 |
|
| 81 |
pl = pv.Plotter(shape=(1, 2)) |
| 82 |
|
| 83 |
pl.add_mesh(contours, opacity=0.5, cmap=cmap) |
| 84 |
pl.enable_depth_peeling(10) |
| 85 |
pl.add_text('Depth Peeling') |
| 86 |
|
| 87 |
pl.subplot(0, 1) |
| 88 |
pl.add_text('Standard') |
| 89 |
pl.add_mesh(contours.copy(), opacity=0.5, cmap=cmap) |
| 90 |
|
| 91 |
pl.link_views() |
| 92 |
pl.camera_position = pv.CameraPosition( |
| 93 |
position=(418.3, 659.0, 53.8), |
| 94 |
focal_point=(90.2, 111.5, 90.0), |
| 95 |
viewup=(0.03, 0.05, 1.0), |
| 96 |
) |
| 97 |
pl.show() |
| 98 |
# %% |
| 99 |
# .. tags:: plot |