Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# samlecuyer/post.html

Last active Sep 20, 2016
perlin noise post 1
 _Note: the diagrams here require a browser that supports canvases_ _If you're on a mobile browser, they may not show up._ I've been reading about Perlin noise and I think it's an amazing method of generating gradients of colors and terrains. I originally came across it because my friend Bill sent me a link to a [procedural map generator](https://twitter.com/williamberryiii/status/765420845971808256). Without driving you away from my own post (except it's way cooler), I'll explain the general map technique briefly. An area is divided into a number of polygons using a [Voronoi diagram](https://en.wikipedia.org/wiki/Voronoi_diagram) and a heightmap is generated using Perlin noise to figure out the elevations of various areas on the map. Although I'm very familiar with Voronoi diagrams and how they're created, I had no idea how heightmaps were created. ### What is it? Perlin noise is an algorithm developed by Ken Perlin in the 1980s that generates what's called _gradient noise_. Basically, it interpolates points in a pre-set lattice. It can work for any number of dimensions, though we'll use 3 today. It does this by calculating an n-dimensional grid of points that have somewhat random vectors off of each point, then calculates how each of those vectors "average out" to the input point. It's a little more complicated than that but it's a simple enough explanation for now. The algorithm itself, though, is simple and can be implemented in a couple dozen lines of code. A key takeaway from this is that the Perlin noise function is not random. The vectors in the grid can be random, but once they're generated the function is completely referentially transparent -- that is, the same inputs will always produce the same outputs. ### What does it look like? We'll take a look at the Perlin noise function in two dimensions, since it's far simpler than three to discuss. The noise function has a signature of `perlin.noise(x, y, z)`, which takes 3 floats of any size and returns a float between -1 and 1. The input values are the coordinates of a point in three dimensional space: `x` is horizontal, `y` is vertical, `z` is depth. If we graph the function along the x plane at a fixed y and z, then we'll see how it goes up and down. It looks like the following: _the perlin noise along the x axis_ That might not seem very useful, so let's talk about it. Despite seeming a bit random, the output is a wave. It is subject to the same frequency and amplitude transforms that other waves can be subjected to. The output above has a very high frequency and a very low amplitude. We can reduce the frequency by multiplying the inputs by a new frequency. Because the frequencies will be quite small, it can sometimes be simpler to divide the inputs by a new period (the inverse of frequency). We can also increase the output by simply multiplying the result by an amplitude. We can make a much more reasonable looking graph by using perlin.noise(x/12, y/12, z/12) * 25: _lower frequency, higher amplitude_ If we're thinking of this in terms of terrain, that might look like a jagged ridge. If we decrease the frequency by even more, we can create a landscape with smoother rolling hills: perlin.noise(x/50, y/50, z/50) * 50: _even lower frequency, even higher amplitude_ It's important to keep in mind that the graphs we're looking at are at a particular depth in 3-dimensional spaces. We can animate the wave as though it were coming toward us (out of the page) and continue to see how the same slice transforms along the z-axis. _the perlin noise on the x and z axes_ ### Changing dimensions I write this section with the following disclaimer: I kind of hate this. By _this_, I mean the following diagram. Displaying the graph in two dimensions is hard. It's hard to show depth. Until now, we've been graphing the output of the function as a vertical value along a single axis in the noise. We're going to start showing it in two dimensions. The typical way to do this is to move along both the x and y axes and use lightness and darkness to indicate the variations in the output. It's almost like we're trying to photograph a slice of an infinite smoke cloud. _such a tired, overused graph_ Again, this is a slice of the box at a specific z value. Every pixel is black with its opacity adjusted to match the output of the noise. Remember, the output is somewhere between -1 and 1. We can just add 1 to get a range of 0 to 1 then divide by 2, and we have a percentage of opacity for each pixel. What's much more fun is to move through space with colors. In addition to the RGBA color scheme you're familiar with, we can also use the [HSV](https://en.wikipedia.org/wiki/HSL_and_HSV) (hue, saturation, value) scheme to create a light and dark scheme of shades within a particular color using the output value as the lightness value. By decreasing the frequency significantly and using an HSV color, we can create a pretty cool plasma effect: There's a lot more that can be said about the Perlin nois function, but it'll have to wait for other posts. We can talk about octaves and heightmaps then. The code for this post is available as a [github gist](https://gist.github.com/samlecuyer/62ee98c6e4e5730681cbfd688fc1b908)

### samlecuyer commented Aug 23, 2016

 This is the source code for the post at http://cateches.is/post/149353123260/perlin-noise

### gfixler commented Sep 20, 2016

 "We can just add 1 to get a range of 0 to 1" Did you mean a range of 0 to 2? Also, you have a "nois" in there (should be "noise"). Fun post!
to join this conversation on GitHub. Already have an account? Sign in to comment