- Home /
Question by
kag359six · Aug 22, 2012 at 07:35 PM ·
randomprogramming
Whats wrong with this PerlinNoise function
Everytime i use this noise function it outputs the same value, no matter what the input is.
static float noise2D(int x, int y) {
int n = x + y * 57;
n = (n<<13) ^ n;
return (float)( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & (int)7) / 1073741824.0);
}
static float smoothenNoise2D(int x, int y) { //smoothens surrounding points
//starting from center, get corner points and add them together and divide by 16
float corners = (noise2D(x-1,y-1) + noise2D(x + 1, y - 1) + noise2D(x + 1, y + 1) + noise2D( x- 1, y+1)) / 16;
//starting from center, get center of each side
float sides = (noise2D(x - 1, y) + noise2D(x + 1, y) + noise2D(x, y -1) + noise2D(x, y+1)) / 8;
//get center point
float center = noise2D(x,y) / 4;
return corners + sides + center;
}
static float interpolateNoise2D(float x, float y) {
int integerX = (int)x;
int integerY = (int)y;
float fractionalX = x - integerX;
float fractionalY = y - integerY;
float v1 = smoothenNoise2D(integerX, integerY);
float v2 = smoothenNoise2D(integerX + 1, integerY);
float v3 = smoothenNoise2D(integerX, integerY + 1);
float v4 = smoothenNoise2D(integerX + 1, integerY + 1);
float interpX = linearInterpolation(v1,v2,fractionalX);
float interpY = linearInterpolation(v3,v4,fractionalY);
return linearInterpolation(interpX, interpY, fractionalY);
}
public static float perlinNoise2D(float gx,float gy, int goctaves, float gpersistence) {
x = gx;
octaves = goctaves;
persistence = gpersistence;
y = gy;
float total = 0;
int oct = octaves - 1;
for(int i = 0; i < oct; i++) {
float frequency = Mathf.Pow(2, i);
float amplitude = Mathf.Pow (gpersistence,i);
total += interpolateNoise2D(gx * frequency, gy * frequency) * amplitude;
}
return total;
}
Comment
Your answer