| 1 |
""" |
| 2 |
.. _perlin_noise_3d_example: |
| 3 |
|
| 4 |
Sample Function: Perlin Noise in 3D |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
Here we use :func:`pyvista.core.utilities.features.sample_function` to sample |
| 7 |
Perlin noise over a region to generate random terrain. |
| 8 |
|
| 9 |
Video games like Minecraft use Perlin noise to create terrain. Here, |
| 10 |
we create a voxelized mesh similar to a Minecraft "cave". |
| 11 |
|
| 12 |
""" |
| 13 |
|
| 14 |
import pyvista as pv |
| 15 |
|
| 16 |
# %% |
| 17 |
# Generate Perlin Noise over a 3D StructuredGrid |
| 18 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 |
# Feel free to change the values of ``freq`` to change the shape of |
| 20 |
# the "caves". For example, lowering the frequency will make the |
| 21 |
# caves larger and more expansive, while a higher frequency in any |
| 22 |
# direction will make the caves appear more "vein-like" and less open. |
| 23 |
# |
| 24 |
# Change the threshold to reduce or increase the percent of the |
| 25 |
# terrain that is open or closed |
| 26 |
|
| 27 |
freq = (1, 1, 1) |
| 28 |
noise = pv.perlin_noise(1, freq, (0, 0, 0)) |
| 29 |
grid = pv.sample_function(noise, bounds=[0, 3.0, -0, 1.0, 0, 1.0], dim=(120, 40, 40)) |
| 30 |
out = grid.threshold(0.02) |
| 31 |
out |
| 32 |
|
| 33 |
# %% |
| 34 |
# color limits without blue |
| 35 |
mn, mx = [out['scalars'].min(), out['scalars'].max()] |
| 36 |
clim = (mn, mx * 1.8) |
| 37 |
|
| 38 |
out.plot( |
| 39 |
cmap='gist_earth_r', |
| 40 |
background='white', |
| 41 |
show_scalar_bar=False, |
| 42 |
lighting=True, |
| 43 |
clim=clim, |
| 44 |
show_edges=False, |
| 45 |
) |
| 46 |
# %% |
| 47 |
# .. tags:: filter |