| 1 | /** |
| 2 | * Classic floor/ceiling casting (Lode-style) — pure math + CPU NEAREST fill. |
| 3 | * Chosen over a grid data-texture shader because frameworkv3 is WebGL1 sprites |
| 4 | * without a shared depth buffer; one ImageData upload stays simple and keeps |
| 5 | * wall/NPC draw order unchanged (floor under walls). |
| 6 | * |
| 7 | * posZ must match wall projection: wallH = (cellSize * screenH) / dist, so the |
| 8 | * floor at distance d sits at p = (cellSize/2 * screenH) / d from the horizon. |
| 9 | * Using posZ = screenH/2 made rowDist ≈ 1–2 across the whole floor (one flat |
| 10 | * texel of a 400² texture). |
| 11 | */ |
| 12 | var FloorCast = |
| 13 | { |
| 14 | /** Camera height in projection units: (cellSize/2) * screenH. */ |
| 15 | posZ: function (screenH, cellSize) |
| 16 | { |
| 17 | return (cellSize * 0.5) * screenH; |
| 18 | }, |
| 19 | |
| 20 | /** |
| 21 | * World-space floor hit for screen pixel (sx, sy). |
| 22 | * @returns {{wx:number,wy:number,rowDist:number,isCeiling:boolean}|null} |
| 23 | */ |
| 24 | floorHit: function (sx, sy, screenW, screenH, playerX, playerY, rot, pitch, posZ, cellSize) |
| 25 | { |
| 26 | cellSize = cellSize || 32; |
| 27 | var mid = screenH / 2 + (-pitch * screenH * 0.25); |
| 28 | var p = sy - mid; |
| 29 | if (p === 0) |
| 30 | { return null; } |
| 31 | if (posZ == null) |
| 32 | { posZ = this.posZ(screenH, cellSize); } |
| 33 | var rowDist = posZ / Math.abs(p); |
| 34 | var camX = (2 * sx / screenW) - 1; |
| 35 | // Match wall FOV: rotationalOffset ≈ camX * (π/4) at screen edges. |
| 36 | var rayAngle = rot + camX * (Math.PI / 4); |
| 37 | var dirX = Math.cos(rayAngle); |
| 38 | var dirY = Math.sin(rayAngle); |
| 39 | return { |
| 40 | wx: playerX + dirX * rowDist, |
| 41 | wy: playerY + dirY * rowDist, |
| 42 | rowDist: rowDist, |
| 43 | isCeiling: p < 0 |
| 44 | }; |
| 45 | }, |
| 46 | |
| 47 | /** |
| 48 | * Sample NEAREST from RGBA Uint8ClampedArray tex (tw×th). |
| 49 | * u,v in [0,1) tile space (or any real; wraps). |
| 50 | */ |
| 51 | sampleNearest: function (rgba, tw, th, u, v) |
| 52 | { |
| 53 | var tx = Math.floor(((u % 1) + 1) % 1 * tw); |
| 54 | var ty = Math.floor(((v % 1) + 1) % 1 * th); |
| 55 | if (tx < 0) tx = 0; |
| 56 | if (ty < 0) ty = 0; |
| 57 | if (tx >= tw) tx = tw - 1; |
| 58 | if (ty >= th) ty = th - 1; |
| 59 | var o = (ty * tw + tx) * 4; |
| 60 | return [rgba[o], rgba[o + 1], rgba[o + 2], rgba[o + 3]]; |
| 61 | }, |
| 62 | |
| 63 | /** |
| 64 | * Count unique RGB triples in ImageData (optionally only in a rect). |
| 65 | * Useful for tests: textured floor must not collapse to 1–2 colours. |
| 66 | */ |
| 67 | countUniqueRgb: function (imgData, screenW, screenH, x0, y0, x1, y1) |
| 68 | { |
| 69 | x0 = x0 == null ? 0 : x0; |
| 70 | y0 = y0 == null ? 0 : y0; |
| 71 | x1 = x1 == null ? screenW : x1; |
| 72 | y1 = y1 == null ? screenH : y1; |
| 73 | var data = imgData.data; |
| 74 | var seen = Object.create(null); |
| 75 | var n = 0; |
| 76 | for (var y = y0; y < y1; y++) |
| 77 | { |
| 78 | for (var x = x0; x < x1; x++) |
| 79 | { |
| 80 | var o = (y * screenW + x) * 4; |
| 81 | var key = data[o] + "," + data[o + 1] + "," + data[o + 2]; |
| 82 | if (!seen[key]) |
| 83 | { |
| 84 | seen[key] = 1; |
| 85 | n++; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return n; |
| 90 | }, |
| 91 | |
| 92 | /** |
| 93 | * Fill ImageData with floor (below horizon) and ceiling (above). |
| 94 | * texMap: { stone: {rgba,w,h}, wood: {...} } |
| 95 | * level: runtime level with floors[], ceilings[], width, height, cellSize |
| 96 | */ |
| 97 | render: function (imgData, screenW, screenH, player, level, texMap, cellSize) |
| 98 | { |
| 99 | cellSize = cellSize || 32; |
| 100 | var data = imgData.data; |
| 101 | var mid = screenH / 2 + (-player.pitch * screenH * 0.25); |
| 102 | var posZ = this.posZ(screenH, cellSize); |
| 103 | var lw = level.width; |
| 104 | var lh = level.height; |
| 105 | var floors = level.floors; |
| 106 | var ceilings = level.ceilings; |
| 107 | |
| 108 | // Step 2×2: NEAREST block fill — ~4× sneller, nog steeds pixelig. |
| 109 | var step = 2; |
| 110 | for (var sy = 0; sy < screenH; sy += step) |
| 111 | { |
| 112 | var p = (sy + step * 0.5) - mid; |
| 113 | if (Math.abs(p) < 0.5) |
| 114 | { continue; } |
| 115 | var isCeil = p < 0; |
| 116 | var rowDist = posZ / Math.abs(p); |
| 117 | // Mild distance shade; keep texel contrast readable. |
| 118 | var shade = Math.max(0.45, Math.min(1, 1.05 - rowDist / (cellSize * 40))); |
| 119 | for (var sx = 0; sx < screenW; sx += step) |
| 120 | { |
| 121 | var camX = (2 * (sx + step * 0.5) / screenW) - 1; |
| 122 | var rayAngle = player.rotation + camX * (Math.PI / 4); |
| 123 | var wx = player.x + Math.cos(rayAngle) * rowDist; |
| 124 | var wy = player.y + Math.sin(rayAngle) * rowDist; |
| 125 | var cx = Math.floor(wx / cellSize); |
| 126 | var cy = Math.floor(wy / cellSize); |
| 127 | var r = 0, g = 0, b = 0; |
| 128 | if (cx >= 0 && cy >= 0 && cx < lw && cy < lh) |
| 129 | { |
| 130 | var idx = cx + cy * lw; |
| 131 | var texId = isCeil ? ceilings[idx] : floors[idx]; |
| 132 | if (texId && texMap[texId]) |
| 133 | { |
| 134 | var tex = texMap[texId]; |
| 135 | // World-space UV so texels shrink toward the horizon. |
| 136 | var u = wx / cellSize; |
| 137 | var vv = wy / cellSize; |
| 138 | var c = this.sampleNearest(tex.rgba, tex.w, tex.h, u, vv); |
| 139 | r = (c[0] * shade) | 0; |
| 140 | g = (c[1] * shade) | 0; |
| 141 | b = (c[2] * shade) | 0; |
| 142 | } |
| 143 | } |
| 144 | for (var dy = 0; dy < step && sy + dy < screenH; dy++) |
| 145 | { |
| 146 | for (var dx = 0; dx < step && sx + dx < screenW; dx++) |
| 147 | { |
| 148 | var o = ((sy + dy) * screenW + (sx + dx)) * 4; |
| 149 | data[o] = r; |
| 150 | data[o + 1] = g; |
| 151 | data[o + 2] = b; |
| 152 | data[o + 3] = 255; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | return imgData; |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | if (typeof module !== "undefined" && module.exports) |
| 162 | { module.exports = FloorCast; } |