/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.p$lw1XyfZaU/rev.1436
*
* authors:
* Gum
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
/*
Procedural Terrain Generation using 2-Dimensional Perlin Noise for Heightmapping
Written in Java
*/
float[][] tiles = {
{24, 53, 80, 119}, // deep ocean
{25, 94, 128, 178}, // ocean
{7, 149, 180, 226}, // clear ocean
{2, 229, 237, 249}, // foamy waters
{.75, 232, 220, 167}, // dark sand
{2, 252, 240, 189}, // sand
{7, 156, 234, 143}, // light grass
{2, 83, 170, 68}, // dark grass
{5.5, 173, 117, 62}, // mountain
{3.5, 209, 145, 81}, // higher mountain
{38.5, 255, 255, 255} // snowcaps
};
int zoom = 9;
boolean showGrid = false;
boolean move = true;
boolean UI = true;
float j = 0;
float spd = 0;
float scl = 0.08;
float yspd = 0;
float xmovement = 0;
float ymovement = 0;
int cols = 120;
int rows = 120;
float heightmap;
int[][] terrain = new int[cols][rows];
int[] tileRGB = new int[3];
int[] returnTile(float hmap)
{
//print(hmap+"\n");
int[] rgb = new int[3];
rgb[0] = 40;
rgb[1] = 20;
rgb[2] = 60;
for(int tile = 0; tile < tiles.length; tile++)
{
if(tile>0)
{
if(tiles[tile-1][0]<hmap&&tiles[tile][0]>hmap)
{
rgb[0] = int(tiles[tile][1]);
rgb[1] = int(tiles[tile][2]);
rgb[2] = int(tiles[tile][3]);
}
}
else
{
if(hmap >= 0 && hmap < tiles[tile][0])
{
rgb[0] = int(tiles[tile][1]);
rgb[1] = int(tiles[tile][2]);
rgb[2] = int(tiles[tile][3]);
}
}
}
return rgb;
}
void setup()
{
size(840,840);
noStroke();
//stroke(0);
j = 0;
for(int t = 0; t < tiles.length; t++)
{
j += (tiles[t][0]);
tiles[t][0] = j;
print(tiles[t][0]+"\n");
}
}
void draw()
{
background(0);
if(showGrid)
{
strokeWeight(1);
stroke(255);
}
else
{
noStroke();
}
for(int x = 0; x < cols; x++)
{
for(int y = 0; y < rows; y++)
{
heightmap = map(noise((x*scl)+xmovement, (y*scl)+ymovement),0,1,0,j+2);
tileRGB = returnTile(heightmap);
fill(tileRGB[0],tileRGB[1],tileRGB[2]);
rect(x*ceil(width/cols), y*ceil(height/rows), (x+1)*ceil(width/cols), (y+1)*ceil(height/rows));
}
}
if(move)
{
xmovement += spd;
ymovement += yspd;
}
stroke(255,255,0);
strokeWeight(4);
noFill();
ellipse(width/2, height/2, 20, 20);
noStroke();
fill(0);
if(UI)
{
myFont = createFont("Century Schoolbook L Bold", 32);
textFont(myFont);
textSize(38);
text("Procedural Terrain", 5, 30);
textSize(18);
text("Controls:\nQ - Zoom Out\nE - Zoom In\nWASD - Movement (Increases Acceleration)\nH - Toggle Help UI\nG - Toggle Grid\n\nIf UI or Grid is flashing, move in any direction.",5, 50);
}
if(keyReleased)
{
if(key == 'h' || key == 'H')
{
UI = !UI;
}
if(key == 'g' || key == 'G')
{
showGrid = !showGrid;
}
}
if(keyPressed)
{
if(key == 'q' || key == 'Q')
{
if(zoom < 9)
{
zoom += 1;
cols = 15*zoom;
rows = 15*zoom;
}
}
if(key == 'e' || key == 'E')
{
if(zoom > 1)
{
zoom -= 1;
cols = 15*zoom;
rows = 15*zoom;
}
}
if(key == 'a' || key == 'A')
{
spd -= 0.01;
}
if(key == 'd' || key == 'D')
{
spd += 0.01;
}
if(key == 'w' || key == 'W')
{
yspd -= 0.01;
}
if(key == 's' || key == 'S')
{
yspd += 0.01;
}
}
}