BlueGit

timfa raytracer

Files

raytracer / test / floorCast.test.js

floorCast.test.js

floorCast.test.js 2.3 KiB Download Raw
1const { describe, it } = require('node:test');
2const assert = require('node:assert/strict');
3const FloorCast = require('../src/floorCast.js');
4
5describe('FloorCast math', () => {
6 it('floorHit below horizon returns positive wy path and isCeiling false', () => {
7 const hit = FloorCast.floorHit(160, 300, 320, 240, 48, 48, 0, 0, 120);
8 assert.ok(hit);
9 assert.equal(hit.isCeiling, false);
10 assert.ok(hit.rowDist > 0);
11 // looking east (rot 0): wx increases with distance
12 assert.ok(hit.wx > 48);
13 });
14
15 it('floorHit above horizon is ceiling', () => {
16 const hit = FloorCast.floorHit(160, 40, 320, 240, 48, 48, 0, 0, 120);
17 assert.ok(hit);
18 assert.equal(hit.isCeiling, true);
19 });
20
21 it('sampleNearest wraps and picks texel', () => {
22 const rgba = new Uint8ClampedArray([
23 10, 0, 0, 255, 20, 0, 0, 255,
24 30, 0, 0, 255, 40, 0, 0, 255
25 ]);
26 assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 0.1, 0.1), [10, 0, 0, 255]);
27 assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 0.6, 0.1), [20, 0, 0, 255]);
28 assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 1.1, 0.1), [10, 0, 0, 255]);
29 assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, -0.1, 0.6), [40, 0, 0, 255]);
30 });
31
32 it('render fills floor pixels from texMap and leaves black where null', () => {
33 const w = 64, h = 48;
34 const img = { data: new Uint8ClampedArray(w * h * 4) };
35 const level = {
36 width: 3, height: 3,
37 floors: [null, null, null, null, 'stone', null, null, null, null],
38 ceilings: [null, null, null, null, 'stone', null, null, null, null]
39 };
40 const stone = {
41 w: 1, h: 1,
42 rgba: new Uint8ClampedArray([200, 100, 50, 255])
43 };
44 const player = { x: 48, y: 48, rotation: 0, pitch: 0 };
45 FloorCast.render(img, w, h, player, level, { stone: stone }, 32);
46 let nonzero = 0;
47 for (let i = 0; i < img.data.length; i += 4) {
48 if (img.data[i] || img.data[i + 1] || img.data[i + 2]) nonzero++;
49 }
50 assert.ok(nonzero > 10, 'expected some textured floor/ceiling pixels');
51 // alpha always 255
52 assert.equal(img.data[3], 255);
53 });
54
55 it('perspective: farther rows have larger world step (rowDist grows toward horizon)', () => {
56 const near = FloorCast.floorHit(160, 220, 320, 240, 48, 48, 0, 0, 120);
57 const far = FloorCast.floorHit(160, 130, 320, 240, 48, 48, 0, 0, 120);
58 assert.ok(far.rowDist > near.rowDist);
59 });
60});