| 1 |
""" |
| 2 |
.. _shading_example: |
| 3 |
|
| 4 |
Types of Shading |
| 5 |
~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Comparison of default, flat shading vs. smooth shading. |
| 8 |
""" |
| 9 |
|
| 10 |
# sphinx_gallery_thumbnail_number = 4 |
| 11 |
import pyvista as pv |
| 12 |
from pyvista import examples |
| 13 |
|
| 14 |
# %% |
| 15 |
# PyVista supports two types of shading: flat and smooth shading that uses |
| 16 |
# VTK's Phong shading algorithm. |
| 17 |
# |
| 18 |
# This is a plot with the default flat shading. |
| 19 |
mesh = examples.load_nut() |
| 20 |
mesh.plot() |
| 21 |
|
| 22 |
|
| 23 |
# %% |
| 24 |
# Here's the same sphere with smooth shading. |
| 25 |
mesh.plot(smooth_shading=True) |
| 26 |
|
| 27 |
|
| 28 |
# %% |
| 29 |
# Note how smooth shading makes edges that should be sharp look odd, |
| 30 |
# it's because the points of these normals are averaged between two |
| 31 |
# faces that have a sharp angle between them. You can avoid this by |
| 32 |
# enabling ``split_sharp_edges``. |
| 33 |
# |
| 34 |
# .. note:: |
| 35 |
# You can configure the splitting angle with the optional |
| 36 |
# ``feature_angle`` keyword argument. |
| 37 |
mesh.plot(smooth_shading=True, split_sharp_edges=True) |
| 38 |
|
| 39 |
|
| 40 |
# %% |
| 41 |
# We can even plot the edges that will be split using |
| 42 |
# :func:`extract_feature_edges <pyvista.DataSetFilters.extract_feature_edges>`. |
| 43 |
|
| 44 |
# extract the feature edges exceeding 30 degrees |
| 45 |
edges = mesh.extract_feature_edges( |
| 46 |
boundary_edges=False, |
| 47 |
non_manifold_edges=False, |
| 48 |
feature_angle=30, |
| 49 |
manifold_edges=False, |
| 50 |
) |
| 51 |
|
| 52 |
# plot both the edges and the smoothed mesh |
| 53 |
pl = pv.Plotter() |
| 54 |
pl.enable_anti_aliasing() |
| 55 |
pl.add_mesh(mesh, smooth_shading=True, split_sharp_edges=True) |
| 56 |
pl.add_mesh(edges, color='k', line_width=5) |
| 57 |
pl.show() |
| 58 |
|
| 59 |
|
| 60 |
# %% |
| 61 |
# The ``split_sharp_edges`` keyword argument is compatible with |
| 62 |
# physically based rendering as well. |
| 63 |
|
| 64 |
# plot both the edges and the smoothed mesh |
| 65 |
|
| 66 |
# sphinx_gallery_start_ignore |
| 67 |
# physically based rendering does not work in interactive plots |
| 68 |
PYVISTA_GALLERY_FORCE_STATIC = True |
| 69 |
# sphinx_gallery_end_ignore |
| 70 |
|
| 71 |
pl = pv.Plotter() |
| 72 |
pl.enable_anti_aliasing() |
| 73 |
pl.add_mesh( |
| 74 |
mesh, color='w', split_sharp_edges=True, pbr=True, metallic=1.0, roughness=0.5 |
| 75 |
) |
| 76 |
pl.show() |
| 77 |
# %% |
| 78 |
# .. tags:: plot |