How does Mathf.PerlinNoise Work?

I’ve been trying to make a sort of randomized terrain for a while, and I’m unable to write a perlin noise function myself, partly due to laziness and partly to being a total noob.

So I borrowed the Mathf.PerlinNoise function. The documentation says that you give it the two coordinates, and it gives you a random number on a texture with the same coordinates or something.

But when I printed a bunch of Mathf.PerlinNoises, with different hand-typed values, all of them printed the same number : 0.4652731. So can anyone explain this to me?

EDIT:
Here’s my script:

function Start () {
	print(Mathf.PerlinNoise(0, 0));
	print(Mathf.PerlinNoise(2, 0));
	print(Mathf.PerlinNoise(0, 5));
	print(Mathf.PerlinNoise(10, 3));
	print(Mathf.PerlinNoise(2, 0));
	print(Mathf.PerlinNoise(1, 1));
}

Here’s the results (one of them):

0.4652731
UnityEngine.MonoBehaviour:print(Object)
PerlinHeight:Start() (at Assets/PerlinHeight.js:5)

4 Answers

4

Whole numbers will return the same result; use fractions.

Ohhhh... Thanks! I'll experiment a bit with it.

Mathf.PerlinNoise work with smaller values marked as float.
Like:

Debug.Log(Mathf.PerlinNoise(0 / 100f, 0 / 100f));
Debug.Log(Mathf.PerlinNoise(2 / 100f, 0 / 100f));
Debug.Log(Mathf.PerlinNoise(0 / 100f, 5 / 100f));
Debug.Log(Mathf.PerlinNoise(10 / 100f, 3 / 100f));
Debug.Log(Mathf.PerlinNoise(2 / 100f, 0 / 100f));
Debug.Log(Mathf.PerlinNoise(1 / 100f, 1 / 100f));

Think of PerlinNoise() as producing value in a random terrain. That is adjacent values are not random, but instead will produce similar values (perhaps a bit more, or perhaps a bit less) as if you are walking some hilly countryside. I find that when working with Perlin noise I have to get the range of values right. Here is a answer with a script for creating a hilly plane. It should be straight forward to modify it for use in a Terrain.

http://answers.unity3d.com/questions/417793/perlin-noise-plane-manipulation.html

But I don't want a mesh - I want the heights of different points.

The mesh part is irrelevant; the point is that it's a demonstration of how perlin noise works.

As @Erich5h5 says, the link was to demonstrate how to use PerlinNoise(). Lines 24-26 make the height calculation. The height of the mesh (which will be the same or similar calculation for a Terrain) is done on line 26. If you know Terrains, the conversion should be easy.

I’ve never used it, but from the documentation, it appears that given x and y coordinates, the algorithm will return a value between 0.0 and 1.0. Different coordinates should give different values.

That's what I thought too, but different coordinates somehow yield the same results.