【发布时间】:2014-12-24 01:38:48
【问题描述】:
我一直在为我想要编程的游戏尝试使用 perlin noie 生成,但我遇到了一些奇怪的结果。当我让我的代码运行并以灰度绘制结果时,我得到以下图像:
我的代码基于关于 perlin noise 的维基百科页面和其中一个参考页面(下面的链接)。假设基于预生成的半随机向量网格为每个点生成 perlin 噪声点。
public static double[][] generateMap(long seed, int width, int height) {
double[][] map = new double[width][height];
double[][][] grid = generateGrid(seed, (int) (width/10)+1, (int) (height/10)+1);
double max = Double.MIN_VALUE;
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
double p = perlinNoise(((double) i) / 10, ((double) j) / 10, grid);
map[i][j] = p;
max = (max < p) ? p : max;
}
}
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
map[i][j] = map[i][j] / max;
}
}
return map;
}
private static double perlinNoise(double x, double y, double[][][] grid) {
int x0 = ((x < 0) ? (int) x-1 : (int) x);
int y0 = ((y < 0) ? (int) y-1 : (int) y);
int x1 = x0 + 1;
int y1 = y0 + 1;
double tx = x - (double)x0;
double ty = y - (double)y0;
double wx = tx*tx*(3-2*tx);
double wy = ty*ty*(3-2*ty);
double l0, l1, ix, iy, p;
l0 = dotGrid(x0, y0, x, y, grid);
l1 = dotGrid(x1, y0, x, y, grid);
ix = cerp(l0, l1, wx);
l0 = dotGrid(x0, y1, x, y, grid);
l1 = dotGrid(x1, y1, x, y, grid);
iy = cerp(l0, l1, wx);
p = cerp(ix, iy, wy);
return p;
}
private static double lerp(double a, double b, double w) {
return (1.0f - w)*a + w*b;
}
private static double cerp(double a, double b, double w) {
double ft = w * 3.1415927;
double f = (1 - Math.cos(ft)) * 0.5;
return a*(1-f) + b*f;
}
private static double dotGrid(int i, int j, double x, double y, double[][][] grid) {
double dx = x - i;
double dy = y - j;
return (dx*grid[i][j][0] + dy*grid[i][j][1]);
}
private static double[][][] generateGrid(long seed, int width, int height) {
Random r = new Random(seed);
double[][][] grid = new double[width][height][2];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
double x = r.nextFloat();
double y = r.nextFloat();
double v = Math.sqrt((x*x) + (y*y));
grid[i][j][0] = x/v;
grid[i][j][1] = y/v;
}
}
return grid;
}
对于那些想要测试我的代码的人,我还将包括我的渲染方法:
private void drawMap(double[][] map, Graphics g) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
float d = (float) Math.abs(map[i][j]);
d = (d > 1.0f) ? 1.0f : d;
Color c = new Color(d, d, d);
g.setColor(c);
g.fillRect(i, j, 1, 1);
}
}
}
我希望有人能告诉我为什么我的噪音中有这些奇怪的迷宫状结构,以及如何摆脱它们。
来源:
http://en.wikipedia.org/wiki/Perlin_noise
http://webstaff.itn.liu.se/~stegu/TNM022-2005/perlinnoiselinks/perlin-noise-math-faq.html
【问题讨论】: