Ok, so I’ve been experimenting with perlin noise and haven’t had that much luck. I find there aren’t many tutorials out their and articles such as this and this don’t really go into enough detail. Anyway here’s my problem:
I took the first article’s script I linked and converted it into UnityScript:
var width : int;
var height : int;
var octaves : int;
var persistence : float;
function Start(){
PerlinNoise();
}
function PerlinNoise(){
var texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
for(i = 0; i < octaves; i++){
var frequency = Mathf.Pow(2, i);
var amplitude = Mathf.Pow(persistence, i);
for(x = 0; x <= width; x++){
for(y = 0; y <= height; y++){
var noise = InterpolatedNoise(x * frequency, y * frequency) * amplitude;
var perlinColor = Color(noise, noise, noise, 0);
texture.SetPixel(x, y, perlinColor);
}
}
}
texture.Apply();
renderer.material.mainTexture = texture;
}
function InterpolatedNoise(x : int, y : int){
var initX = x;
var fractionX = x - initX;
var initY = y;
var fractionY = y - initY;
var v1 = SmoothNoise(initX, initY);
var v2 = SmoothNoise(initX + 1, initY);
var v3 = SmoothNoise(initX, initY + 1);
var v4 = SmoothNoise(initX + 1, initY + 1);
var i1 = Interpolate(v1, v2, fractionX);
var i2 = Interpolate(v3, v4, fractionX);
return Interpolate(i1, i2, fractionY);
}
function SmoothNoise(x : float, y : float){
corners = (Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1))/16;
sides = (Noise(x-1, y)+Noise(x+1, y)+Noise(x, y-1)+Noise(x, y+1))/ 8;
center = Noise(x, y)/4;
return corners + sides + center;
}
function Interpolate(a : float, b : float, x : float){
ft = x * 3.1415927;
f = (1 - Mathf.Cos(ft)) * 0.5;
return a*(1-f) + b*f;
}
function Noise(x : int, y : int){
var n = x + y * 57;
n = (n<<13) ^ n;
var res = (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
return res;
}
After doing that I wasn’t surprised to find this function gave me a number overflow error:
function Noise(x : int, y : int){
var n = x + y * 57;
n = (n<<13) ^ n;
var res = (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
return res;
}
I figured I would just use the random method, just to see what would happen. I know it’s not pseudo but I figured at least something random would show up. After doing that I got this picture. Although It looks cool it obviously isn’t perlin noise.
So basically what did I do wrong? My logic is telling me that the pixels are in that pattern because the random method inside the function I replaced isn’t perlin. I can’t use the other function though because it throws me a number overflow error as explained before. So what’s a good perlin number generator that doesn’t give me an error, or is their more to this? Could someone please help me out I want to be able to create noise like this.
Thanks!