xxxxxxxxxx
65
let terrain;
let cols, rows;
let w = 25;
let flying = 0;
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
cols = floor(width / w);
rows = floor(height / w);
terrain = new Terrain(cols, rows);
// Set the camera position and orientation
let camX = width;
let camY = -height / 4; // Adjust this value for the desired side view
let camZ = (height/2) / tan(PI / 7); // Adjust this value for the desired zoom
camera(camX, camY, camZ, 0, 0, 0, 0, 1, 0);
}
function draw() {
background(0);
translate(-width / 2, -height / 1.4, 0);
terrain.update();
terrain.display();
flying -= 0.05;
}
class Terrain {
constructor(cols, rows) {
this.cols = cols;
this.rows = rows;
this.w = w;
this.terrain = [];
}
update() {
let yoff = flying;
for (let y = 0; y < this.rows; y++) {
let xoff = 0;
this.terrain[y] = [];
for (let x = 0; x < this.cols; x++) {
this.terrain[y][x] = map(noise(xoff, yoff), 0, 1, -100, 100);
xoff += 0.2;
}
yoff += 0.2;
}
}
display() {
stroke(255);
noFill();
translate(width / 2.2, height / 2);
rotateX(PI / 3);
for (let y = 0; y < this.rows - 1; y++) {
beginShape(TRIANGLE_STRIP);
for (let x = 0; x < this.cols; x++) {
vertex(x * this.w, y * this.w, this.terrain[y][x]);
vertex(x * this.w, (y + 1) * this.w, this.terrain[y + 1][x]);
}
endShape();
}
}
}