From 99ee6bffdb49c6ae23f5657c00b9963b3e92abaa Mon Sep 17 00:00:00 2001 From: Jaiden Grimminck <78890549+JaidenAGrimminck@users.noreply.github.com> Date: Thu, 17 Oct 2024 21:33:35 +0200 Subject: [PATCH] added a sky! --- art/3D-Raycasting-Jaiden/index.js | 88 ++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/art/3D-Raycasting-Jaiden/index.js b/art/3D-Raycasting-Jaiden/index.js index 1adf67c79..da523d619 100644 --- a/art/3D-Raycasting-Jaiden/index.js +++ b/art/3D-Raycasting-Jaiden/index.js @@ -82,6 +82,17 @@ const containLinesWithinShading = true; //fov for display. 60 is a good value const fov = 60; +//enables a sky. +const enableSky = true; + +//runs this through a spacing function, floor(e^{(-1/x) * y + 3}). this is the x in the function. (increase = increase in distance between lines) +const skyLineSpacing = 150; + +//if you want to make sure it doesn't make too thin spacing (make it too thick) +//acts as a minimum spacing for the sky +//set this to 0 to disable. +const minSkySpacing = 1; + /* end parameters */ //if in local enviornment, set to true. If in the blot editor, set to false. @@ -305,6 +316,8 @@ translateEnviornment(); let farthestDistance = 0; let closestDistance = 1000; +let topLinesHeight = {}; + function fillFinalLines() { const offset = displayMap ? 100 : 0; @@ -336,6 +349,16 @@ function fillFinalLines() { if (wallHeight < closestDistance) { closestDistance = wallHeight; } + + lineHeight = 400 - Math.floor(lineHeight); + + if (!(lineHeight in topLinesHeight)) { + topLinesHeight[lineHeight] = []; + } + + for (let x = width * (i / fov); x < width * ((i + 1) / fov); x++) { + topLinesHeight[lineHeight].push(Math.floor(x)); + } } let signChanges = []; @@ -346,7 +369,6 @@ function fillFinalLines() { for (let i = 1; i < heights.length; i++) { if (Math.abs(heights[i] - heights[i - 1]) > maxJumpForShift) { - let lowerHeight = Math.min(heights[i], heights[i - 1]); let higherHeight = Math.max(heights[i], heights[i - 1]); @@ -435,6 +457,66 @@ function fillFinalLines() { ]]); } +function drawSky() { + //sort top line heights + for (let key of Object.keys(topLinesHeight)) { + topLinesHeight[key].sort((a, b) => a - b); + } + + let dAdd = (y) => { + let n = Math.exp((-1/skyLineSpacing) * y + 3); + + return n < minSkySpacing ? minSkySpacing : n; + } + + let findFirstRecordingOfX = (x) => { + for (let key of Object.keys(topLinesHeight)) { + if (topLinesHeight[key].includes(x)) { + return key; + } + } + + return -1; + } + + for (let y = 0; y < 400; y += dAdd(y)) { + let frx = findFirstRecordingOfX(1); + + let inWall = frx != -1 && frx < y; + let x = 0; + let lastX = 0; + + for (; x <= width; x++) { + frx = findFirstRecordingOfX(x); + + if (frx != -1 && frx <= y) { + if (!inWall) { + inWall = true; + + if (lastX != x) { + finalLines.push([ + [lastX, y], + [x, y] + ]) + } + } + } else { + if (inWall) { + inWall = false; + lastX = x; + } + } + } + + if (!inWall) { + finalLines.push([ + [lastX, y], + [width, y] + ]) + } + } +} + const font = { "A": [ [ @@ -940,6 +1022,10 @@ function drawMap() { fillFinalLines(); +if (enableSky) { + drawSky(); +} + if (displayMap) { drawMap(); }