BlueGit

timfa raytracer

Files

raytracer / src / floorCast.js

floorCast.js

floorCast.js 6.2 KiB Download Raw
1/**
2 * Classic floor/ceiling casting (Lode-style) — pure math + CPU NEAREST fill.
3 * Rays use Camera plane (dir+plane*camX); rowDist is perpendicular depth.
4 * Chosen over a grid data-texture shader because frameworkv3 is WebGL1 sprites
5 * without a shared depth buffer; one ImageData upload stays simple and keeps
6 * wall/NPC draw order unchanged (floor under walls).
7 *
8 * posZ must match wall projection: wallH = (cellSize * screenH) / dist, so the
9 * floor at distance d sits at p = (cellSize/2 * screenH) / d from the horizon.
10 * Using posZ = screenH/2 made rowDist ≈ 1–2 across the whole floor (one flat
11 * texel of a 400² texture).
12 */
13var FloorCast =
14{
15 /** Camera height in projection units: (cellSize/2) * screenH. */
16 posZ: function (screenH, cellSize)
17 {
18 return (cellSize * 0.5) * screenH;
19 },
20
21 /**
22 * World-space floor hit for screen pixel (sx, sy).
23 * @returns {{wx:number,wy:number,rowDist:number,isCeiling:boolean}|null}
24 */
25 floorHit: function (sx, sy, screenW, screenH, playerX, playerY, rot, pitch, posZ, cellSize)
26 {
27 cellSize = cellSize || 32;
28 var mid = screenH / 2 + (-pitch * screenH * 0.25);
29 var p = sy - mid;
30 if (p === 0)
31 { return null; }
32 if (posZ == null)
33 { posZ = this.posZ(screenH, cellSize); }
34 var rowDist = posZ / Math.abs(p);
35 var camX = (typeof Camera !== "undefined")
36 ? Camera.camXFromScreen(sx, screenW)
37 : (2 * sx / screenW) - 1;
38 var dir;
39 if (typeof Camera !== "undefined")
40 { dir = Camera.rayDir(rot, camX); }
41 else
42 {
43 var rayAngle = rot + camX * (Math.PI / 4);
44 dir = { x: Math.cos(rayAngle), y: Math.sin(rayAngle) };
45 }
46 return {
47 wx: playerX + dir.x * rowDist,
48 wy: playerY + dir.y * rowDist,
49 rowDist: rowDist,
50 isCeiling: p < 0
51 };
52 },
53
54 /**
55 * Sample NEAREST from RGBA Uint8ClampedArray tex (tw×th).
56 * u,v in [0,1) tile space (or any real; wraps).
57 */
58 sampleNearest: function (rgba, tw, th, u, v)
59 {
60 var tx = Math.floor(((u % 1) + 1) % 1 * tw);
61 var ty = Math.floor(((v % 1) + 1) % 1 * th);
62 if (tx < 0) tx = 0;
63 if (ty < 0) ty = 0;
64 if (tx >= tw) tx = tw - 1;
65 if (ty >= th) ty = th - 1;
66 var o = (ty * tw + tx) * 4;
67 return [rgba[o], rgba[o + 1], rgba[o + 2], rgba[o + 3]];
68 },
69
70 /**
71 * Count unique RGB triples in ImageData (optionally only in a rect).
72 * Useful for tests: textured floor must not collapse to 1–2 colours.
73 */
74 countUniqueRgb: function (imgData, screenW, screenH, x0, y0, x1, y1)
75 {
76 x0 = x0 == null ? 0 : x0;
77 y0 = y0 == null ? 0 : y0;
78 x1 = x1 == null ? screenW : x1;
79 y1 = y1 == null ? screenH : y1;
80 var data = imgData.data;
81 var seen = Object.create(null);
82 var n = 0;
83 for (var y = y0; y < y1; y++)
84 {
85 for (var x = x0; x < x1; x++)
86 {
87 var o = (y * screenW + x) * 4;
88 var key = data[o] + "," + data[o + 1] + "," + data[o + 2];
89 if (!seen[key])
90 {
91 seen[key] = 1;
92 n++;
93 }
94 }
95 }
96 return n;
97 },
98
99 /**
100 * Fill ImageData with floor (below horizon) and ceiling (above).
101 * texMap: { stone: {rgba,w,h}, wood: {...} }
102 * level: runtime level with floors[], ceilings[], width, height, cellSize
103 */
104 render: function (imgData, screenW, screenH, player, level, texMap, cellSize)
105 {
106 cellSize = cellSize || 32;
107 var data = imgData.data;
108 var mid = screenH / 2 + (-player.pitch * screenH * 0.25);
109 var posZ = this.posZ(screenH, cellSize);
110 var lw = level.width;
111 var lh = level.height;
112 var floors = level.floors;
113 var ceilings = level.ceilings;
114
115 // Step 2×2: NEAREST block fill — ~4× sneller, nog steeds pixelig.
116 var step = 2;
117 for (var sy = 0; sy < screenH; sy += step)
118 {
119 var p = (sy + step * 0.5) - mid;
120 if (Math.abs(p) < 0.5)
121 { continue; }
122 var isCeil = p < 0;
123 var rowDist = posZ / Math.abs(p);
124 // Mild distance shade; keep texel contrast readable.
125 var shade = Math.max(0.45, Math.min(1, 1.05 - rowDist / (cellSize * 40)));
126 for (var sx = 0; sx < screenW; sx += step)
127 {
128 var camX = (typeof Camera !== "undefined")
129 ? Camera.camXFromScreen(sx + step * 0.5, screenW)
130 : (2 * (sx + step * 0.5) / screenW) - 1;
131 var rdir = (typeof Camera !== "undefined")
132 ? Camera.rayDir(player.rotation, camX)
133 : { x: Math.cos(player.rotation + camX * (Math.PI / 4)),
134 y: Math.sin(player.rotation + camX * (Math.PI / 4)) };
135 var wx = player.x + rdir.x * rowDist;
136 var wy = player.y + rdir.y * rowDist;
137 var cx = Math.floor(wx / cellSize);
138 var cy = Math.floor(wy / cellSize);
139 var r = 0, g = 0, b = 0;
140 if (cx >= 0 && cy >= 0 && cx < lw && cy < lh)
141 {
142 var idx = cx + cy * lw;
143 var texId = isCeil ? ceilings[idx] : floors[idx];
144 // Solid wall cells store null floor/ceiling; still sample a default
145 // so perspective rays that hit that XY don't punch a black hole
146 // next to lit open floor (looks like a hard vertical seam).
147 if (!texId && level.tiles)
148 {
149 var tid = level.tiles[idx];
150 if (tid === 1 || tid === 3)
151 { texId = tid === 3 ? "wood" : "stone"; }
152 }
153 if (texId && texMap[texId])
154 {
155 var tex = texMap[texId];
156 var u = wx / cellSize;
157 var vv = wy / cellSize;
158 var c = this.sampleNearest(tex.rgba, tex.w, tex.h, u, vv);
159 var wz = isCeil ? cellSize : 0;
160 var nrm = isCeil
161 ? { x: 0, y: 0, z: -1 }
162 : { x: 0, y: 0, z: 1 };
163 var lights = level.lights || [];
164 var ambient = level.ambient != null ? level.ambient : 0.38;
165 var lit;
166 if (typeof Lighting !== "undefined" && lights.length)
167 {
168 // No grid LOS on floors/ceilings — avoids hard screen-space seams.
169 lit = Lighting.shadeRgb({ x: wx, y: wy, z: wz }, nrm, lights, ambient, null);
170 }
171 else
172 {
173 // No lamps: keep previous distance shade look.
174 lit = { r: shade, g: shade, b: shade };
175 }
176 r = Math.min(255, (c[0] * lit.r) | 0);
177 g = Math.min(255, (c[1] * lit.g) | 0);
178 b = Math.min(255, (c[2] * lit.b) | 0);
179 }
180 }
181 for (var dy = 0; dy < step && sy + dy < screenH; dy++)
182 {
183 for (var dx = 0; dx < step && sx + dx < screenW; dx++)
184 {
185 var o = ((sy + dy) * screenW + (sx + dx)) * 4;
186 data[o] = r;
187 data[o + 1] = g;
188 data[o + 2] = b;
189 data[o + 3] = 255;
190 }
191 }
192 }
193 }
194 return imgData;
195 }
196};
197
198if (typeof module !== "undefined" && module.exports)
199{ module.exports = FloorCast; }