terrain-generator

digital terrain generation and animation
git clone https://tongong.net/git/terrain-generator.git
Log | Files | Refs | README

commit 34b827052a3444884b309cd935a19cab77d7d18c
parent d7fa0453f6137a677400a66ba8e689a0c1bcb6bc
Author: tongong <tongong@gmx.net>
Date:   Thu,  8 Aug 2019 20:32:38 -0400

added side walls

Diffstat:
Mscripts/main.js | 1+
Mscripts/render.js | 29++++++++++++++++++++++++-----
Mscripts/terrain.js | 18+++++++++++++++---
3 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/scripts/main.js b/scripts/main.js @@ -89,4 +89,5 @@ function draw() { } break; } + //drawWall(3, [{x:-100,y:100,z:100},{x:100,y:-100,z:100}],getRotation()*TWO_PI); } \ No newline at end of file diff --git a/scripts/render.js b/scripts/render.js @@ -1,4 +1,5 @@ -function render(points, polygons, rotation) { +function render(points, polygons, modes, rotation) { + //Modes: 0: polygon, 1-4: wall rotation *= TWO_PI; // extract points @@ -13,7 +14,7 @@ function render(points, polygons, rotation) { // sort the polygons let rot = createVector(round(cos(rotation) * 1000) / 1000, round(-sin(rotation) * 1000) / 1000); - polygons.forEach(polygon => { + polygons.forEach((polygon,index) => { let sum = createVector(0, 0); polygon.forEach(corner => { sum.x += corner.x; @@ -25,6 +26,7 @@ function render(points, polygons, rotation) { sum.z /= polygon.length; polygon.average = Object.assign({}, sum); polygon.depth = -polygon.average.x * rot.y - polygon.average.y * rot.x; + polygon.mode = modes[index]; }); polygons.sort(function(a, b) { @@ -36,7 +38,11 @@ function render(points, polygons, rotation) { // draw to the screen polygons.forEach(polygon => { + if (polygon.mode!==0) { + drawWall(polygon.mode, polygon, rotation); + } else { drawPolygon(polygon, rotation); + } }); } @@ -66,19 +72,32 @@ function drawWall(side, points, rotation) { vertex(CENTER.x + points[0].x, 0); break; case 2: - vertex(WIDTH, CENTER.y - item.y - ANGLE+SIZE/2); + vertex(WIDTH, CENTER.y - points[0].y - ANGLE+SIZE/2); break; case 3: vertex(CENTER.x + points[0].x, HEIGHT); break; case 4: - vertex(0, CENTER.y - item.y - ANGLE+SIZE/2); + vertex(0, CENTER.y - points[0].y - ANGLE+SIZE/2); break; } points.forEach(function(item) { vertex(CENTER.x + item.x, CENTER.y - item.y - ANGLE+SIZE/2); }); - vertex(CENTER.x + points[0].x, CENTER.y - points[0].y - ANGLE+SIZE/2); +switch(side) { + case 1: + vertex(CENTER.x + points[points.length-1].x, 0); + break; + case 2: + vertex(WIDTH, CENTER.y - points[points.length-1].y - ANGLE+SIZE/2); + break; + case 3: + vertex(CENTER.x + points[points.length-1].x, HEIGHT); + break; + case 4: + vertex(0, CENTER.y - points[points.length-1].y - ANGLE+SIZE/2); + break; + } endShape(); } diff --git a/scripts/terrain.js b/scripts/terrain.js @@ -89,19 +89,31 @@ class Terrain { //Terrain triangles let polygons = []; + let modes = []; for (let i = 0; i < length-1; i++) { for (let j = 0; j < length-1; j++) { if (j-i+1<this.dimension && i-j<this.dimension) { + modes.push(0,0); polygons.push([i*length+j, i*length+j+1,(i+1)*length+j+1]); polygons.push([j*length+i, (j+1)*length+i,(j+1)*length+i+1]); } } } - //Borders - //polygons.push([0,1,2,length*length,length*length+1]); + //Walls + let walls = [[],[],[],[],[],[]]; + for (let i = 0; i < this.dimension; i++) { + walls[0].push(i); + walls[1].push(i*length); + walls[2].push(this.dimension+i*(length+1)-1); + walls[3].push((this.dimension-1)*length+i*(length+1)); + walls[4].push(length*length-i-1); + walls[5].push(length*(length-i)-1); + } + polygons.push(walls[0],walls[1],walls[2],walls[3],walls[4],walls[5]); + modes.push(3,3,3,3,3,3); - render(points,polygons,rotation); + render(points,polygons,modes,rotation); } }