2014-09-03 42 views
1

我想創建一個效果,如this article中的效果,從在'逼真地形的130行'處理端口找到的代碼開始。我修改了代碼,因此它只能在Processing中生成灰度高度圖,而不是3D地形。但是,我的代碼不會生成島狀高度圖,並且還會留下不尋常的方形圖案,如圖所示here我的中點位移生成島的實現不會創建所需的模式?

我的代碼如下:

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); 
    } 
} 

感謝您的幫助!

回答

1

您首先在16x16塊中分解地形。在輸出中,這些邊界非常明顯。這是爲什麼?那麼,因爲邊界上的數值比每個數據塊中的數值有很大差異。

塊之間的差異由初始generate值確定,與塊的差異由divide引起。在divide中,您需要generate或更少的更多變體。

即使在所有塊生成後,在整個圖像上應用平滑算子也是值得的。這會降低粗糙度,但它會進一步降低特定邊界的可見性(特別是對人眼;統計測試仍然可能證明該圖像是由軸對齊過程生成的)

+0

這有助於相當位,謝謝! – Zarpar 2014-09-03 13:35:14

相關問題