# Creating Perlin Noise

I’ve read articles and tutorials like this and this about perlin noise and found a way to do exactly what the pictures show.

Proof:

I don’t understand how I would add these together to get a nice outcome like this for example:

or

Could somebody help out on how to do such a thing? Here is my script:

``````function PerlinNoise(){
var texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
renderer.material.mainTexture = texture;
for(x=0; x<width; x++){
for(y=0; y<height; y++){
if(Mathf.Round(Random.Range(0.0, 1.0)) == 0){
color = Color.red;
}
else{
color = Color.white;
}
texture.SetPixel(x, y, color);
}
}
texture.Apply();
}
``````

Thanks!

here is a img of unity perlin for reference. it’s Mathf.PerlinNoise(x/10, 1.9); dots are x= 1,2,3 etc…

if you do perlin(x/10, 1.1) you’ll get a curve like that, 1.1 is the random seed of the curve.

The above function will repeat every 200 or so kilometres, I just found out because I made my terrain with perlin(x/1.0 , 88.2) type of functions, and they all loop exactly the same after roughly 20,000 unity units, which is a surprise if you thought you were making an endless landscape!!! so if you do stuff like

perlin(x*0.789 , 123)perlin(x0.987 , 321), then it should repeat every 1 zillion kilometers because they are at different periods

NOTE: perlin noise isn’t great for total randomisation. in fact it doesn’t even go from 0 to 1 as the reference says! if you use it for random, the chances of having 1 and 0 are smaller than the chance of .5 and .8, it seems to be mostly distributed in the centre, probably like a Gaussian curve. To be more evenly distributed, it would have to be zig zag looking not rounded.

I just did a test to count the distribution… it turns out that for lower seeds for example under 20, none of the curve points are higher than 0.9

``````for 100000 perlin points:

seed=1.9,
60 perlins> .85
ZERO perlins>.9 (WEIRD!)

seed =43.543
843 perlinpts> .85
137 perlinpts> .9
2694 perlinpts< .15
linear randomness would have given 15 000 above and below 8.5 and 1.5.

erm seed =4143.6543
663 perlinpts< 0.01, i.e 0.6 percent under 0.01
``````

so! a WEIRD distribution. handy but weird.

the distribution is bottom heavy… esp. with higher seeds. and doesnt go over .9 with small seeds like 1.1

for some purposes you will want to amplify your perlin function and use Mathf.PingPong 0,1,to make it more evenly distributed, and add another perlin to it for granularity.

Also to note: 1.432=seed for perlin is not the same curve as 123.432, it’s not modulo’d by 1.

to make more Granularity, to the perlin to make it possible to zoom in to more noise on smaller features… Unity perlin doesnt do that. you would have to add 2-3 perlin graphs together, 1 to make details on the 10/100th dimension, one on the 1/10, and one to make the large waves.

``````Random.seed = 1;
print (Random.value);
print (Random.value);

for (var p1:float = 0; p1 < 10000; p1 ++) {

var Perlin = Mathf.PerlinNoise(p1/100, 1.9);
Instantiate(digger, Vector3(p1-5000, Perlin*40, 0), rot3);
Debug.Log(     "Perlin  "   + Perlin );

}
``````

To create noise you need a float - it's not just a binary black or white, it's a smooth gradient.

As for how to do smooth color gradients, here's a snippet example:

``````//The two colors to generate noise between
var color1 = Color.white;
var color2 = Color.red;

var noise = .3; //You'd of course generate this from your perlin function
var perlinColor = new Color(color1.r * noise + color2.r * (1 - noise), color1.g * noise + color2.g * (1 - noise), color1.b * noise + color2.b * (1 - noise), color1.a * noise + color2.a * (1 - noise));

``````

Sorry if that doesn't quite work - I'm a C# programmer guessing at JS.

The procedural examples have a Perlin noise example, available here:

http://unity3d.com/support/resources/example-projects/procedural-examples.html