/* * Start File: * /workspace/raytracer-work/src/textureColumn.js */ /** * Texture-column helpers for Wolfenstein-style wall strips. * Each screen column samples exactly one source pixel column. */ function textureColumnIndex (tileX, texWidth) { if (!(texWidth > 0)) { return 0; } var sx = Math.floor(Number(tileX) * texWidth); if (sx < 0) { sx = 0; } if (sx > texWidth - 1) { sx = texWidth - 1; } return sx; } function textureColumnUv (tileX, texWidth) { var sx = textureColumnIndex(tileX, texWidth); var w = texWidth > 0 ? texWidth : 1; return { sx: sx, sw: 1, u0: sx / w, u1: (sx + 1) / w, v0: 0, v1: 1 }; } /** Face U from hit position within a cell (no +1 bias). */ function faceTileX (hitCoord, cellSize, face) { var local = hitCoord % cellSize; if (local < 0) { local += cellSize; } var t = local / cellSize; switch (face) { case "north": return t; case "south": return 1 - t; case "east": return 1 - t; case "west": return t; default: return t; } } if (typeof module !== "undefined" && module.exports) { module.exports = { textureColumnIndex: textureColumnIndex, textureColumnUv: textureColumnUv, faceTileX: faceTileX }; } /* * End File: * /workspace/raytracer-work/src/textureColumn.js */ /* * Start File: * /workspace/raytracer-work/src/levels.js */ var Levels = [ { name: "Level 1", width: 10, height: 10, spawnX: 1, spawnY: 1, tiles: [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 3, 3, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 2, 1, 0, 1, 1, 0, 1, 0, 0, 1, 3, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ] } ]; /* * End File: * /workspace/raytracer-work/src/levels.js */ /* * Start File: * /workspace/raytracer-work/src/tiles.js */ /** Tile table; Image fields filled after preload. */ var Tiles = { Nothing: { ID: 0, Color: { R: 255, G: 255, B: 255 }, Solid: false, ImageName: null }, Stone: { ID: 1, Color: { R: 155, G: 155, B: 155 }, Solid: true, ImageName: "stone" }, Finish: { ID: 2, Color: { R: 255, G: 255, B: 0 }, Solid: false, ImageName: null }, Wood: { ID: 3, Color: { R: 168, G: 102, B: 50 }, Solid: true, ImageName: "wood" }, findByID: function (id) { for (var key in Tiles) { if (!Object.prototype.hasOwnProperty.call(Tiles, key)) { continue; } var value = Tiles[key]; if (value && value.ID === id) { return value; } } return null; }, bindImages: function (images) { for (var key in Tiles) { if (!Object.prototype.hasOwnProperty.call(Tiles, key)) { continue; } var tile = Tiles[key]; if (tile && tile.ImageName && images[tile.ImageName]) { tile.Image = images[tile.ImageName]; } } } }; /* * End File: * /workspace/raytracer-work/src/tiles.js */ /* * Start File: * /workspace/raytracer-work/src/world.js */ var World = { size: 32, level: null, loadLevel: function (index) { this.level = Levels[index]; }, spawnPoint: function () { return { x: this.level.spawnX * this.size + this.size / 2, y: this.level.spawnY * this.size + this.size / 2 }; }, rayCast: function (x, y, radian) { var maxDist = this.level.width + this.level.height; var cell = this.size; var tileXpercentage = 0; var tileXp = 0; var tileYp = 0; // --- horizontal grid lines --- var hrayX, hrayY, hoffX, hoffY, distDone, hmapX, hmapY, htileHit; var hDir = "north"; distDone = 0; var aTan = -1 / Math.tan(radian); if (radian > Math.PI) { hrayY = Math.floor(y / cell) * cell - 0.01; hrayX = (y - hrayY) * aTan + x; hoffY = -cell; hoffX = -hoffY * aTan; } else if (radian < Math.PI) { hrayY = Math.floor(y / cell) * cell + cell; hrayX = (y - hrayY) * aTan + x; hoffY = cell; hoffX = -hoffY * aTan; hDir = "south"; } else { hrayX = x; hrayY = y; distDone = maxDist; } while (distDone < maxDist) { tileXp = ((hrayX % cell) + cell) % cell / cell; tileYp = ((hrayY % cell) + cell) % cell / cell; hmapX = Math.floor(hrayX / cell); hmapY = Math.floor(hrayY / cell); var htileHitId = this.level.tiles[hmapX + hmapY * this.level.width]; htileHit = Tiles.findByID(htileHitId); if (htileHit && htileHit.Solid) { distDone = maxDist; } else { hrayX += hoffX; hrayY += hoffY; distDone += 1; } } switch (hDir) { case "north": tileXpercentage = tileXp; break; case "south": tileXpercentage = 1 - tileXp; break; case "east": tileXpercentage = 1 - tileYp; break; case "west": tileXpercentage = tileYp; break; } var hResult = { tile: htileHit, tileX: tileXpercentage, endX: hrayX, endY: hrayY, dir: hDir }; // --- vertical grid lines --- var rayX, rayY, offX, offY, mapX, mapY, tileHit; var vDir = "east"; var nTan = -Math.tan(radian); distDone = 0; if (radian > Math.PI / 2 && radian < Math.PI * 1.5) { rayX = Math.floor(x / cell) * cell - 0.01; rayY = (x - rayX) * nTan + y; offX = -cell; offY = -offX * nTan; } else if (radian < Math.PI / 2 || radian > Math.PI * 1.5) { rayX = Math.floor(x / cell) * cell + cell; rayY = (x - rayX) * nTan + y; offX = cell; offY = -offX * nTan; vDir = "west"; } else { rayX = x; rayY = y; distDone = maxDist; } while (distDone < maxDist) { tileXp = ((rayX % cell) + cell) % cell / cell; tileYp = ((rayY % cell) + cell) % cell / cell; mapX = Math.floor(rayX / cell); mapY = Math.floor(rayY / cell); var tileHitId = this.level.tiles[mapX + mapY * this.level.width]; tileHit = Tiles.findByID(tileHitId); if (tileHit && tileHit.Solid) { distDone = maxDist; } else { rayX += offX; rayY += offY; distDone += 1; } } switch (vDir) { case "north": tileXpercentage = tileXp; break; case "south": tileXpercentage = 1 - tileXp; break; case "east": tileXpercentage = 1 - tileYp; break; case "west": tileXpercentage = tileYp; break; } var vResult = { tile: tileHit, tileX: tileXpercentage, endX: rayX, endY: rayY, dir: vDir }; var dxH = hResult.endX - x; var dyH = hResult.endY - y; var dxV = vResult.endX - x; var dyV = vResult.endY - y; hResult.distance = Math.sqrt(dxH * dxH + dyH * dyH); vResult.distance = Math.sqrt(dxV * dxV + dyV * dyV); if (hResult.distance < vResult.distance) { return hResult; } return vResult; } }; if (typeof module !== "undefined" && module.exports) { module.exports = { World: World }; } /* * End File: * /workspace/raytracer-work/src/world.js */ /* * Start File: * /workspace/raytracer-work/src/player.js */ var Player = { x: 0, y: 0, rotation: 0, pitch: 0, /** Screen columns per frame. 240 is sharp on ~1080p and cheap (~240 drawSprite calls). */ horizontalResolution: 240, gunX: 0.6, gunY: 0.6, currentGun: 0, guns: 3, resetToSpawn: function () { var spawn = World.spawnPoint(); this.x = spawn.x; this.y = spawn.y; this.rotation = 0; this.pitch = 0; }, applyLookDelta: function (movementX, movementY) { this.pitch += movementY * 0.01; this.rotation += movementX * 0.001; if (this.pitch > Math.PI) { this.pitch = Math.PI; } if (this.pitch < -Math.PI) { this.pitch = -Math.PI; } if (this.rotation > Math.PI * 2) { this.rotation -= Math.PI * 2; } if (this.rotation < 0) { this.rotation += Math.PI * 2; } }, /** Legacy angles: 0 = +X (east), increases toward +Y. */ forwardVector: function (radian) { return { x: Math.cos(radian), y: Math.sin(radian) }; }, tryMove: function (rot, speed, deltaTime) { if (World.rayCast(this.x, this.y, rot).distance <= 5) { return; } var forward = this.forwardVector(rot); this.x += forward.x * deltaTime * speed; this.y += forward.y * deltaTime * speed; }, update: function (deltaTime) { if (GameInput.isHeld(37) || GameInput.isHeld(65)) { var rotL = this.rotation - Math.PI / 2; if (rotL < 0) { rotL += Math.PI * 2; } this.tryMove(rotL, 60, deltaTime); } if (GameInput.isHeld(39) || GameInput.isHeld(68)) { var rotR = this.rotation + Math.PI / 2; if (rotR > Math.PI * 2) { rotR -= Math.PI * 2; } this.tryMove(rotR, 60, deltaTime); } if (GameInput.isHeld(38) || GameInput.isHeld(87)) { this.tryMove(this.rotation, 60, deltaTime); } if (GameInput.isHeld(40) || GameInput.isHeld(83)) { var rotB = this.rotation + Math.PI; if (rotB > Math.PI * 2) { rotB -= Math.PI * 2; } if (World.rayCast(this.x, this.y, rotB).distance > 5) { var forward = this.forwardVector(this.rotation); this.x -= forward.x * deltaTime * 40; this.y -= forward.y * deltaTime * 40; } } if (GameInput.isHeld(188)) { this.horizontalResolution -= this.horizontalResolution * 0.1 * deltaTime; if (this.horizontalResolution < 30) { this.horizontalResolution = 30; } } if (GameInput.isHeld(190)) { this.horizontalResolution += this.horizontalResolution * 0.1 * deltaTime; } } }; /* * End File: * /workspace/raytracer-work/src/player.js */ /* * Start File: * /workspace/raytracer-work/src/raycastScene.js */ /** * Fullscreen WebGL raycast view on frameworkv3. * Wall strips: one source texel column per screen column, NEAREST, tinted for shading. */ class RaycastScene extends Scene { constructor (width, height) { super(0, 0, width, height, Scene.DisplayModes.absolute); this.backgroundColor = { r: 0, g: 0, b: 0 }; this.tintShader = null; this.solidTexture = null; this.gunImages = null; this._lookBound = false; } onLoad () { const gl = this.glContext.gl; this.glContext.setDefaultTextureFilter(gl.NEAREST, gl.NEAREST); this.tintShader = new Shader(Shader.DEFAULT_VERT, [ "precision mediump float;", "uniform sampler2D u_texture;", "uniform float u_alpha;", "uniform vec3 u_tint;", "varying vec2 v_texCoord;", "void main() {", " vec4 color = texture2D(u_texture, v_texCoord);", " gl_FragColor = vec4(color.rgb * u_tint, color.a * u_alpha);", "}" ].join("\n")); // 1x1 white for solid (untextured) walls. const c = document.createElement("canvas"); c.width = 1; c.height = 1; const ctx = c.getContext("2d"); ctx.fillStyle = "#ffffff"; ctx.fillRect(0, 0, 1, 1); this.solidTexture = this.glContext.getTexture(c); this.bindPointerLock(); } bindPointerLock () { if (this._lookBound) { return; } this._lookBound = true; const canvas = this.cv_canvas; canvas.addEventListener("click", function () { if (canvas.requestPointerLock) { canvas.requestPointerLock(); } }); document.addEventListener("mousemove", function (e) { if (document.pointerLockElement === canvas) { Player.applyLookDelta(e.movementX || 0, e.movementY || 0); } }); } update (deltaTime) { super.update(deltaTime); Player.update(deltaTime); if (Player.horizontalResolution > this.real_size.x) { Player.horizontalResolution = this.real_size.x; } } draw () { if (this.destroyed || !this.glContext) { return; } this.g_fixSize(); const glc = this.glContext; glc.endOffscreenPass(); glc.clear(0, 0, 0, 1); this.drawColumns(); this.drawGun(); } faceLight (dir) { switch (dir) { case "east": return 0.9; case "west": return 0.8; case "south": return 0.7; default: return 1.0; } } drawColumns () { const glc = this.glContext; const w = this.real_size.x; const h = this.real_size.y; const cols = Math.max(1, Math.floor(Player.horizontalResolution)); const rad = Math.PI / (cols * 2); const widthLine = w / cols; for (var i = 0; i < cols; i++) { var offsetP = -0.5 + (i / cols); var offset = offsetP * cols; var rotationalOffset = offset * rad; var rot = Player.rotation + rotationalOffset; if (rot < 0) { rot += Math.PI * 2; } if (rot > Math.PI * 2) { rot -= Math.PI * 2; } var rayHit = World.rayCast(Player.x, Player.y, rot); if (!rayHit || !rayHit.tile) { continue; } var light = this.faceLight(rayHit.dir); var deltaAngle = Player.rotation - rot; var dist = rayHit.distance * Math.cos(deltaAngle); if (dist < 0.0001) { dist = 0.0001; } var xLine = i * widthLine; var wallH = (World.size * h) / dist; var yCenter = (h / 2) + (-Player.pitch * (World.size * h - wallH) * 0.005); var tint = [light, light, light]; var htmlImage = null; var texW = 1; if (rayHit.tile.Image && rayHit.tile.Image.g_image) { htmlImage = rayHit.tile.Image.g_image; texW = htmlImage.width || 1; var uv = textureColumnUv(rayHit.tileX, texW); glc.drawSprite({ texture: glc.getTexture(htmlImage), x: xLine + widthLine / 2, y: yCenter, w: widthLine, h: wallH, rotationDeg: 0, alpha: 1, uvRect: { u0: uv.u0, v0: uv.v0, u1: uv.u1, v1: uv.v1 }, shader: this.tintShader, uniforms: { u_tint: tint } }); } else { var c = rayHit.tile.Color || { R: 255, G: 255, B: 255 }; tint = [(c.R / 255) * light, (c.G / 255) * light, (c.B / 255) * light]; glc.drawSprite({ texture: this.solidTexture, x: xLine + widthLine / 2, y: yCenter, w: widthLine, h: wallH, rotationDeg: 0, alpha: 1, uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 }, shader: this.tintShader, uniforms: { u_tint: tint } }); } } } drawGun () { const images = this.game && this.game.images; if (!images) { return; } var gunImg = null; switch (Player.currentGun) { case 0: gunImg = images.shotgun; break; case 1: gunImg = images.machinegun; break; case 2: gunImg = images.pulserifle; break; } if (!gunImg || !gunImg.g_image) { return; } var p = this.real_size.y / 1080; var gw = p * gunImg.g_image.width; var gh = p * gunImg.g_image.height; var gx = this.real_size.x * Player.gunX + gw / 2; var gy = this.real_size.y * Player.gunY + gh / 2; gunImg.draw( this, gx, gy, gw, gh, 0, false, false, 1, 0 ); } } /* * End File: * /workspace/raytracer-work/src/raycastScene.js */ /* * Start File: * /workspace/raytracer-work/src/main.js */ console.log("Raytracer (frameworkv3) starting"); var game = new Game(); window.onload = function () { game.preloadImagesThenStart( [ { name: "stone", url: "images/stone.png" }, { name: "wood", url: "images/wood.png" }, { name: "shotgun", url: "images/shotgun.png" }, { name: "machinegun", url: "images/machinegun.png" }, { name: "pulserifle", url: "images/pulserifle.png" } ], function () { Tiles.bindImages(game.images); World.loadLevel(0); Player.resetToSpawn(); var w = Math.max(320, window.innerWidth - 2); var h = Math.max(240, window.innerHeight - 2); var scene = new RaycastScene(w, h); game.loadScene(scene); window.addEventListener("resize", function () { scene.size.x = Math.max(320, window.innerWidth - 2); scene.size.y = Math.max(240, window.innerHeight - 2); }); } ); }; /* * End File: * /workspace/raytracer-work/src/main.js */