xxxxxxxxxx
119
const noiseScale = 0.1;
const s = 5;
let w, h;
let maxD;
let sun;
const maxH = 20;
function setup() {
createCanvas(400, 400);
w = width/s;
h = height/s;
noStroke();
maxD = min(w, h);
maxD = (maxD/2) * (maxD/2);
//sun = createVector(0, 0, 30);
sun = createVector(0, height/2, 0);
}
function draw() {
background(220);
for(let i = 0; i < w; i ++) {
for(let j = 0; j < h; j ++) {
tile(i, j);
}
}
//sun.x = mouseX/s;
//sun.y = mouseY/s;
const timeSlow = 10000;
sun.x = cos(millis()/timeSlow) * width/2 + width/2;
sun.z = (sin(millis()/timeSlow) + 1) * maxH * 2;
// inShadow(w/2, h/2);
}
function mouseReleased() {
noiseSeed(millis());
}
function tile(x, y) {
let n = noiseVal(x, y);
if(n <= 0.3) {
fill("#62A6A9");
} else if(n <= 0.4) {
fill("#D6B69E");
} else if(n <= 0.5) {
fill("#98AD5A");
} else if(n <= 0.6) {
fill("#658541");
} else if(n <= 0.7) {
fill("#477645");
} else if(n <= 0.8) {
fill("#6D7687");
} else if(n <= 0.9) {
fill("#848D9A");
} else {
fill("#D2E0DE");
}
rect(x * s, y * s, s, s);
if(inShadow(x, y, n * maxH)) {
fill(0, 50);
rect(x * s, y * s, s, s);
}
}
function inShadow(x, y, z) {
let pos = createVector(x, y, z);
const dx = abs(sun.x - pos.x);
const dy = abs(sun.y - pos.y);
const dz = abs(sun.z - pos.z);
const steps = max(max(dx, dy), dz);
for(let i = 0; i < steps; i ++) {
x = int(lerp(pos.x, sun.x, i/steps));
y = int(lerp(pos.y, sun.y, i/steps));
z = int(lerp(pos.z, sun.z, i/steps));
// fill(255, 0, 0);
// rect(x * s, y * s, s, s);
if(x === pos.x && y === pos.y) {
continue;
}
let h = noiseVal(x, y) * maxH;
if(h > z) {
return true;
}
}
return false;
}
function noiseVal(x, y) {
let n = noise(x * noiseScale, y * noiseScale)
n *= islandMod(x, y);
return n > 0.3 ? n : 0.3;
}
function islandMod(x, y) {
const dx = abs(w/2 - x);
const dy = abs(h/2 - y);
const dSq = (dx * dx) + (dy * dy);
return map(dSq, 0, maxD, 1, 0);
}