Mod: Coding Challenge #11: 3D Terrain Generation with Perlin Noise in Processing
やったやつ上げ忘れていたのであげておく。OpenProcessing用にp5.jsに移植しようと思ったけどWEBGLのレンダラだとTRIANGLE_STRIPが使えなかったのでおあずけ。GIFで。
コード
int w, h, rows, cols; int scale = 20; float[][] terrain; // setup関数 : 初回1度だけ実行される void setup() { size(960, 540, P3D); // ウィンドウサイズを960px,540pxに colorMode(HSB, 360, 100, 100, 100); // HSBでの色指定にする w = 1800; h = 1800; rows = h/scale; cols = w/scale; terrain = new float[cols][rows]; for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { terrain[x][y] = map(noise(x*0.1, y*0.05), 0, 1, -100, 100); } } } // draw関数 : setup関数実行後繰り返し実行される void draw() { background(220, 100, 100); for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { terrain[x][y] = map(noise(x*0.1, (y-frameCount)*0.05), 0, 1, -100, 100); } } lights(); translate(width/2, height/2+sin(frameCount*0.007)*150); rotateX(PI/3); rotateY(map(sin(frameCount*0.001), -1, 1, PI/6, -PI/6)); translate(-w/2, -h/2); int i = 0; for (int y = 0; y < rows-1; y++) { beginShape(TRIANGLE_STRIP); for (int x = 0; x < cols; x++) { float z1 = terrain[x][y]; float z2 = terrain[x][y+1]; stroke(0, 0, 100, 20); float saturation = map(y, 0, rows-1, 0, 80); float brightness = map((z1+z2)/2, -150, 150, 0, 100); fill(120, brightness, saturation); i++; vertex(x*scale, y*scale, z1); vertex(x*scale, (y+1)*scale, z2); } endShape(); } }