| 1 |
""" |
| 2 |
.. _backface_prop_example: |
| 3 |
|
| 4 |
Setting Backface Properties |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
By default front and backface rendering uses the same properties. In certain |
| 8 |
situations it can be useful to set different properties for backfaces than |
| 9 |
for frontfaces. |
| 10 |
|
| 11 |
One straightforward example is when a closed (or close enough) surface has a |
| 12 |
different color on the inside. Note that the notion of "inside" and "outside" |
| 13 |
depend on the orientation of the surface normals: |
| 14 |
""" |
| 15 |
|
| 16 |
# sphinx_gallery_thumbnail_number = 1 |
| 17 |
|
| 18 |
# sphinx_gallery_start_ignore |
| 19 |
# backface properties do not work in interactive |
| 20 |
PYVISTA_GALLERY_FORCE_STATIC = True |
| 21 |
# sphinx_gallery_end_ignore |
| 22 |
|
| 23 |
import numpy as np |
| 24 |
import pyvista as pv |
| 25 |
from pyvista import examples |
| 26 |
|
| 27 |
mesh = pv.ParametricEllipsoid(min_v=np.pi / 2 - 0.2, max_v=np.pi / 2 + 0.2) |
| 28 |
|
| 29 |
# create a shifted copy with flipped normals |
| 30 |
mesh_inside_out = mesh.translate((0, 0, 1), inplace=False) |
| 31 |
mesh_inside_out.compute_normals(flip_normals=True, inplace=True) |
| 32 |
meshes = mesh + mesh_inside_out |
| 33 |
|
| 34 |
backface_params = dict(color='orangered') |
| 35 |
meshes.plot(color='aquamarine', backface_params=backface_params, smooth_shading=True) |
| 36 |
|
| 37 |
|
| 38 |
# %% |
| 39 |
# A more interesting use case is helping visualize the orientation of complex, |
| 40 |
# self-intersecting surfaces. For instance :func:`Catalan's minimal surface |
| 41 |
# <pyvista.ParametricCatalanMinimal>` has a complex shape, and coloring the |
| 42 |
# front and backfaces differently helps viewers comprehend the intricate |
| 43 |
# structure of the surface. This example also demonstrates use of the |
| 44 |
# :attr:`backface_prop <pyvista.Actor.backface_prop>` property of the |
| 45 |
# :class:`pyvista.Actor` class. |
| 46 |
|
| 47 |
catalan = pv.ParametricCatalanMinimal() |
| 48 |
pl = pv.Plotter() |
| 49 |
actor = pl.add_mesh(catalan, color='dodgerblue', smooth_shading=True) |
| 50 |
bprop = actor.backface_prop |
| 51 |
bprop.color = 'forestgreen' |
| 52 |
bprop.specular = 1.0 |
| 53 |
bprop.specular_power = 50.0 |
| 54 |
pl.show() |
| 55 |
|
| 56 |
|
| 57 |
# %% |
| 58 |
# In the case of non-orientable surfaces, adding specific backface properties can |
| 59 |
# make the non-orientable quality very obvious by the emergence of "seams" |
| 60 |
# where the face properties are discontinuous. |
| 61 |
|
| 62 |
henneberg = pv.ParametricHenneberg().scale(0.25, inplace=False) |
| 63 |
klein = ( |
| 64 |
pv.ParametricKlein().rotate_z(150, inplace=False).translate((6, 0, 0), inplace=False) |
| 65 |
) |
| 66 |
meshes = henneberg + klein |
| 67 |
|
| 68 |
backface_params = dict(color='mediumseagreen', specular=1.0, specular_power=50.0) |
| 69 |
meshes.plot(color='gold', backface_params=backface_params, smooth_shading=True) |
| 70 |
|
| 71 |
|
| 72 |
# %% |
| 73 |
# Of course we aren't constrained to only setting distinct colors for backfaces; |
| 74 |
# most :class:`pyvista.Property` attributes can be overridden. However, some of |
| 75 |
# these have no effect, while others merely don't make any sense. For instance, |
| 76 |
# most objects have the same opacity no matter which direction you look at them. |
| 77 |
# Here is a GIF animation circling around such an asymmetrically opaque Möbius |
| 78 |
# strip: |
| 79 |
|
| 80 |
mobius = pv.ParametricMobius().rotate_z(-90, inplace=False) |
| 81 |
backface_params = dict(opacity=0.5) |
| 82 |
pl = pv.Plotter() |
| 83 |
pl.add_mesh( |
| 84 |
mobius, color='deepskyblue', backface_params=backface_params, smooth_shading=True |
| 85 |
) |
| 86 |
pl.open_gif('mobius_semiopaque.gif') |
| 87 |
|
| 88 |
viewup = [0, 0, 1] |
| 89 |
orbit = pl.generate_orbital_path(n_points=24, shift=0.0, viewup=viewup) |
| 90 |
pl.orbit_on_path(orbit, write_frames=True, viewup=viewup, step=0.02) |
| 91 |
|
| 92 |
|
| 93 |
# %% |
| 94 |
# Apply Backface Properties to Textured Meshes |
| 95 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 96 |
# Backface textures can also be applied to meshes that have textures applied to |
| 97 |
# them. For this example we load the globe texture with |
| 98 |
# :func:`pyvista.examples.load_globe() <pyvista.examples.examples.load_globe>`, |
| 99 |
# clip it, and then apply a different color to the interior surface. |
| 100 |
# |
| 101 |
# The lighting has been disabled for this example to demonstrate how you can |
| 102 |
# make the interior of the surface appear occluded without any directional |
| 103 |
# lighting simply by providing a different color for backface. |
| 104 |
|
| 105 |
globe = examples.load_globe() |
| 106 |
texture = examples.load_globe_texture() |
| 107 |
clipped = globe.clip(normal='z', value=4.37e9) |
| 108 |
|
| 109 |
pl = pv.Plotter() |
| 110 |
pl.add_mesh( |
| 111 |
clipped, |
| 112 |
backface_params={'color': [0.2, 0.2, 0.2]}, |
| 113 |
lighting=False, |
| 114 |
smooth_shading=True, |
| 115 |
texture=texture, |
| 116 |
) |
| 117 |
pl.show() |
| 118 |
|
| 119 |
|
| 120 |
# %% |
| 121 |
# Backface Properties and Physically Based Rendering |
| 122 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 123 |
# Note that backfaces are automatically culled when physically based rendering |
| 124 |
# is enabled, regardless of the settings of backface parameters. |
| 125 |
|
| 126 |
sphere = pv.Sphere() |
| 127 |
clipped_sphere = sphere.clip(normal='z', value=0.4) |
| 128 |
|
| 129 |
pl = pv.Plotter() |
| 130 |
pl.set_environment_texture(examples.download_sky_box_cube_map()) |
| 131 |
pl.add_mesh( |
| 132 |
clipped_sphere, |
| 133 |
backface_params={'color': 'r'}, |
| 134 |
pbr=True, |
| 135 |
metallic=1.0, |
| 136 |
roughness=0.2, |
| 137 |
) |
| 138 |
pl.show() |
| 139 |
|
| 140 |
|
| 141 |
# %% |
| 142 |
# See also the :ref:`sphere_eversion_example` example which relies on |
| 143 |
# distinguishing the inside and the outside of a sphere. |
| 144 |
# |
| 145 |
# .. tags:: plot |