/** * Classic floor/ceiling casting (Lode-style) — pure math + CPU NEAREST fill. * 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 = (2 * sx / screenW) - 1; // Match wall FOV: rotationalOffset ≈ camX * (π/4) at screen edges. var rayAngle = rot + camX * (Math.PI / 4); var dirX = Math.cos(rayAngle); var dirY = Math.sin(rayAngle); return { wx: playerX + dirX * rowDist, wy: playerY + dirY * 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 = (2 * (sx + step * 0.5) / screenW) - 1; var rayAngle = player.rotation + camX * (Math.PI / 4); var wx = player.x + Math.cos(rayAngle) * rowDist; var wy = player.y + Math.sin(rayAngle) * 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]; if (texId && texMap[texId]) { var tex = texMap[texId]; // World-space UV so texels shrink toward the horizon. var u = wx / cellSize; var vv = wy / cellSize; var c = this.sampleNearest(tex.rgba, tex.w, tex.h, u, vv); r = (c[0] * shade) | 0; g = (c[1] * shade) | 0; b = (c[2] * shade) | 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; }