terrain-generator

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

commit ee79479acef5297a790b12490039dea1e4952d8b
parent fbb2f8fab250ca522ff9fe1a98c6710d97ba7c85
Author: tongong <tongong@gmx.net>
Date:   Mon, 12 Aug 2019 20:38:54 +0200

render fixes

Diffstat:
Mscripts/main.js | 11++++++-----
Mscripts/render.js | 109+++++++++++++++++++++++++++++++++++--------------------------------------------
2 files changed, 54 insertions(+), 66 deletions(-)

diff --git a/scripts/main.js b/scripts/main.js @@ -3,10 +3,10 @@ const WIDTH = window.innerWidth; var CENTER_HORIZON; var LEFT_HORIZON; var CENTER; -const ANGLE = 300; -const RANDOM_FACTOR = 0.4; +var ANGLE; +var RANDOM_FACTOR = 0.5; var SIZE; -const MAX_STATE = 4; +var MAX_STATE = 5; var state = 0; var statePercentage = 0; @@ -21,8 +21,9 @@ const time = () => (typeof(startMillis) == "undefined") ? 0 : millis() - startMi function setup() { CENTER = createVector(WIDTH / 2, HEIGHT / 2); CENTER_HORIZON = createVector(0, 0); - LEFT_HORIZON = createVector(-500, 0); - SIZE = ((HEIGHT > WIDTH) ? WIDTH : HEIGHT) / 2 - 50; + LEFT_HORIZON = createVector(-CENTER.x, 0); + SIZE = min(HEIGHT / 3, WIDTH / 2 - 50, 500); + ANGLE = SIZE * 2; createCanvas(WIDTH, HEIGHT); stroke(255); diff --git a/scripts/render.js b/scripts/render.js @@ -26,39 +26,13 @@ function render(points, polygons, modes, rotation) { sum.z /= polygon.length; polygon.average = Object.assign({}, sum); polygon.depth = depth(polygon.average, rot); - polygon.size = abs(createVector(polygon[0].x, polygon[0].y).dist(createVector(polygon[polygon.length - 1].x, polygon[polygon.length - 1].y))); polygon.mode = modes[index]; - if (polygon.depth > 0) { - polygon.depth *= polygon.size; - } else { - polygon.depth *= polygon.size; - } + polygon.size = (polygon.mode > 0) ? SIZE : 1; + polygon.depth *= polygon.size; }); polygons.sort(function(a, b) { - /*if (abs(a.depth - b.depth) <= 5) { - return abs(b.average.z) - abs(a.average.z); - }*/ return a.depth - b.depth; - /*let start1 = a.average; - let start2 = b[0]; - let end1 = createVector(a.average.x+rot.x,a.average.y+rot.y); - let end2 = b[1]; - - // Two lines f(x) = m * x + n - let m1 = (end1.y - start1.y) / ((rot.x==0)?99999:end1.x - start1.x); - let m2 = (end2.y - start2.y) / ((end2.x == start2.x)?99999:end2.x - start2.x); - let n1 = end1.y - end1.x * m1; - let n2 = end2.y - end2.x * m2; - - let crossX = (n2 - n1) / (m1 - m2); - let crossY = crossX * m1 + n1; - - if (mouseIsPressed) { - console.log(crossX,crossY); - } - return a.depth + crossX * rot.y + crossY * rot.x;*/ - }); // draw to the screen @@ -90,44 +64,57 @@ function drawPolygon(points, rotation) { } function drawWall(side, points, rotation) { - points.forEach(function(item) { + let endX; + let startX; + let rot = createVector(round(cos(rotation) * 1000) / 1000, round(-sin(rotation) * 1000) / 1000); + let swap = depth(points[0], rot) > depth(points[points.length - 1], rot); + points.forEach(function(item, index) { let newVec = calc3D(item.x, item.y, item.z - ANGLE, rotation); item.x = newVec.x; item.y = newVec.y; + if (index == 0) { + startX = newVec.x; + } + if (index == points.length - 1) { + endX = newVec.x; + } }); - beginShape(); - switch (side) { - case 1: - vertex(CENTER.x + points[0].x, 0); - break; - case 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 - points[0].y - ANGLE + SIZE / 2); - break; - } - points.forEach(function(item) { - vertex(CENTER.x + item.x, CENTER.y - item.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; + if (swap)[startX, endX] = [endX, startX]; + if ((abs(endX) - abs(startX)) < 0) { + beginShape(); + switch (side) { + case 1: + vertex(CENTER.x + points[0].x, 0); + break; + case 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 - points[0].y - ANGLE + SIZE / 2); + break; + } + points.forEach(function(item) { + vertex(CENTER.x + item.x, CENTER.y - item.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(); } - endShape(); } function calc3D(x, y, z, rotation) {