Processing 我的中点置换生成岛屿的实现没有创建所需的图案?
我想从“130行中的真实地形”的处理端口找到的代码开始创建一个效果,如中的效果。我修改了代码,使其在处理过程中只生成灰度高度图,而不是三维地形。然而,我的代码并没有生成岛屿高度图,也留下了一个不寻常的正方形图案,如图所示 我的代码如下:Processing 我的中点置换生成岛屿的实现没有创建所需的图案?,processing,terrain,heightmap,Processing,Terrain,Heightmap,我想从“130行中的真实地形”的处理端口找到的代码开始创建一个效果,如中的效果。我修改了代码,使其在处理过程中只生成灰度高度图,而不是三维地形。然而,我的代码并没有生成岛屿高度图,也留下了一个不寻常的正方形图案,如图所示 我的代码如下: int size = 512; Terrain t; float rough = 0.7; void setup() { t = new Terrain(8, size); doit(); } void doit() { t.generat
int size = 512;
Terrain t;
float rough = 0.7;
void setup() {
t = new Terrain(8, size);
doit();
}
void doit() {
t.generate(rough);
size(513, 513);
loadPixels();
for (int a=0;a<t.tMap.length;a++) {
float val = ((t.tMap[a]+24)/48)*255;
pixels[a] = color(floor(val));
}
updatePixels();
}
void mousePressed() {
doit();
}
class Terrain {
public final int tSize, tMax;
public float[] tMap;
public float tRoughness;
Terrain(int detail, int size) {
tSize = size+1;
tMax = tSize-1;
tMap = new float[tSize*tSize];
}
private float getp(int x, int y) {
if (x < 0) x= tMax + x;
if (x > tMax) x= x % tMax;
if (y < 0) y= tMax + y;
if (y > tMax) y= y % tMax;
return tMap[x + tSize * y];
}
private void setp(int x, int y, float val) {
tMap[x + tSize * y] = val;
}
public void generate(float roughness) {
tRoughness = roughness;
//set edges and corners 0
for (int x=0;x<tSize;x++) {
setp(x, 0, 0);
setp(x, tMax, 0);
}
for (int y=0;y<tSize;y++) {
setp(y, 0, 0);
setp(y, tMax, 0);
}
//seed random numbers every 16
for (int x=0;x<tSize;x+=16) {
for (int y=0;y<tSize;y+=16) {
setp(x, y, random(-(roughness*16), roughness*16));
}
}
//divide each 16x16 square
for (int x=0;x<tSize;x+=16) {
for (int y=0;y<tSize;y+=16) {
divide(16, x, y);
}
}
}
private void divide(int size, int smallestX, int smallestY) {
int x, y, half = size/2;
float scale = tRoughness * size;
if (half < 1) return;
for (y = smallestY + half; y < tMax; y += size) {
for (x = smallestX + half; x < tMax; x += size) {
square(x, y, half, random(-scale, scale));
}
}
for (y = smallestY; y <= tMax; y += half) {
for (x = (y + half + smallestY) % size; x <= tMax; x += size) {
diamond(x, y, half, random(-scale, scale));
}
}
divide(size/2, smallestX, smallestY);
}
private float average(float[] values) {
int valid = 0;
float total = 0;
for (int i=0; i<values.length; i++) {
if (values[i] != -1) {
valid++;
total += values[i];
}
}
return valid == 0 ? 0 : total / valid;
}
private void square(int x, int y, int size, float offset) {
float ave = average(new float[] {
getp(x - size, y - size), // upper left
getp(x + size, y - size), // upper right
getp(x + size, y + size), // lower right
getp(x - size, y + size) // lower left
}
);
setp(x, y, ave + offset);
}
private void diamond(int x, int y, int size, float offset) {
float ave = average(new float[] {
getp(x, y - size), // top
getp(x + size, y), // right
getp(x, y + size), // bottom
getp(x - size, y) // left
}
);
setp(x, y, ave + offset);
}
}
int size=512;
地形t;
浮子粗糙度=0.7;
无效设置(){
t=新地形(8,尺寸);
doit();
}
void doit(){
t、 生成(粗糙);
大小(513513);
loadPixels();
对于(int a=0;a tMax)x=x%tMax;
如果(y<0)y=tMax+y;
如果(y>tMax)y=y%tMax;
返回tMap[x+tSize*y];
}
私有无效设置(整数x,整数y,浮点值){
tMap[x+tSize*y]=val;
}
公共空隙生成(浮动粗糙度){
槽度=粗糙度;
//将边和角设置为0
对于(int x=0;x您首先将地形分解为16x16个块。在输出中,这些边界非常可见。这是为什么?因为边界处的值的差异比每个块内的值的差异大得多
块之间的差异由初始的generate
值决定,与块之间的差异是由divide
引起的。您需要在generate
中增加变化,或在divide
中减少变化
即使如此,在生成所有块后,在整个图像上应用平滑操作符也是值得的。这会稍微降低粗糙度,但会进一步降低特定边界的可见性(特别是对人眼而言;统计测试仍然可能证明图像是由轴对齐过程生成的)这非常有帮助,谢谢!