{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Sample Function: Perlin Noise in 2D {#perlin_noise_2d_example}\n\nHere we use\n`pyvista.core.utilities.features.sample_function`{.interpreted-text\nrole=\"func\"} to sample Perlin noise over a region to generate random\nterrain.\n\nPerlin noise is atype of gradient noise often used by visual effects\nartists to increase the appearance of realism in computer graphics.\nSource: [Perlin Noise\nWikipedia](https://en.wikipedia.org/wiki/Perlin_noise)\n\nThe development of Perlin Noise has allowed computer graphics artists to\nbetter represent the complexity of natural phenomena in visual effects\nfor the motion picture industry.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pyvista as pv"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Generate Perlin Noise over a StructuredGrid\n\nFeel free to change the values of `freq` to change the shape of the\n\\\"mountains\\\". For example, lowering the frequency will make the terrain\nseem more like hills rather than mountains.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "freq = [0.689, 0.562, 0.683]\nnoise = pv.perlin_noise(1, freq, (0, 0, 0))\nsampled = pv.sample_function(noise, bounds=(-10, 10, -10, 10, -10, 10), dim=(500, 500, 1))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Warp by scalar\n\nHere we warp by scalar to give the terrain some height based on the\nvalue of the Perlin noise. This is necessary to the terrain its shape.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mesh = sampled.warp_by_scalar('scalars')\nmesh = mesh.extract_surface()\n\n# clean and smooth a little to reduce Perlin noise artifacts\nmesh = mesh.smooth(n_iter=100, inplace=False, relaxation_factor=1)\n\n# This makes the \"water\" level look flat.\nz = mesh.points[:, 2]\ndiff = z.max() - z.min()\n\n# water level at 70%  (change this to change the water level)\nwater_percent = 0.7\nwater_level = z.max() - water_percent * diff\nmesh.points[z < water_level, 2] = water_level"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Show the terrain as a contour plot\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# make the water blue\nrng = z.max() - z.min()\nclim = (z.max() - rng * 1.65, z.max())\n\npl = pv.Plotter()\npl.add_mesh(\n    mesh,\n    scalars=z,\n    cmap='gist_earth',\n    n_colors=10,\n    show_scalar_bar=False,\n    smooth_shading=True,\n    clim=clim,\n)\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Show the terrain with custom lighting and shadows\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl = pv.Plotter(lighting=None)\npl.add_light(pv.Light((3, 1, 0.5), show_actor=True, positional=True, cone_angle=90, intensity=1.2))\npl.add_mesh(mesh, cmap='gist_earth', show_scalar_bar=False, smooth_shading=True, clim=clim)\npl.enable_shadows = True\npl.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}