diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..6d40c2c --- /dev/null +++ b/.editorconfig @@ -0,0 +1,23 @@ +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.py] +indent_style = space +indent_size = 4 + +[{*.html,*.css,*.json}] +indent_style = tab +indent_size = 4 + +[humans.txt] +indent_style = tab +indent_size = 2 + +[*.js] +indent_style = space +indent_size = 2 diff --git a/LICENSE b/LICENSE index d072510..e347384 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,22 @@ +MIT License + Copyright 2022 Paul Saikko +Copyright 2022 Alexander Popov -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. diff --git a/README.md b/README.md index 034a8f9..2a97d50 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,5 @@ -# raycast -A small raycaster graphics engine in javascript with html 5 canvas. +# JSRay +A Small Raycaster Graphics Engine in JavaScript with HTML5 ``. -Code mostly follows [this C++ raycaster tutorial](http://lodev.org/cgtutor/raycasting.html) - -Live at [github.io](http://psaikko.github.io/raycast/) - -![screenshot](https://raw.githubusercontent.com/psaikko/raycast/master/screenshot.png) +## Original author +Paul Saikko - [Github](https://github.com/psaikko/raycast) diff --git a/game.js b/game.js deleted file mode 100644 index d6f5160..0000000 --- a/game.js +++ /dev/null @@ -1,21 +0,0 @@ -var raycast = raycast || {}; - -window.requestAnimFrame = (function(){ - return window.requestAnimationFrame || - window.webkitRequestAnimationFrame || - window.mozRequestAnimationFrame || - window.oRequestAnimationFrame || - window.msRequestAnimationFrame || - function(callback, element){ - window.setTimeout(callback, 1000 / 60); - }; -})(); - -function start() { - document.onkeyup = raycast.keyhandler.onKeyup; - document.onkeydown = raycast.keyhandler.onKeydown; - var textureFiles = ["img/brick.png", "img/ground.png", "img/sky.png"]; - raycast.texture.initiateLoad(textureFiles, raycast.engine.start); -}; - -window.onload = start; \ No newline at end of file diff --git a/index.html b/index.html deleted file mode 100644 index 9881628..0000000 --- a/index.html +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - -
- -
- - - - - - - diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..70f0a51 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1 @@ +editorconfig-checker==2.4.0 diff --git a/screenshot.png b/screenshot.png deleted file mode 100644 index 790900d..0000000 Binary files a/screenshot.png and /dev/null differ diff --git a/src/game.js b/src/game.js new file mode 100644 index 0000000..4236e70 --- /dev/null +++ b/src/game.js @@ -0,0 +1,21 @@ +var raycast = raycast || {}; + +window.requestAnimFrame = (function(){ + return window.requestAnimationFrame || + window.webkitRequestAnimationFrame || + window.mozRequestAnimationFrame || + window.oRequestAnimationFrame || + window.msRequestAnimationFrame || + function(callback, element){ + window.setTimeout(callback, 1000 / 60); + }; +})(); + +function start() { + document.onkeyup = raycast.keyhandler.onKeyup; + document.onkeydown = raycast.keyhandler.onKeydown; + var textureFiles = ["img/brick.png", "img/ground.png", "img/sky.png"]; + raycast.texture.initiateLoad(textureFiles, raycast.engine.start); +}; + +window.onload = start; diff --git a/img/brick.png b/src/img/brick.png similarity index 100% rename from img/brick.png rename to src/img/brick.png diff --git a/img/ground.png b/src/img/ground.png similarity index 100% rename from img/ground.png rename to src/img/ground.png diff --git a/img/sky.png b/src/img/sky.png similarity index 100% rename from img/sky.png rename to src/img/sky.png diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..c86f326 --- /dev/null +++ b/src/index.html @@ -0,0 +1,18 @@ + + + + + + JSRay + + +
+ +
+ + + + + + + diff --git a/keyhandler.js b/src/keyhandler.js similarity index 78% rename from keyhandler.js rename to src/keyhandler.js index cb18608..cdabd80 100644 --- a/keyhandler.js +++ b/src/keyhandler.js @@ -18,19 +18,19 @@ raycast.keyhandler = (function () { var lastState = new Array(); for (var i = 0; i < 255; i++) { - state[i] = false; + state[i] = false; lastState[i] = false; } onKeyup = function (e) { state[e.which] = false; - if (isUsedKey(e.which)) + if (isUsedKey(e.which)) e.preventDefault(); } onKeydown = function (e) { state[e.which] = true; - if (isUsedKey(e.which)) + if (isUsedKey(e.which)) e.preventDefault(); } @@ -47,13 +47,13 @@ raycast.keyhandler = (function () { return state[codes[keyname]]; } - isKeypress = function(keyname) { - return state[codes[keyname]] && !lastState[codes[keyname]]; - } + isKeypress = function(keyname) { + return state[codes[keyname]] && !lastState[codes[keyname]]; + } - tick = function() { - lastState = state.slice(); - } + tick = function() { + lastState = state.slice(); + } return { onKeyup: onKeyup, @@ -62,4 +62,4 @@ raycast.keyhandler = (function () { isKeypress: isKeypress, tick: tick }; -})(); \ No newline at end of file +})(); diff --git a/raycast.js b/src/raycast.js similarity index 91% rename from raycast.js rename to src/raycast.js index 0b637f8..a6cb53c 100644 --- a/raycast.js +++ b/src/raycast.js @@ -1,349 +1,349 @@ -// http://lodev.org/cgtutor/raycasting.html -var raycast = raycast || {}; - -raycast.engine = (function () { - var canvas = document.getElementById("viewport"); - var g = canvas.getContext("2d"); - var filtering = false; - var mapWidth = 24, - mapHeight = 24, - texHeight = 64, - texWidth = 64; - - var texture; - function initTexture() { - texture = raycast.texture.getTextures(); - console.log(texture); - texture.push([]); - for(var x = 0; x < texWidth; x++) { - for(var y = 0; y < texHeight; y++) { - var xorcolor = (x * 256 / texWidth) ^ (y * 256 / texHeight); - var d = Math.sqrt((texWidth/2 - x)*(texWidth/2 - x) + (texHeight/2 - y)*(texHeight/2 - y)); - var sincolor = 256 * (1 + Math.sin(d/2)) / 2; - texture[3][texWidth * y + x] = [xorcolor, 0, sincolor]; // blue sine pattern - } - } - } - /* - texture[0] = raycast.texture.load("ground"); - texture[1] = raycast.texture.load("brick"); - texture[2] = raycast.texture.load("sky"); - */ - - var worldMap = [ - [1,1,1,1,1,1,1,1,1,1], - [1,0,0,0,0,0,0,0,0,1], - [1,0,0,0,0,0,0,0,0,1], - [1,0,0,1,0,0,1,0,0,1], - [1,0,0,0,0,0,0,0,0,1], - [1,0,0,0,0,0,0,0,0,1], - [1,0,0,1,0,0,1,0,0,1], - [1,0,0,0,0,0,0,0,0,1], - [1,0,0,0,0,0,0,0,0,1], - [1,1,1,1,1,1,1,1,1,1] - ]; - - var posX = 2, posY = 2, - dirX = -1, dirY = 0, - planeX = 0, planeY = 0.66, - time = Date.now(), oldTime = Date.now(); - - var moveSpeed, rotSpeed; - - var w = canvas.width, - h = canvas.height; - - function verLine(arr, x, yStart, yEnd, color){ - //console.log(x, yStart, yEnd); - for (var y = yStart | 0; y < yEnd | 0; y++) { - var i = 4 * (w * y) + 4 * x; - arr[i + 0] = color[0]; - arr[i + 1] = color[1]; - arr[i + 2] = color[2]; - arr[i + 3] = 255; - } - }; - - imagedata = g.getImageData(0,0,w,h); - var buffer = imagedata.data; - - var keys = raycast.keyhandler; - - function input() { - if (keys.isKeydown("up")) { - if(worldMap[(posX + dirX * moveSpeed) | 0][posY | 0] == 0) posX += dirX * moveSpeed; - if(worldMap[posX | 0][(posY + dirY * moveSpeed) | 0] == 0) posY += dirY * moveSpeed; - } - //move backwards if no wall behind you - if (keys.isKeydown("down")) { - if(worldMap[(posX - dirX * moveSpeed) | 0][posY | 0] == 0) posX -= dirX * moveSpeed; - if(worldMap[posX | 0][(posY - dirY * moveSpeed) | 0] == 0) posY -= dirY * moveSpeed; - } - if (keys.isKeydown("right")) { - //both camera direction and camera plane must be rotated - var oldDirX = dirX; - dirX = dirX * Math.cos(-rotSpeed) - dirY * Math.sin(-rotSpeed); - dirY = oldDirX * Math.sin(-rotSpeed) + dirY * Math.cos(-rotSpeed); - var oldPlaneX = planeX; - planeX = planeX * Math.cos(-rotSpeed) - planeY * Math.sin(-rotSpeed); - planeY = oldPlaneX * Math.sin(-rotSpeed) + planeY * Math.cos(-rotSpeed); - } - if (keys.isKeydown("left")) { - //both camera direction and camera plane must be rotated - var oldDirX = dirX; - dirX = dirX * Math.cos(rotSpeed) - dirY * Math.sin(rotSpeed); - dirY = oldDirX * Math.sin(rotSpeed) + dirY * Math.cos(rotSpeed); - var oldPlaneX = planeX; - planeX = planeX * Math.cos(rotSpeed) - planeY * Math.sin(rotSpeed); - planeY = oldPlaneX * Math.sin(rotSpeed) + planeY * Math.cos(rotSpeed); - } - if (keys.isKeypress("d")) - filtering = !filtering; - } - - function draw() { - for(var x = 0; x < w; x++) { - var cameraX = 2 * x / w - 1, - rayPosX = posX, - rayPosY = posY, - rayDirX = dirX + planeX * cameraX, - rayDirY = dirY + planeY * cameraX; - - var mapX = rayPosX | 0, - mapY = rayPosY | 0; - - var deltaDistX = Math.sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX)), - deltaDistY = Math.sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY)); - - var stepX, - stepY, - sideDistX, - sideDistY; - - if (rayDirX < 0) { - stepX = -1; - sideDistX = (rayPosX - mapX) * deltaDistX; - } else { - stepX = 1; - sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX; - } - if (rayDirY < 0) { - stepY = -1; - sideDistY = (rayPosY - mapY) * deltaDistY; - } else { - stepY = 1; - sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY; - } - - var side, hit = 0; - - // DDA - while (hit == 0) { - side = sideDistX > sideDistY; - if (side == 0) { - sideDistX += deltaDistX; - mapX += stepX; - } else { - sideDistY += deltaDistY; - mapY += stepY; - } - if (worldMap[mapX][mapY] > 0) { - hit = 1; - } - } - - var perpWallDist; - if (side == 0) - perpWallDist = Math.abs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX); - else - perpWallDist = Math.abs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY); - - //Calculate height of line to draw on screen - var lineHeight = Math.abs((h / perpWallDist) | 0); - - //calculate lowest and highest pixel to fill in current stripe - var drawStart = ((h - lineHeight) / 2) | 0; - if(drawStart < 0) - drawStart = 0; - var drawEnd = ((h + lineHeight) / 2) | 0; - if(drawEnd >= h) - drawEnd = h - 1; - - var wallX; // the exact value where the wall was hit - if (side == 1) - wallX = rayPosX + ((mapY - rayPosY + (1 - stepY) / 2) / rayDirY) * rayDirX; - else - wallX = rayPosY + ((mapX - rayPosX + (1 - stepX) / 2) / rayDirX) * rayDirY; - wallX -= wallX | 0; - - var texX = (wallX * texWidth)/* | 0*/; - if(side == 0 && rayDirX > 0) - texX = texWidth - texX - 1; - if(side == 1 && rayDirY < 0) - texX = texWidth - texX - 1; - - var shade = (side == 1 ? 0.6: 1); - - var wallTex = texture[worldMap[mapX][mapY] - 1]; - - for (var y = drawStart; y < drawEnd; y++) { - var d = (y * 256 - h * 128 + lineHeight * 128) | 0; - var texY = ((d * texHeight) / (lineHeight * 256))/* | 0*/; - if (texY < 0) texY = 0; - - var color; - if (filtering) { - var ty1 = texY | 0; - var ty2 = (ty1 + 1) % texHeight; - var tx1 = texX | 0; - var tx2 = (tx1 + 1) % texWidth; - var xf = texX - (texX | 0); - var yf = texY - (texY | 0); - - color = [0,0,0]; - var c1 = wallTex[texWidth * ty1 + tx1]; - var c2 = wallTex[texWidth * ty1 + tx2]; - var c3 = wallTex[texWidth * ty2 + tx1]; - var c4 = wallTex[texWidth * ty2 + tx2]; - - color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0; - color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0; - color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0; - } else { - texX |= 0; - texY |= 0; - color = wallTex[texHeight * texY + texX]; - } - var i = 4 * (w * y) + 4 * x; - - buffer[i + 0] = color[0] * shade; - buffer[i + 1] = color[1] * shade; - buffer[i + 2] = color[2] * shade; - buffer[i + 3] = 255; - } - - var floorXWall, floorYWall; //x, y position of the floor texel at the bottom of the wall - //4 different wall directions possible - if (side == 0 && rayDirX > 0) { - floorXWall = mapX; - floorYWall = mapY + wallX; - } else if (side == 0 && rayDirX < 0) { - floorXWall = mapX + 1.0; - floorYWall = mapY + wallX; - } else if (side == 1 && rayDirY > 0) { - floorXWall = mapX + wallX; - floorYWall = mapY; - } else /* side == 1 && rayDirY > 0*/{ - floorXWall = mapX + wallX; - floorYWall = mapY + 1.0; - } - - var currentDist; - var distWall = perpWallDist; - var distPlayer = 0.0; - - if (drawEnd < 0) drawEnd = h; //becomes < 0 when the integer overflows - - var ceilTex = texture[2]; - var floorTex = texture[1]; - - //draw the floor from drawEnd to the bottom of the screen - for(var y = drawEnd; y < h; y++) - { - currentDist = h / (2.0 * y - h); //you could make a small lookup table for this instead - - var weight = (currentDist - distPlayer) / (distWall - distPlayer); - var nextWeight = (h / (2.0 * (y+1) - h)) / distWall; - - var currentFloorX = weight * floorXWall + (1.0 - weight) * posX; - var currentFloorY = weight * floorYWall + (1.0 - weight) * posY; - - var floorTexX = (currentFloorX * texWidth) % texWidth; - var floorTexY = (currentFloorY * texHeight) % texHeight; - - if (floorTexX < 0) floorTexX = 0; - if (floorTexY < 0) floorTexY = 0; - - var color; - if (filtering) { - ty1 = floorTexY | 0; - ty2 = (ty1 + 1) % texHeight; - tx1 = floorTexX | 0; - tx2 = (tx1 + 1) % texWidth; - xf = floorTexX - (floorTexX | 0); - yf = floorTexY - (floorTexY | 0); - - color = [0,0,0]; - c1 = floorTex[texWidth * ty1 + tx1]; - c2 = floorTex[texWidth * ty1 + tx2]; - c3 = floorTex[texWidth * ty2 + tx1]; - c4 = floorTex[texWidth * ty2 + tx2]; - - color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0; - color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0; - color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0; - } else { - color = floorTex[texWidth * (floorTexY|0) + (floorTexX|0)]; - } - - //floor - i = 4 * (w * y) + 4 * x; - - buffer[i+0] = (color[0])/2; - buffer[i+1] = (color[1])/2; - buffer[i+2] = (color[2])/2; - buffer[i+3] = 255; - - if (filtering) { - color = [0,0,0]; - c1 = ceilTex[texWidth * ty1 + tx1]; - c2 = ceilTex[texWidth * ty1 + tx2]; - c3 = ceilTex[texWidth * ty2 + tx1]; - c4 = ceilTex[texWidth * ty2 + tx2]; - - color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0; - color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0; - color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0; - } else { - color = ceilTex[texWidth * (floorTexY|0) + (floorTexX|0)]; - } - - //ceiling (symmetrical!) - i = 4 * (w * (h - y - 1)) + 4 * x; - - buffer[i+0] = color[0]/2; - buffer[i+1] = color[1]/2; - buffer[i+2] = color[2]/2; - buffer[i+3] = 255; - } - } - - oldTime = time; - time = Date.now() - var frameTime = (time - oldTime) / 1000.0; //frameTime is the time this frame has taken, in seconds - - //speed modifiers - moveSpeed = frameTime * 5.0; //the constant value is in squares/second - rotSpeed = frameTime * 3.0; //the constant value is in radians/second - - g.putImageData(imagedata, 0, 0); - g.font = "bold 30pt Monospace"; - g.fillText(""+((1000 / (time - oldTime))|0), 0, 30); - }; - - function tick() { - draw(); - input(); - keys.tick(); - - window.requestAnimFrame(tick); - //window.setTimeout(tick, 1); - }; - - function start() { - initTexture(); - tick(); - } - - return {start: start} -}()); \ No newline at end of file +// http://lodev.org/cgtutor/raycasting.html +var raycast = raycast || {}; + +raycast.engine = (function () { + var canvas = document.getElementById("viewport"); + var g = canvas.getContext("2d"); + var filtering = false; + var mapWidth = 24, + mapHeight = 24, + texHeight = 64, + texWidth = 64; + + var texture; + function initTexture() { + texture = raycast.texture.getTextures(); + console.log(texture); + texture.push([]); + for(var x = 0; x < texWidth; x++) { + for(var y = 0; y < texHeight; y++) { + var xorcolor = (x * 256 / texWidth) ^ (y * 256 / texHeight); + var d = Math.sqrt((texWidth/2 - x)*(texWidth/2 - x) + (texHeight/2 - y)*(texHeight/2 - y)); + var sincolor = 256 * (1 + Math.sin(d/2)) / 2; + texture[3][texWidth * y + x] = [xorcolor, 0, sincolor]; // blue sine pattern + } + } + } + /* + texture[0] = raycast.texture.load("ground"); + texture[1] = raycast.texture.load("brick"); + texture[2] = raycast.texture.load("sky"); + */ + + var worldMap = [ + [1,1,1,1,1,1,1,1,1,1], + [1,0,0,0,0,0,0,0,0,1], + [1,0,0,0,0,0,0,0,0,1], + [1,0,0,1,0,0,1,0,0,1], + [1,0,0,0,0,0,0,0,0,1], + [1,0,0,0,0,0,0,0,0,1], + [1,0,0,1,0,0,1,0,0,1], + [1,0,0,0,0,0,0,0,0,1], + [1,0,0,0,0,0,0,0,0,1], + [1,1,1,1,1,1,1,1,1,1] + ]; + + var posX = 2, posY = 2, + dirX = -1, dirY = 0, + planeX = 0, planeY = 0.66, + time = Date.now(), oldTime = Date.now(); + + var moveSpeed, rotSpeed; + + var w = canvas.width, + h = canvas.height; + + function verLine(arr, x, yStart, yEnd, color){ + //console.log(x, yStart, yEnd); + for (var y = yStart | 0; y < yEnd | 0; y++) { + var i = 4 * (w * y) + 4 * x; + arr[i + 0] = color[0]; + arr[i + 1] = color[1]; + arr[i + 2] = color[2]; + arr[i + 3] = 255; + } + }; + + imagedata = g.getImageData(0,0,w,h); + var buffer = imagedata.data; + + var keys = raycast.keyhandler; + + function input() { + if (keys.isKeydown("up")) { + if(worldMap[(posX + dirX * moveSpeed) | 0][posY | 0] == 0) posX += dirX * moveSpeed; + if(worldMap[posX | 0][(posY + dirY * moveSpeed) | 0] == 0) posY += dirY * moveSpeed; + } + //move backwards if no wall behind you + if (keys.isKeydown("down")) { + if(worldMap[(posX - dirX * moveSpeed) | 0][posY | 0] == 0) posX -= dirX * moveSpeed; + if(worldMap[posX | 0][(posY - dirY * moveSpeed) | 0] == 0) posY -= dirY * moveSpeed; + } + if (keys.isKeydown("right")) { + //both camera direction and camera plane must be rotated + var oldDirX = dirX; + dirX = dirX * Math.cos(-rotSpeed) - dirY * Math.sin(-rotSpeed); + dirY = oldDirX * Math.sin(-rotSpeed) + dirY * Math.cos(-rotSpeed); + var oldPlaneX = planeX; + planeX = planeX * Math.cos(-rotSpeed) - planeY * Math.sin(-rotSpeed); + planeY = oldPlaneX * Math.sin(-rotSpeed) + planeY * Math.cos(-rotSpeed); + } + if (keys.isKeydown("left")) { + //both camera direction and camera plane must be rotated + var oldDirX = dirX; + dirX = dirX * Math.cos(rotSpeed) - dirY * Math.sin(rotSpeed); + dirY = oldDirX * Math.sin(rotSpeed) + dirY * Math.cos(rotSpeed); + var oldPlaneX = planeX; + planeX = planeX * Math.cos(rotSpeed) - planeY * Math.sin(rotSpeed); + planeY = oldPlaneX * Math.sin(rotSpeed) + planeY * Math.cos(rotSpeed); + } + if (keys.isKeypress("d")) + filtering = !filtering; + } + + function draw() { + for(var x = 0; x < w; x++) { + var cameraX = 2 * x / w - 1, + rayPosX = posX, + rayPosY = posY, + rayDirX = dirX + planeX * cameraX, + rayDirY = dirY + planeY * cameraX; + + var mapX = rayPosX | 0, + mapY = rayPosY | 0; + + var deltaDistX = Math.sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX)), + deltaDistY = Math.sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY)); + + var stepX, + stepY, + sideDistX, + sideDistY; + + if (rayDirX < 0) { + stepX = -1; + sideDistX = (rayPosX - mapX) * deltaDistX; + } else { + stepX = 1; + sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX; + } + if (rayDirY < 0) { + stepY = -1; + sideDistY = (rayPosY - mapY) * deltaDistY; + } else { + stepY = 1; + sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY; + } + + var side, hit = 0; + + // DDA + while (hit == 0) { + side = sideDistX > sideDistY; + if (side == 0) { + sideDistX += deltaDistX; + mapX += stepX; + } else { + sideDistY += deltaDistY; + mapY += stepY; + } + if (worldMap[mapX][mapY] > 0) { + hit = 1; + } + } + + var perpWallDist; + if (side == 0) + perpWallDist = Math.abs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX); + else + perpWallDist = Math.abs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY); + + //Calculate height of line to draw on screen + var lineHeight = Math.abs((h / perpWallDist) | 0); + + //calculate lowest and highest pixel to fill in current stripe + var drawStart = ((h - lineHeight) / 2) | 0; + if(drawStart < 0) + drawStart = 0; + var drawEnd = ((h + lineHeight) / 2) | 0; + if(drawEnd >= h) + drawEnd = h - 1; + + var wallX; // the exact value where the wall was hit + if (side == 1) + wallX = rayPosX + ((mapY - rayPosY + (1 - stepY) / 2) / rayDirY) * rayDirX; + else + wallX = rayPosY + ((mapX - rayPosX + (1 - stepX) / 2) / rayDirX) * rayDirY; + wallX -= wallX | 0; + + var texX = (wallX * texWidth)/* | 0*/; + if(side == 0 && rayDirX > 0) + texX = texWidth - texX - 1; + if(side == 1 && rayDirY < 0) + texX = texWidth - texX - 1; + + var shade = (side == 1 ? 0.6: 1); + + var wallTex = texture[worldMap[mapX][mapY] - 1]; + + for (var y = drawStart; y < drawEnd; y++) { + var d = (y * 256 - h * 128 + lineHeight * 128) | 0; + var texY = ((d * texHeight) / (lineHeight * 256))/* | 0*/; + if (texY < 0) texY = 0; + + var color; + if (filtering) { + var ty1 = texY | 0; + var ty2 = (ty1 + 1) % texHeight; + var tx1 = texX | 0; + var tx2 = (tx1 + 1) % texWidth; + var xf = texX - (texX | 0); + var yf = texY - (texY | 0); + + color = [0,0,0]; + var c1 = wallTex[texWidth * ty1 + tx1]; + var c2 = wallTex[texWidth * ty1 + tx2]; + var c3 = wallTex[texWidth * ty2 + tx1]; + var c4 = wallTex[texWidth * ty2 + tx2]; + + color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0; + color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0; + color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0; + } else { + texX |= 0; + texY |= 0; + color = wallTex[texHeight * texY + texX]; + } + var i = 4 * (w * y) + 4 * x; + + buffer[i + 0] = color[0] * shade; + buffer[i + 1] = color[1] * shade; + buffer[i + 2] = color[2] * shade; + buffer[i + 3] = 255; + } + + var floorXWall, floorYWall; //x, y position of the floor texel at the bottom of the wall + //4 different wall directions possible + if (side == 0 && rayDirX > 0) { + floorXWall = mapX; + floorYWall = mapY + wallX; + } else if (side == 0 && rayDirX < 0) { + floorXWall = mapX + 1.0; + floorYWall = mapY + wallX; + } else if (side == 1 && rayDirY > 0) { + floorXWall = mapX + wallX; + floorYWall = mapY; + } else /* side == 1 && rayDirY > 0*/{ + floorXWall = mapX + wallX; + floorYWall = mapY + 1.0; + } + + var currentDist; + var distWall = perpWallDist; + var distPlayer = 0.0; + + if (drawEnd < 0) drawEnd = h; //becomes < 0 when the integer overflows + + var ceilTex = texture[2]; + var floorTex = texture[1]; + + //draw the floor from drawEnd to the bottom of the screen + for(var y = drawEnd; y < h; y++) + { + currentDist = h / (2.0 * y - h); //you could make a small lookup table for this instead + + var weight = (currentDist - distPlayer) / (distWall - distPlayer); + var nextWeight = (h / (2.0 * (y+1) - h)) / distWall; + + var currentFloorX = weight * floorXWall + (1.0 - weight) * posX; + var currentFloorY = weight * floorYWall + (1.0 - weight) * posY; + + var floorTexX = (currentFloorX * texWidth) % texWidth; + var floorTexY = (currentFloorY * texHeight) % texHeight; + + if (floorTexX < 0) floorTexX = 0; + if (floorTexY < 0) floorTexY = 0; + + var color; + if (filtering) { + ty1 = floorTexY | 0; + ty2 = (ty1 + 1) % texHeight; + tx1 = floorTexX | 0; + tx2 = (tx1 + 1) % texWidth; + xf = floorTexX - (floorTexX | 0); + yf = floorTexY - (floorTexY | 0); + + color = [0,0,0]; + c1 = floorTex[texWidth * ty1 + tx1]; + c2 = floorTex[texWidth * ty1 + tx2]; + c3 = floorTex[texWidth * ty2 + tx1]; + c4 = floorTex[texWidth * ty2 + tx2]; + + color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0; + color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0; + color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0; + } else { + color = floorTex[texWidth * (floorTexY|0) + (floorTexX|0)]; + } + + //floor + i = 4 * (w * y) + 4 * x; + + buffer[i+0] = (color[0])/2; + buffer[i+1] = (color[1])/2; + buffer[i+2] = (color[2])/2; + buffer[i+3] = 255; + + if (filtering) { + color = [0,0,0]; + c1 = ceilTex[texWidth * ty1 + tx1]; + c2 = ceilTex[texWidth * ty1 + tx2]; + c3 = ceilTex[texWidth * ty2 + tx1]; + c4 = ceilTex[texWidth * ty2 + tx2]; + + color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0; + color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0; + color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0; + } else { + color = ceilTex[texWidth * (floorTexY|0) + (floorTexX|0)]; + } + + //ceiling (symmetrical!) + i = 4 * (w * (h - y - 1)) + 4 * x; + + buffer[i+0] = color[0]/2; + buffer[i+1] = color[1]/2; + buffer[i+2] = color[2]/2; + buffer[i+3] = 255; + } + } + + oldTime = time; + time = Date.now() + var frameTime = (time - oldTime) / 1000.0; //frameTime is the time this frame has taken, in seconds + + //speed modifiers + moveSpeed = frameTime * 5.0; //the constant value is in squares/second + rotSpeed = frameTime * 3.0; //the constant value is in radians/second + + g.putImageData(imagedata, 0, 0); + g.font = "bold 30pt Monospace"; + g.fillText(""+((1000 / (time - oldTime))|0), 0, 30); + }; + + function tick() { + draw(); + input(); + keys.tick(); + + window.requestAnimFrame(tick); + //window.setTimeout(tick, 1); + }; + + function start() { + initTexture(); + tick(); + } + + return {start: start} +}()); diff --git a/texture.js b/src/texture.js similarity index 90% rename from texture.js rename to src/texture.js index cc4cc60..6f6e88d 100644 --- a/texture.js +++ b/src/texture.js @@ -1,63 +1,63 @@ -var raycast = raycast || {}; - -raycast.texture = (function () { - function load(id) { - var canvas = document.createElement('canvas'); - var image = document.getElementById(id); - canvas.width = image.width; - canvas.height = image.height; - var ctx = canvas.getContext('2d'); - ctx.drawImage(image, 0, 0); - var imagedata = ctx.getImageData(0, 0, image.width, image.height); - var rgbArray = new Array(image.width * image.height); - for( var i = 0; i < image.width * image.height; i++) { - rgbArray[i] = [imagedata.data[4*i], imagedata.data[4*i+1], imagedata.data[4*i+2]]; - } - image.width = 0; - image.height = 0; - return rgbArray; - } - - var images = []; - - function initiateLoad(textures, onSuccess) { - var n = textures.length; - var counter = 0; - - var callback = function() { - counter++; - console.log(counter+" of "+n+" textures received") - if (counter == n) onSuccess(); - } - - for (var i = 0; i < n; i++) { - var image = new Image() - image.onload = callback; - image.src = textures[i]; - images.push(image); - } - } - - function getTextures() { - var n = images.length; - var textures = []; - for (var i = 0; i < n; i++) { - var canvas = document.createElement('canvas'); - var image = images[i]; - canvas.width = image.width; - canvas.height = image.height; - var ctx = canvas.getContext('2d'); - ctx.drawImage(image, 0, 0); - var imagedata = ctx.getImageData(0, 0, image.width, image.height); - var rgbArray = new Array(image.width * image.height); - for(var j = 0; j < image.width * image.height; j++) { - rgbArray[j] = [imagedata.data[4*j], imagedata.data[4*j+1], imagedata.data[4*j+2]]; - } - textures.push(rgbArray); - } - - return textures; - } - - return {load: load, initiateLoad: initiateLoad, getTextures: getTextures}; -}()); \ No newline at end of file +var raycast = raycast || {}; + +raycast.texture = (function () { + function load(id) { + var canvas = document.createElement('canvas'); + var image = document.getElementById(id); + canvas.width = image.width; + canvas.height = image.height; + var ctx = canvas.getContext('2d'); + ctx.drawImage(image, 0, 0); + var imagedata = ctx.getImageData(0, 0, image.width, image.height); + var rgbArray = new Array(image.width * image.height); + for( var i = 0; i < image.width * image.height; i++) { + rgbArray[i] = [imagedata.data[4*i], imagedata.data[4*i+1], imagedata.data[4*i+2]]; + } + image.width = 0; + image.height = 0; + return rgbArray; + } + + var images = []; + + function initiateLoad(textures, onSuccess) { + var n = textures.length; + var counter = 0; + + var callback = function() { + counter++; + console.log(counter+" of "+n+" textures received") + if (counter == n) onSuccess(); + } + + for (var i = 0; i < n; i++) { + var image = new Image() + image.onload = callback; + image.src = textures[i]; + images.push(image); + } + } + + function getTextures() { + var n = images.length; + var textures = []; + for (var i = 0; i < n; i++) { + var canvas = document.createElement('canvas'); + var image = images[i]; + canvas.width = image.width; + canvas.height = image.height; + var ctx = canvas.getContext('2d'); + ctx.drawImage(image, 0, 0); + var imagedata = ctx.getImageData(0, 0, image.width, image.height); + var rgbArray = new Array(image.width * image.height); + for(var j = 0; j < image.width * image.height; j++) { + rgbArray[j] = [imagedata.data[4*j], imagedata.data[4*j+1], imagedata.data[4*j+2]]; + } + textures.push(rgbArray); + } + + return textures; + } + + return {load: load, initiateLoad: initiateLoad, getTextures: getTextures}; +}());