| 1 |
""" |
| 2 |
.. _pbr_example: |
| 3 |
|
| 4 |
Physically Based Rendering |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
VTK 9 introduced Physically Based Rendering (PBR) and we have exposed |
| 8 |
that functionality in PyVista. Read the `blog about PBR |
| 9 |
<https://blog.kitware.com/vtk-pbr/>`_ for more details. |
| 10 |
|
| 11 |
PBR is only supported for :class:`pyvista.PolyData` and can be |
| 12 |
triggered via the ``pbr`` keyword argument of ``add_mesh``. Also use |
| 13 |
the ``metallic`` and ``roughness`` arguments for further control. |
| 14 |
|
| 15 |
Let's show off this functionality by rendering a high quality mesh of |
| 16 |
a statue as though it were metallic. |
| 17 |
|
| 18 |
""" |
| 19 |
|
| 20 |
# sphinx_gallery_start_ignore |
| 21 |
# physically based rendering does not seem to work in vtk-js |
| 22 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 23 |
# sphinx_gallery_end_ignore |
| 24 |
|
| 25 |
from itertools import product |
| 26 |
|
| 27 |
import pyvista as pv |
| 28 |
from pyvista import examples |
| 29 |
|
| 30 |
# Load the bust mesh (George Washington, CC0, Smithsonian). |
| 31 |
mesh = examples.download_washington_bust().rotate_x(-90) |
| 32 |
|
| 33 |
# Download skybox |
| 34 |
cubemap = examples.download_sky_box_cube_map() |
| 35 |
|
| 36 |
|
| 37 |
# %% |
| 38 |
# Let's render the mesh with a base color of "linen" to give it a metal looking |
| 39 |
# finish. |
| 40 |
pl = pv.Plotter() |
| 41 |
pl.add_actor(cubemap.to_skybox()) |
| 42 |
pl.set_environment_texture(cubemap) # For reflecting the environment off the mesh |
| 43 |
pl.add_mesh(mesh, color='linen', pbr=True, metallic=0.8, roughness=0.1, diffuse=1) |
| 44 |
pl.view_xy(negative=True) |
| 45 |
pl.show() |
| 46 |
|
| 47 |
|
| 48 |
# %% |
| 49 |
# Show the variation of the metallic and roughness parameters. |
| 50 |
# |
| 51 |
# Plot with metallic increasing from left to right and roughness |
| 52 |
# increasing from bottom to top. |
| 53 |
|
| 54 |
colors = ['red', 'teal', 'black', 'orange', 'silver'] |
| 55 |
|
| 56 |
pl = pv.Plotter() |
| 57 |
pl.set_environment_texture(cubemap) |
| 58 |
|
| 59 |
for i, j in product(range(5), range(6)): |
| 60 |
sphere = pv.Sphere(radius=0.5, center=(0.0, 4 - i, j)) |
| 61 |
pl.add_mesh(sphere, color=colors[i], pbr=True, metallic=i / 4, roughness=j / 5) |
| 62 |
|
| 63 |
pl.view_vector((-1, 0, 0), (0, 1, 0)) |
| 64 |
pl.show() |
| 65 |
|
| 66 |
|
| 67 |
# %% |
| 68 |
# Combine custom lighting and physically based rendering. |
| 69 |
|
| 70 |
# download louis model |
| 71 |
mesh = examples.download_louis_louvre() |
| 72 |
mesh.rotate_z(140, inplace=True) |
| 73 |
|
| 74 |
|
| 75 |
pl = pv.Plotter(lighting=None) |
| 76 |
pl.set_background('black') |
| 77 |
pl.add_mesh(mesh, color='linen', pbr=True, metallic=0.5, roughness=0.5, diffuse=1) |
| 78 |
|
| 79 |
|
| 80 |
# set up lighting |
| 81 |
light = pv.Light(position=(-2, 2, 0), focal_point=(0, 0, 0), color='white') |
| 82 |
pl.add_light(light) |
| 83 |
|
| 84 |
light = pv.Light(position=(2, 0, 0), focal_point=(0, 0, 0), color=(0.7, 0.0862, 0.0549)) |
| 85 |
pl.add_light(light) |
| 86 |
|
| 87 |
light = pv.Light(position=(0, 0, 10), focal_point=(0, 0, 0), color='white') |
| 88 |
pl.add_light(light) |
| 89 |
|
| 90 |
|
| 91 |
# plot with a good camera position |
| 92 |
pl.camera_position = pv.CameraPosition( |
| 93 |
position=(9.51, 13.92, 15.81), |
| 94 |
focal_point=(-2.836, -0.93, 10.2), |
| 95 |
viewup=(-0.22, -0.18, 0.959), |
| 96 |
) |
| 97 |
cpos = pl.show() |
| 98 |
# %% |
| 99 |
# .. tags:: plot |