/** * Classic floor/ceiling casting (Lode-style) — pure math + CPU NEAREST fill. * Rays use Camera plane (dir+plane*camX); rowDist is perpendicular depth. * Chosen over a grid data-texture shader because frameworkv3 is WebGL1 sprites * without a shared depth buffer; one ImageData upload stays simple and keeps * wall/NPC draw order unchanged (floor under walls). * * posZ must match wall projection: wallH = (cellSize * screenH) / dist, so the * floor at distance d sits at p = (cellSize/2 * screenH) / d from the horizon. * Using posZ = screenH/2 made rowDist ≈ 1–2 across the whole floor (one flat * texel of a 400² texture). */ var FloorCast = { /** Camera height in projection units: (cellSize/2) * screenH. */ posZ: function (screenH, cellSize) { return (cellSize * 0.5) * screenH; }, /** * World-space floor hit for screen pixel (sx, sy). * @returns {{wx:number,wy:number,rowDist:number,isCeiling:boolean}|null} */ floorHit: function (sx, sy, screenW, screenH, playerX, playerY, rot, pitch, posZ, cellSize) { cellSize = cellSize || 32; var mid = screenH / 2 + (-pitch * screenH * 0.25); var p = sy - mid; if (p === 0) { return null; } if (posZ == null) { posZ = this.posZ(screenH, cellSize); } var rowDist = posZ / Math.abs(p); var camX = (typeof Camera !== "undefined") ? Camera.camXFromScreen(sx, screenW) : (2 * sx / screenW) - 1; var dir; if (typeof Camera !== "undefined") { dir = Camera.rayDir(rot, camX); } else { var rayAngle = rot + camX * (Math.PI / 4); dir = { x: Math.cos(rayAngle), y: Math.sin(rayAngle) }; } return { wx: playerX + dir.x * rowDist, wy: playerY + dir.y * rowDist, rowDist: rowDist, isCeiling: p < 0 }; }, /** * Sample NEAREST from RGBA Uint8ClampedArray tex (tw×th). * u,v in [0,1) tile space (or any real; wraps). */ sampleNearest: function (rgba, tw, th, u, v) { var tx = Math.floor(((u % 1) + 1) % 1 * tw); var ty = Math.floor(((v % 1) + 1) % 1 * th); if (tx < 0) tx = 0; if (ty < 0) ty = 0; if (tx >= tw) tx = tw - 1; if (ty >= th) ty = th - 1; var o = (ty * tw + tx) * 4; return [rgba[o], rgba[o + 1], rgba[o + 2], rgba[o + 3]]; }, /** * Count unique RGB triples in ImageData (optionally only in a rect). * Useful for tests: textured floor must not collapse to 1–2 colours. */ countUniqueRgb: function (imgData, screenW, screenH, x0, y0, x1, y1) { x0 = x0 == null ? 0 : x0; y0 = y0 == null ? 0 : y0; x1 = x1 == null ? screenW : x1; y1 = y1 == null ? screenH : y1; var data = imgData.data; var seen = Object.create(null); var n = 0; for (var y = y0; y < y1; y++) { for (var x = x0; x < x1; x++) { var o = (y * screenW + x) * 4; var key = data[o] + "," + data[o + 1] + "," + data[o + 2]; if (!seen[key]) { seen[key] = 1; n++; } } } return n; }, /** * Fill ImageData with floor (below horizon) and ceiling (above). * texMap: { stone: {rgba,w,h}, wood: {...} } * level: runtime level with floors[], ceilings[], width, height, cellSize */ render: function (imgData, screenW, screenH, player, level, texMap, cellSize) { cellSize = cellSize || 32; var data = imgData.data; var mid = screenH / 2 + (-player.pitch * screenH * 0.25); var posZ = this.posZ(screenH, cellSize); var lw = level.width; var lh = level.height; var floors = level.floors; var ceilings = level.ceilings; // Step 2×2: NEAREST block fill — ~4× sneller, nog steeds pixelig. var step = 2; for (var sy = 0; sy < screenH; sy += step) { var p = (sy + step * 0.5) - mid; if (Math.abs(p) < 0.5) { continue; } var isCeil = p < 0; var rowDist = posZ / Math.abs(p); // Mild distance shade; keep texel contrast readable. var shade = Math.max(0.45, Math.min(1, 1.05 - rowDist / (cellSize * 40))); for (var sx = 0; sx < screenW; sx += step) { var camX = (typeof Camera !== "undefined") ? Camera.camXFromScreen(sx + step * 0.5, screenW) : (2 * (sx + step * 0.5) / screenW) - 1; var rdir = (typeof Camera !== "undefined") ? Camera.rayDir(player.rotation, camX) : { x: Math.cos(player.rotation + camX * (Math.PI / 4)), y: Math.sin(player.rotation + camX * (Math.PI / 4)) }; var wx = player.x + rdir.x * rowDist; var wy = player.y + rdir.y * rowDist; var cx = Math.floor(wx / cellSize); var cy = Math.floor(wy / cellSize); var r = 0, g = 0, b = 0; if (cx >= 0 && cy >= 0 && cx < lw && cy < lh) { var idx = cx + cy * lw; var texId = isCeil ? ceilings[idx] : floors[idx]; // Solid wall cells store null floor/ceiling; still sample a default // so perspective rays that hit that XY don't punch a black hole // next to lit open floor (looks like a hard vertical seam). if (!texId && level.tiles) { var tid = level.tiles[idx]; if (tid === 1 || tid === 3) { texId = tid === 3 ? "wood" : "stone"; } } if (texId && texMap[texId]) { var tex = texMap[texId]; var u = wx / cellSize; var vv = wy / cellSize; var c = this.sampleNearest(tex.rgba, tex.w, tex.h, u, vv); var wz = isCeil ? cellSize : 0; var nrm = isCeil ? { x: 0, y: 0, z: -1 } : { x: 0, y: 0, z: 1 }; var lights = level.lights || []; var ambient = level.ambient != null ? level.ambient : 0.38; var lit; if (typeof Lighting !== "undefined" && lights.length) { // No grid LOS on floors/ceilings — avoids hard screen-space seams. lit = Lighting.shadeRgb({ x: wx, y: wy, z: wz }, nrm, lights, ambient, null); } else { // No lamps: keep previous distance shade look. lit = { r: shade, g: shade, b: shade }; } r = Math.min(255, (c[0] * lit.r) | 0); g = Math.min(255, (c[1] * lit.g) | 0); b = Math.min(255, (c[2] * lit.b) | 0); } } for (var dy = 0; dy < step && sy + dy < screenH; dy++) { for (var dx = 0; dx < step && sx + dx < screenW; dx++) { var o = ((sy + dy) * screenW + (sx + dx)) * 4; data[o] = r; data[o + 1] = g; data[o + 2] = b; data[o + 3] = 255; } } } } return imgData; } }; if (typeof module !== "undefined" && module.exports) { module.exports = FloorCast; }