# Unity How to Implement Frequency into Perlin Noise

This topic is 2539 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 experimenting with Perlin Noise recently but I've encountered a problem that I can't figure out. Articles such as this say that frequency is implemented by taking 2 to the power of the current octave then you multiply x and y by that. As shown here:
 function PerlinNoise_2D(float x, float y) total = 0 p = persistence n = Number_Of_Octaves - 1 loop i from 0 to n frequency = 2i //2 to the power of the current octave amplitude = pi total = total + InterpolatedNoisei(x * frequency, y * frequency) * amplitude //multiply x and y by that end of i loop return total end function

Adding this into my script doesn't work and I have no idea why it just gives me a pure white texture.

My script uses Unity3D but don't worry you can easily tell what it's doing:
 using UnityEngine; using System.Collections; public class noise2 : MonoBehaviour { public int width; public int height; public int octaves; public float persistence; void Start(){ Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false); genorateWhiteNoise(texture); texture.Apply(); genorateSmoothNoise(texture); texture.Apply(); renderer.material.mainTexture = texture; } void genorateWhiteNoise(Texture2D texture){ System.Random random = new System.Random(0); //seed to 0 for testing for(int x = 0; x < width; x++){ for(int y = 0; y < height; y++){ float perlinValue = (float)random.NextDouble(); Color perlinColor = new Color(perlinValue, perlinValue, perlinValue, 1); texture.SetPixel(x, y, perlinColor); } } } void genorateSmoothNoise(Texture2D texture){ for(int o = 0; o < octaves; o++){ int frequency = Mathf.Pow(2, o); //same thing as above take 2 to the power of the current octave float amplitude = Mathf.Pow(persistence, o); for(int x = 0; x < width; x++){ for(int y = 0; y < height; y++){ float perlinValue = noise2D(x * frequency, y * frequency, texture) * amplitude;//multiply x and y by that Color perlinColor = new Color(perlinValue, perlinValue, perlinValue); texture.SetPixel(x, y, perlinColor); } } } } public float noise2D(int x, int y, Texture2D t){ 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))/4; Color sides = (t.GetPixel(x-1, y) + t.GetPixel(x+1, y) + t.GetPixel(x, y-1) + t.GetPixel(x, y+1))/4; Color center = t.GetPixel(x, y); return (corners.r + sides.r + center.r)/3; } }

Could I get help on implementing frequency into my script?

Thanks!

##### Share on other sites
maybe your other posts might shed some light

http://www.gamedev.net/topic/617270-need-help-with-perlin-noise/page__p__4895931#entry4895931

http://www.gamedev.net/topic/616892-creating-perlin-noise/page__p__4894021#entry4894021

##### Share on other sites

maybe your other posts might shed some light

http://www.gamedev.n...31#entry4895931

http://www.gamedev.n...21#entry4894021

What do you mean? That first one was about multiple octaves which got solved. The other one was about interpolating 2D noise which I'm still a little fuzzy on but think I got the hang of and this one is about frequency. Now that I think about it I should have probably made one thread but It would have been a little weird talking about one thing that doesn't have to do with the other. Sorry that offends you or whatever but there aren't many tutorials/articles/guides over this and a lot of them suggest I have a degree in math or don't quite go into enough detail. Not to mention each one does it a different way than the other.

##### Share on other sites

[quote name='freeworld' timestamp='1324603788' post='4896695']
maybe your other posts might shed some light

http://www.gamedev.n...31#entry4895931

http://www.gamedev.n...21#entry4894021

What do you mean? That first one was about multiple octaves which got solved. The other one was about interpolating 2D noise which I'm still a little fuzzy on but think I got the hang of and this one is about frequency. Now that I think about it I should have probably made one thread but It would have been a little weird talking about one thing that doesn't have to do with the other. Sorry that offends you or whatever but there aren't many tutorials/articles/guides over this and a lot of them suggest I have a degree in math or don't quite go into enough detail. Not to mention each one does it a different way than the other.
[/quote]

how bout checking out http://lmgtfy.com/?q...ng+perlin+noise

1: What does this do, is it just returning a random number or is it doing something more
[font="Arial"](float)random.NextDouble[/font][font="Arial"]();

[s]2: Your smooth function looks like it just adds all the neighboring colors together meaning, your color values are going to be way above 100%, meaning all white.[/s][/font] never mind, didn't notice the divide by 3.

3: What does persistence equal? when does it get set? How is it used in this function.. what does this function do exactly?
float amplitude = Mathf.Pow(persistence, o);

4: In your smooth noise function, you don't take into account the fact that your interating through the octaves loop over and over again resetting the texture data each time. Looks like only the last iteration will ever matter.

5: What does octaves equal? How high does it go?

6: what does noise2D * aamplitude return when amplitude is above 2 ? are you scaling the values at all? or do you just let them go above 100% like a suspect is happening?

##### Share on other sites

[quote name='MrSplosion' timestamp='1324605824' post='4896705']
[quote name='freeworld' timestamp='1324603788' post='4896695']
maybe your other posts might shed some light

http://www.gamedev.n...31#entry4895931

http://www.gamedev.n...21#entry4894021

What do you mean? That first one was about multiple octaves which got solved. The other one was about interpolating 2D noise which I'm still a little fuzzy on but think I got the hang of and this one is about frequency. Now that I think about it I should have probably made one thread but It would have been a little weird talking about one thing that doesn't have to do with the other. Sorry that offends you or whatever but there aren't many tutorials/articles/guides over this and a lot of them suggest I have a degree in math or don't quite go into enough detail. Not to mention each one does it a different way than the other.
[/quote]

how bout checking out http://lmgtfy.com/?q...ng+perlin+noise

1: What does this do, is it just returning a random number or is it doing something more
(float)random.NextDouble();

2: Your smooth function looks like it just adds all the neighboring colors together meaning, your color values are going to be way above 100%, meaning all white. never mind, didn't notice the divide by 3.

3: What does persistence equal? when does it get set? How is it used in this function.. what does this function do exactly?
float amplitude = Mathf.Pow(persistence, o);

4: In your smooth noise function, you don't take into account the fact that your interating through the octaves loop over and over again resetting the texture data each time. Looks like only the last iteration will ever matter.

5: What does octaves equal? How high does it go?

6: what does noise2D * aamplitude return when amplitude is above 2 ? are you scaling the values at all? or do you just let them go above 100% like a suspect is happening?
[/quote]

First of all sorry for the delayed post I was kinda busy anyway...

1. Yes, that is just returning a random number. First I fill a texture with random color values shown here which is why I need that.

2. Yeah I'm averaging the surrounding pixels to smooth it out.

3. I've been making persistence usually 0.25 or 0.5 because in that first article I linked there is a diagram with those values. I could be making that value totally wrong though. It gets set before anything happens then with each octave amplitude gets raised to the power of the current octave. So if I set persistence to 0.25 the first octave will have a amplitude of 1 (0.25^0) the second octave will have a amplitude of 0.25 (0.25^1) and so on.

4. No I'm not resetting the texture data I'm running the loop over and over with each successive octave. This better shows it. Right now it just smooths it out more and more because I need to implement frequency.

5. Like you saw in the image in number 4 it can be anything. The article says usually 8 octaves is good but I've been making it all over the place for testing. But again like I've said before all adding octaves does is smooth the texture out more are more because I need to implement frequency.

6. Noise2D() * amplitude seems to make the texture darker. I'm not sure on what it's suppose to accomplish but I just implemented it like shown in the article. I've just been setting it to 1 so nothing happens because I've been focusing on frequency right now but help on that would be great too.

Hope that answered all your questions so I can get some feedback .

##### Share on other sites
now I'm confused.. you say it produces an all white texture but the images you show, say otherwise? I love trying something new, so I''ll put together a shader that generates perlin noise. To better understand your situation.

##### Share on other sites

now I'm confused.. you say it produces an all white texture but the images you show, say otherwise? I love trying something new, so I''ll put together a shader that generates perlin noise. To better understand your situation.

Right it produces an all white texture with everything implemented exactly that script above. Oh and by the way it's only all white if persistence is 1 or higher else it's all black sorry for the confusion didn't think that would be a big deal. As for the other images not being all white that's because everything isn't implemented. In number 1 I said I fill a texture with random color values. That's all I did I looped through each pixel in the texture and assigned it a random color value nothing else, that's why it's not all white. Same thing for the other image, I didn't have frequency or amplitude I just smoothed it out with each octave. However with frequency and amplitude implemented yes, the texture is all white or black. I simply did not implement everything in order to better illustrate each answer.

##### Share on other sites
here's "my" perlin noise shader (I've simply converted someones c++ code to hlsl).

 float Noise(float x, float y) { return tex2D(noiseTex, float2(x / noisetexSize.x, y / noisetexSize.y)).x; } float Interpolate(float x, float y, float a) { float val = (1 - cos(a * 3.1415926535f)) * .5; return x * (1 - val) + y * val; } float Smooth(float x, float y) { float x_fl = floor(x); float y_fl = floor(y); float n1 = Noise(x_fl, y_fl); float n2 = Noise(x_fl + 1, y_fl); float n3 = Noise(x_fl, y_fl + 1); float n4 = Noise(x_fl + 1, y_fl + 1); float i1 = Interpolate(n1, n2, x - x_fl); float i2 = Interpolate(n3, n4, x - x_fl); return Interpolate(i1, i2, y - y_fl); } float PerlinNoise2d(float x, float y) { int octaves = 10; float total = 0.0f; float frequency = 4.0f; float persistence = 0.4f; float amplitude = 0.7f; float cloudCoverage = 0.0f; float cloudDensity = 0.9f; for(int lcv = 0; lcv <= octaves; ++lcv) { total += Smooth(x * frequency, y * frequency) * amplitude; frequency *= 2.0f; amplitude *= persistence; } total = (total + cloudCoverage) * cloudDensity; return clamp(total, 0.0f, 1.0f); } PS_output ps_Perlin(in VS_output_VS input) { PS_output output = (PS_output)0; output.Color = PerlinNoise2d(input.UV.x, input.UV.y); return output; } 

##### Share on other sites

here's "my" perlin noise shader (I've simply converted someones c++ code to hlsl).

 float Noise(float x, float y) { return tex2D(noiseTex, float2(x / noisetexSize.x, y / noisetexSize.y)).x; } float Interpolate(float x, float y, float a) { float val = (1 - cos(a * 3.1415926535f)) * .5; return x * (1 - val) + y * val; } float Smooth(float x, float y) { float x_fl = floor(x); float y_fl = floor(y); float n1 = Noise(x_fl, y_fl); float n2 = Noise(x_fl + 1, y_fl); float n3 = Noise(x_fl, y_fl + 1); float n4 = Noise(x_fl + 1, y_fl + 1); float i1 = Interpolate(n1, n2, x - x_fl); float i2 = Interpolate(n3, n4, x - x_fl); return Interpolate(i1, i2, y - y_fl); } float PerlinNoise2d(float x, float y) { int octaves = 10; float total = 0.0f; float frequency = 4.0f; float persistence = 0.4f; float amplitude = 0.7f; float cloudCoverage = 0.0f; float cloudDensity = 0.9f; for(int lcv = 0; lcv <= octaves; ++lcv) { total += Smooth(x * frequency, y * frequency) * amplitude; frequency *= 2.0f; amplitude *= persistence; } total = (total + cloudCoverage) * cloudDensity; return clamp(total, 0.0f, 1.0f); } PS_output ps_Perlin(in VS_output_VS input) { PS_output output = (PS_output)0; output.Color = PerlinNoise2d(input.UV.x, input.UV.y); return output; } 

Thanks for the script but you don't seem to be looping through each pixel in the texture. Where would that go? Would this line get looped with the x and y parameters changing?: total += Smooth(x * frequency, y * frequency) * amplitude;

Or perhaps the whole PerlinNoise2d(float x, float y) function gets looped with the x and y parameters changing?

##### Share on other sites
The idea is that each octave the amplitude of the signal should be halved(approximately), and the frequency should be doubled(again approximately).

psuedo code

freq = 1
amp = 0.5

noise = 0
for(each octave)
noise += noise(x*freq,y*freq)*amp
amp *= 0.5
freq *=2.0923 <--don't want exactly 2 as it makes the way perlin noise works stand out

The frequency adjusts how far apart the ripples are, and amplitude the strength

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633660
• Total Posts
3013221
×