# Need Help with Perlin Noise

This topic is 2348 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I've been trying to make perlin noise but haven't been successful. I find there aren't many sources out there and articles such as this and this don't really go into enough detail. Anyway here I go...

First, could I be confirmed my concept on perlin noise it right? From what I understand and please correct me if I'm wrong, you create a texture with random color values by looping through each pixel. From doing this you get pure randomness. The next step is to smooth that texture out by averaging out the neighboring pixel values. Now you have this. After that you... well I don't know here is where I'm stuck. The first article I linked talks about interpolating. Which brings me to another question. What is the point of interpolating 2D noise? I understand doing it with 1D noise because it smooths out the line instead of a harsh liner one. However, with 2D noise there is no line, just a bunch of pixels. Plus I'm already smoothing it out by averaging the surrounding pixel as explained before so why do it again? Continuing on to another problem... That first article also talks about using multiple octaves witch I'm pretty sure I understand. You create multiple smoothed out textures add them up and there you go. In the pseudo code (at bottom of article) for every x octaves you have you call the function x times. Straight from the code (with a few of my comments) here is that function:
 function PerlinNoise_2D(float x, float y) total = 0 p = persistence n = Number_Of_Octaves - 1 loop i from 0 to n //run function once for each octave frequency = 2i amplitude = pi //loop through each pixel in texture here total = total + InterpolatedNoisei(x * frequency, y * frequency) * amplitude //end of pixel loop end of i loop return total end function

Calling my function once for each octave like above gives me this. [color="#111111"][font="Helvetica, Arial, sans-serif"]Obviously it isn't noisy enough to really be used as anything unless you want something really subtle. Now my logic tells me that the texture looks that way because I'm not multiplying each pixel by the frequency. Adding this to my script breaks it though and I get a pure black texture. The texture might also look like that because I'm not interpolating which I don't understand as explained before.[/font]
[color="#111111"][font="Helvetica, Arial, sans-serif"] [/font]
[color="#111111"][font="Helvetica, Arial, sans-serif"]Short Version: What is the concept behind interpolating 2D noise? Also how can I add persistence, octaves, frequency and amplitude to create a nice cloudy texture?[/font]
[font="Helvetica, Arial, sans-serif"] [/font]
[font="Helvetica, Arial, sans-serif"][color="#111111"]Here is my script:[/font]
[font="Helvetica, Arial, sans-serif"][color="#111111"] void Start () { System.Random random = new System.Random(); //purely randomize the texture for(int x = 0; x < width; x++){ for(int y = 0; y < height; y++){ float numb = (float)random.NextDouble(); //set pixel color to the numb value } } //now smooth the random texture for(int i = 0; i < octaves; i++){ for(int x = 0; x < width; x++){ for(int y = 0; y < height; y++){ float numb = smoothNoise(x, y); //i realize it should be "float numb = smoothNoise(x * frequency, y * frequency) * amplitude" but like explained above it didn't work. //set pixel to the numb value } } } } public float smoothNoise(int x, int y){ //here "t" is just the texture. "GetPixel()" gets the color of the pixel at the specified x and y cordinate Color corners = (t.GetPixel(x-1, y-1) + t.GetPixel(x-1, y+1) + t.GetPixel(x+1, y-1) + t.GetPixel(x+1, y+1))/16; Color sides = (t.GetPixel(x-1, y) + t.GetPixel(x+1, y) + t.GetPixel(x, y-1) + t.GetPixel(x, y+1))/8; Color center = t.GetPixel(x, y)/4; return corners.r + sides.r + center.r; } 
[/font]
[font="Helvetica, Arial, sans-serif"] [/font][color="#111111"][font="Helvetica, Arial, sans-serif"] [/font]
[color="#111111"][font="Helvetica, Arial, sans-serif"]Anything is welcome and I need all the help I can get but please don't just post your script (unless it's simple) because usually when people do that they're five times more complex and just add to my confusion.[/font]
[color="#111111"][font="Helvetica, Arial, sans-serif"] [/font]
[color="#111111"][font="Helvetica, Arial, sans-serif"]Thanks![/font]

##### Share on other sites
2D Perlin noise is a function that given two real numbers returns a value. For points with integer coordinates, it returns something random. For other points, it returns a value that is interpolated between the surrounding points with integer coordinates. I you want to see what this looks like in an image, zoom in so 1 in the space of the noise function is something like 16 pixels.

You can add several 2D noise functions using different scales (sometimes called "octaves" because going up an octave in music means doubling the frequency):
noise1(x,y) + 1/2 * noise2(2*x,2*y) + 1/4 * noise3(4*x,4*y) + 1/8 * noise4(8*x,8*y) + ...

That's my 1-minute introduction to Perlin noise. I hope it helps you go through the links you posted, which seemed reasonable to me. You may also enjoy this book on the subject.

• 16
• 9
• 13
• 41
• 15