BlueGit

timfa raytracer

Files

raytracer / src / floorCast.js

floorCast.js

floorCast.js 3.5 KiB Download Raw
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 */
7var FloorCast =
8{
9 /**
10 * World-space floor hit for screen pixel (sx, sy).
11 * @returns {{wx:number,wy:number,rowDist:number}|null}
12 */
13 floorHit: function (sx, sy, screenW, screenH, playerX, playerY, rot, pitch, posZ)
14 {
15 var mid = screenH / 2 + (-pitch * screenH * 0.25);
16 var p = sy - mid;
17 if (p === 0)
18 { return null; }
19 var absP = Math.abs(p);
20 posZ = posZ == null ? screenH * 0.5 : posZ;
21 var rowDist = posZ / absP;
22 var camX = (2 * sx / screenW) - 1;
23 // Match wall FOV: rotationalOffset ≈ camX * (π/4) at screen edges.
24 var rayAngle = rot + camX * (Math.PI / 4);
25 var dirX = Math.cos(rayAngle);
26 var dirY = Math.sin(rayAngle);
27 return {
28 wx: playerX + dirX * rowDist,
29 wy: playerY + dirY * rowDist,
30 rowDist: rowDist,
31 isCeiling: p < 0
32 };
33 },
34
35 /**
36 * Sample NEAREST from RGBA Uint8ClampedArray tex (tw×th).
37 */
38 sampleNearest: function (rgba, tw, th, u, v)
39 {
40 var tx = Math.floor(((u % 1) + 1) % 1 * tw);
41 var ty = Math.floor(((v % 1) + 1) % 1 * th);
42 if (tx < 0) tx = 0;
43 if (ty < 0) ty = 0;
44 if (tx >= tw) tx = tw - 1;
45 if (ty >= th) ty = th - 1;
46 var o = (ty * tw + tx) * 4;
47 return [rgba[o], rgba[o + 1], rgba[o + 2], rgba[o + 3]];
48 },
49
50 /**
51 * Fill ImageData with floor (below horizon) and ceiling (above).
52 * texMap: { stone: {rgba,w,h}, wood: {...} }
53 * level: runtime level with floors[], ceilings[], width, height, cellSize
54 */
55 render: function (imgData, screenW, screenH, player, level, texMap, cellSize)
56 {
57 cellSize = cellSize || 32;
58 var data = imgData.data;
59 var mid = screenH / 2 + (-player.pitch * screenH * 0.25);
60 var posZ = screenH * 0.5;
61 var lw = level.width;
62 var lh = level.height;
63 var floors = level.floors;
64 var ceilings = level.ceilings;
65
66 // Step 2×2: NEAREST block fill — ~4× sneller, nog steeds pixelig.
67 var step = 2;
68 for (var sy = 0; sy < screenH; sy += step)
69 {
70 var p = (sy + step * 0.5) - mid;
71 if (Math.abs(p) < 0.5)
72 { continue; }
73 var isCeil = p < 0;
74 var rowDist = posZ / Math.abs(p);
75 var shade = Math.max(0.35, Math.min(1, 1.2 - rowDist / 400));
76 for (var sx = 0; sx < screenW; sx += step)
77 {
78 var camX = (2 * (sx + step * 0.5) / screenW) - 1;
79 var rayAngle = player.rotation + camX * (Math.PI / 4);
80 var wx = player.x + Math.cos(rayAngle) * rowDist;
81 var wy = player.y + Math.sin(rayAngle) * rowDist;
82 var cx = Math.floor(wx / cellSize);
83 var cy = Math.floor(wy / cellSize);
84 var r = 0, g = 0, b = 0;
85 if (cx >= 0 && cy >= 0 && cx < lw && cy < lh)
86 {
87 var idx = cx + cy * lw;
88 var texId = isCeil ? ceilings[idx] : floors[idx];
89 if (texId && texMap[texId])
90 {
91 var tex = texMap[texId];
92 var u = (wx / cellSize) - cx;
93 var vv = (wy / cellSize) - cy;
94 var c = this.sampleNearest(tex.rgba, tex.w, tex.h, u, vv);
95 r = (c[0] * shade) | 0;
96 g = (c[1] * shade) | 0;
97 b = (c[2] * shade) | 0;
98 }
99 }
100 for (var dy = 0; dy < step && sy + dy < screenH; dy++)
101 {
102 for (var dx = 0; dx < step && sx + dx < screenW; dx++)
103 {
104 var o = ((sy + dy) * screenW + (sx + dx)) * 4;
105 data[o] = r;
106 data[o + 1] = g;
107 data[o + 2] = b;
108 data[o + 3] = 255;
109 }
110 }
111 }
112 }
113 return imgData;
114 }
115};
116
117if (typeof module !== "undefined" && module.exports)
118{ module.exports = FloorCast; }