Sample Function: Perlin Noise in 2D#
Here we use
pyvista.core.imaging.sample_function() to sample
Perlin noise over a region to generate random terrain.
Perlin noise is atype of gradient noise often used by visual effects artists to increase the appearance of realism in computer graphics. Source: https://en.wikipedia.org/wiki/Perlin_noise
The development of Perlin Noise has allowed computer graphics artists to better represent the complexity of natural phenomena in visual effects for the motion picture industry.
import pyvista as pv
Generate Perlin Noise over a StructuredGrid#
Feel free to change the values of
freq to change the shape of
the “mountains”. For example, lowering the frequency will make the
terrain seem more like hills rather than mountains.
Warp by scalar#
Here we warp by scalar to give the terrain some height based on the value of the Perlin noise. This is necessary to the terrain its shape.
mesh = sampled.warp_by_scalar('scalars') mesh = mesh.extract_surface() # clean and smooth a little to reduce Perlin noise artifacts mesh = mesh.smooth(n_iter=100, inplace=False, relaxation_factor=1) # This makes the "water" level look flat. z = mesh.points[:, 2] diff = z.max() - z.min() # water level at 70% (change this to change the water level) water_percent = 0.7 water_level = z.max() - water_percent * diff mesh.points[z < water_level, 2] = water_level
Show the terrain as a contour plot
Show the terrain with custom lighting and shadows
Total running time of the script: ( 0 minutes 0.962 seconds)