Debian Codegraph @

examples/02-plot/depth_of_field.py

raw
1 """
2 .. _depth_of_field_example:
3
4 Depth of Field Plotting
5 ~~~~~~~~~~~~~~~~~~~~~~~
6
7 This example shows how you can use :func:`enable_depth_of_field
8 <pyvista.Plotter.enable_depth_of_field>` to highlight part of your plot.
9
10 """
11
12 # sphinx_gallery_start_ignore
13 # depth field modification does not seem to work in interactive mode
14 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
15 # sphinx_gallery_end_ignore
16
17 import numpy as np
18 import pyvista as pv
19 from pyvista import examples
20
21 # %%
22 # Generate a bunch of bunnies
23 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 # Create many bunnies using the :func:`glyph <pyvista.DataSetFilters.glyph>`
25 # filter.
26
27 # download the stanford bunny and rotate it into a good position
28 mesh = examples.download_bunny()
29 mesh = mesh.rotate_x(90, inplace=False).rotate_z(90, inplace=False).scale(4)
30
31 # We use a uniform grid here simply to create equidistantly spaced points for
32 # our glyph filter
33 grid = pv.ImageData(dimensions=(4, 3, 3), spacing=(3, 1, 1))
34
35 bunnies = grid.glyph(geom=mesh, scale=False, orient=False)
36 bunnies
37
38
39 # %%
40 # Show the plot without enabling depth of field
41 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42
43 # convert points into rgba colors
44 colors = bunnies.points - bunnies.bounds[::2]
45 colors /= colors.max(axis=0)
46 colors *= 255
47 colors = colors.astype(np.uint8)
48
49 # obtained camera position with `cpos = pl.show(return_cpos)`
50 cpos = pv.CameraPosition(
51 position=(11.6159, -1.2803, 1.5338),
52 focal_point=(4.1354, 1.4796, 1.2711),
53 viewup=(-0.0352, -0.0004, 1.0),
54 )
55
56 # Since we're using physically based rendering (PBR), let's also download a
57 # skybox cubemap use it as an environment texture. For PBR to work well you
58 # should have a environment texture.
59 cubemap = examples.download_sky_box_cube_map()
60
61 pl = pv.Plotter()
62 pl.background_color = 'w'
63 pl.add_mesh(bunnies, scalars=colors, rgb=True, pbr=True, metallic=0.85)
64 pl.camera_position = cpos
65 pl.set_environment_texture(cubemap)
66 pl.show()
67
68
69 # %%
70 # Show the plot while enabling depth of field
71 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72
73 pl = pv.Plotter()
74 pl.background_color = 'w'
75 pl.add_mesh(bunnies, scalars=colors, rgb=True, pbr=True, metallic=0.85)
76 pl.camera_position = cpos
77 pl.enable_depth_of_field()
78 pl.enable_anti_aliasing('fxaa')
79 pl.set_environment_texture(cubemap)
80 pl.show()
81 # %%
82 # .. tags:: plot