xxxxxxxxxx
let cols, rows;
let scl = 20;
let w = 900;
let h = 900;
let terrain = [];
let particles = [];
function setup() {
createCanvas(w, h, WEBGL);
cols = w / scl;
rows = h / scl;
for (let x = 0; x < cols; x++) {
terrain[x] = [];
for (let y = 0; y < rows; y++) {
terrain[x][y] = 0;
}
}
}
function draw() {
background('#4255A3');
rotateX(PI / 3);
translate(-w / 2, -h / 2);
// Define the colors for the terrain gradient
let terrainColors = [
color("#D0C473"),
color("#B3C268"),
color("#58CC91"),
color("#83A2D1"),
color("#5C76DF"),
color("#C06FC4"),
color("#E87DAD"),
];
// Draw the terrain
for (let y = 0; y < rows - 1; y++) {
beginShape(TRIANGLE_STRIP);
for (let x = 0; x < cols; x++) {
// Map the height of the terrain to a color in the gradient
let index = floor(
map(terrain[x][y], -350, 100, 0, terrainColors.length - 1)
);
fill(terrainColors[index]);
vertex(x * scl, y * scl, terrain[x][y]);
vertex(x * scl, (y + 1) * scl, terrain[x][y + 1]);
}
endShape();
}
// Generate the terrain using Perlin noise
let yoff = 0;
for (let y = 0; y < rows; y++) {
let xoff = 0;
for (let x = 0; x < cols; x++) {
terrain[x][y] = map(noise(xoff, yoff), 0, 1, -350, 100);
xoff += 0.2;
}
yoff += 0.2;
}
orbitControl();
// Add particles to the sky
if (random(1) < 10.0) {
let p = new Particle();
particles.push(p);
}
// Draw the particles
for (let i = particles.length - 1; i >= 0; i--) {
let p = particles[i];
p.update();
p.show();
if (p.pos.z < -200) {
particles.splice(i, 1);
}
}
}
class Particle {
constructor() {
this.pos = createVector(random(-w, w), random(-h, h), 400);
this.vel = createVector(0, 0, random(-10, -20));
this.acc = createVector(0, 0, 0);
this.size = random(1, 3);
this.color = color('#97CAE0');
}
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
show() {
noStroke();
fill(this.color);
push();
translate(this.pos.x, this.pos.y, this.pos.z);
sphere(this.size);
pop();
}
}
function keyTyped() {
if (key === "s" || key === "S") {
saveCanvas("mapgen_2-1_a-" + floor(random(100)), "png");
}
}