I have finally gotten deeper into programming with Processing and recently watched a video on terrain generation by Daniel Shiffman (The Coding Train) on 3D Terrain Generation with Perlin Noise in Processing .
Daniel is an excellent teacher and provides complete source code for all his presentations.
This is a little recorded sample of my attempt to implement it and add some Minecraft-like coloring.
Here is my Processing code, based heavily on Daniel’s.
// Terrain Generation and Flyby using Processing
// Douglas E. Welch After Daniel Schiffman - TechIQ.welchwrite.com
int cols, rows;
int scl = 20;
int w = 2200;
int h = 2200;
float flying=0;
float [][] terrain;
void setup() {
size(1280, 720, P3D);
cols = w /scl;
rows = h/scl;
terrain= new float[cols][rows];
}
void draw() {
flying += - .1;
float yoff = flying;
for (int y = 0; y< rows; y++) {
float xoff=0;
for (int x=0; x < cols; x++) {
terrain[x][y] = map(noise(xoff, yoff), 0, 1, -150, 150);
xoff+=0.1;
}
yoff +=0.1;
}
background(0);
//noStroke();
stroke(75);
fill(175);
//noFill();
translate(width/2, height/2);
rotateX(PI/3);
translate(-w/2, -h/2);
for (int y = 0; y< rows-1; y++) {
beginShape(TRIANGLE_STRIP);
for (int x=0; x < cols; x++) {
//rect(x*scl, y*scl, scl, scl);
vertex(x*scl, y*scl, terrain[x][y]);
vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
// Water
if (terrain[x][y] <= -60) {
fill(0, 0, 150);
stroke(0,0,150);
}
//Plains
if (terrain[x][y] >= -59 && terrain[x][y] <=0) {
stroke(0,100,0);
fill(0, 100, 0);
}
//Stone
if (terrain[x][y] >= 0 && terrain[x][y] <=50) {
stroke(51);
fill(51);
}
// Snow
if (terrain[x][y] > 50) {
stroke(255);
fill(255, 255, 255);
}
}
endShape();
}
saveFrame("output/ter-#####.png");
}
Comments