xxxxxxxxxx
99
// noprotect
// https://github.com/CodingTrain/website/blob/master/CodingChallenges/CC_011_PerlinNoiseTerrain_p5.js/sketch.js
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/IKB1hWWedMk
// Edited by SacrificeProductions
// mod kll & monkstone / oct 2018
var cols, rows;
var scl = 20;
var w = 1000;
var h = 600;
var x, y;
var flying = 0;
var ang = 0,
speed = 0.07;
var terrain = {};
var lake = 0,
lowlim = -45;
var showStroke = false;
function setup() {
createCanvas(700, 600, WEBGL);
cols = w / scl;
rows = h / scl;
ang = PI / 5;
print("cols " + cols + " rows " + rows);
print("use mouseWheel to change view angle");
print("click on canvas window first and use: \n [UP] [DOWN] key for water level and [s] for stroke, [f] prints FPS");
}
function draw() {
make_terrain();
background(20, 20, 180);
if (showStroke) stroke(200, 200, 0);
else noStroke();
translate(0, 50);
rotateX(ang);
translate(-w / 2, -h / 2);
draw_terrain();
}
function make_terrain() {
flying -= speed;
var yoff = flying;
for (y = 0; y < rows; y++) {
var xoff = 0;
for (x = 0; x < cols; x++) {
terrain[hash_key(x, y)] = constrain(map(noise(xoff, yoff), 0, 1, -100, 100), lowlim, 100);
xoff += 0.2;
}
yoff += 0.2;
}
}
function draw_terrain() {
for (y = 0; y < rows - 1; y++) {
beginShape(TRIANGLE_STRIP);
for (x = 0; x < cols; x++) {
var one = terrain[hash_key(x, y)];
var two = terrain[hash_key(x, y + 1)];
if (one <= lowlim) lake = 80;
else lake = 0; // lakes
fill(50, 150 - int(one), 50 + lake); // mountain color green to grey
vertex(x * scl, y * scl, one);
vertex(x * scl, (y + 1) * scl, two);
}
endShape();
}
}
function mouseWheel(event) {
ang += event.delta / 50.0;
return false;
}
function keyPressed() {
if (keyCode === UP_ARROW) print("lowlim: " + (lowlim+=5));
if (keyCode === DOWN_ARROW) print("lowlim: " + (lowlim-=5));
if (key === 's') print("showStroke: " + (showStroke=!showStroke));
if (key === 'f') print(nf(frameRate(), 0, 1) + " FPS");
}
function hash_key(x, y) {
return w * y + x;
}