# What is Perlin noise?

Are you looking for ways to produce natural-looking textures and content for your game projects? Then, you should know about Perlin noise.

Perlin noise is a type of gradient noise that is ideal for producing realistic content for games. In this article, you will be provided with all you need to know about Perlin noise and how to use it in Unity.

## What is Perlin noise?

Perlin noise is a procedural generation algorithm developed by Ken Perlin in 1983. (Fun fact: Ken Perlin earned his Ph.D at New York University and works at a Professor there. The founder of OccaSoftware, Michael Sacco, also attended New York University).

Perlin Noise is a type of gradient noise used by artists to enhance realism in graphics. This noise can be implemented for any number of dimensions, including one, two, three, or four dimensions.

## What is Perlin Noise good for?

Ken Perlin’s thirst for ultimate realism in computer graphics led to the birth of this interesting algorithm.

The algorithm is used to produce or simulate natural effects like; clouds, fire, and landscapes with procedural textures, allowing artists to better represent natural phenomena in visual effects.

Ken Perlin mentioned this when he was awarded the Academy Award for Technical Achievement in 1997.

Perlin noise is often used in CGI to make graphics appear more natural and realistic. Generally, Perlin noise is used to create natural-looking textures, terrains, and animations for video games.

## How does Perlin Noise work?

Now, we understand what Perlin noise is. Let’s take a look at the working process of the algorithm.

The foundation of the Perlin noise is based on pseudo-random number generation.

Initially, it generates an evenly distributed set of pseudo-random numbers (or vectors). This set of numbers are placed across a grid of n dimensions.

Next, the algorithm calculates a gradient vector for each grid cell based on how the vector would need to change to meet the corner points. This concept of “how would it change?” is what makes the Perlin Noise a gradient noise.

The algorithm then calculates the dot product between the gradient vector and the distance vector from the grid point to the four corners of the grid cell. Then, there’s an interpolation of the dot products to get the final noise value for the grid point. This interpolation is done to ensure smooth and continuous noise.

The fact that the noise produced is smooth and continuous makes it a procedural algorithm.

## Common use cases of Perlin noise

The Perlin noise algorithm is useful for a wide range of applications.

It can be used to generate natural-looking terrains and landscapes for video games.

Perlin noise produces realistic terrains to enhance the realism of game scenes. It can also be used to generate textures and content for video games.

The use of Perlin noise helps simulate natural effects on textures, enhancing the natural feel of textures. Perlin noise can also be used to generate realistic animations for games with visual effects like; cloud, fire, or smoke to enhance the visual ambiance of scenes.

Overall, the Perlin noise algorithm is significantly useful for game developers who thirst for ultimate realism in their game projects.

## Perlin noise in Unity.

Unity exclusively supports the use of the Perlin noise algorithm by the provision of a built-in implementation for generating 2D Perlin noise values - `Mathf.PerlinNoise`

.

Here’s a basic example implementation in Unity;

```
using UnityEngine;
public class PerlinNoiseExample : MonoBehaviour
{
public int width = 256;
public int height = 256;
public float scale = 20f;
void Start()
{
GeneratePerlinNoise();
}
void GeneratePerlinNoise()
{
Texture2D texture = new Texture2D(width, height);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
float xCoord = (float)x / width * scale;
float yCoord = (float)y / height * scale;
float sample = Mathf.PerlinNoise(xCoord, yCoord);
Color color = new Color(sample, sample, sample);
texture.SetPixel(x, y, color);
}
}
texture.Apply();
// Assign the generated texture to a material or UI element
GetComponent<Renderer>().material.mainTexture = texture;
}
}
```

In the above example script, the Perlin Noise algorithm was used in a function called `GeneratePerlinNoise`

to calculate the Perlin Noise values for each pixel in a texture. The texture generated was then applied to a material to visualize the generated Perlin noise texture.

Learn more about Unity’s Perlin Noise implementation in their documentation.

## Conclusion

The Perlin noise algorithm is used to enhance the realism of game projects.

You should try out this algorithm today to experience realism in full.