BlueGit

timfa raytracer

Files

raytracer / test / floorCast.test.js

floorCast.test.js

floorCast.test.js 4.3 KiB Download Raw
1const { describe, it } = require('node:test');
2const assert = require('node:assert/strict');
3const fs = require('fs');
4const path = require('path');
5const FloorCast = require('../src/floorCast.js');
6
7function makeChecker (tw, th, cell)
8{
9 const rgba = new Uint8ClampedArray(tw * th * 4);
10 const colors = [
11 [255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0],
12 [255, 0, 255], [0, 255, 255], [255, 128, 0], [128, 0, 255]
13 ];
14 for (let y = 0; y < th; y++)
15 {
16 for (let x = 0; x < tw; x++)
17 {
18 const ci = ((x / cell) | 0) + ((y / cell) | 0) * 2;
19 const c = colors[ci % colors.length];
20 const o = (y * tw + x) * 4;
21 rgba[o] = c[0]; rgba[o + 1] = c[1]; rgba[o + 2] = c[2]; rgba[o + 3] = 255;
22 }
23 }
24 return { rgba, w: tw, h: th };
25}
26
27function openLevel (texId)
28{
29 const w = 8, h = 8, n = w * h;
30 const floors = [];
31 const ceilings = [];
32 for (let i = 0; i < n; i++)
33 {
34 floors.push(texId);
35 ceilings.push(texId);
36 }
37 return { width: w, height: h, floors, ceilings };
38}
39
40describe('FloorCast math', () => {
41 it('floorHit below horizon returns positive path and isCeiling false', () => {
42 const hit = FloorCast.floorHit(160, 300, 320, 240, 48, 48, 0, 0, null, 32);
43 assert.ok(hit);
44 assert.equal(hit.isCeiling, false);
45 assert.ok(hit.rowDist > 0);
46 assert.ok(hit.wx > 48);
47 });
48
49 it('floorHit above horizon is ceiling', () => {
50 const hit = FloorCast.floorHit(160, 40, 320, 240, 48, 48, 0, 0, null, 32);
51 assert.ok(hit);
52 assert.equal(hit.isCeiling, true);
53 });
54
55 it('posZ matches wall projection: bottom row ≈ one cell away', () => {
56 const screenH = 240, cell = 32;
57 const hit = FloorCast.floorHit(160, screenH - 1, 320, screenH, 48, 48, 0, 0, null, cell);
58 // p ≈ screenH/2 → rowDist ≈ cellSize
59 assert.ok(hit.rowDist > cell * 0.7 && hit.rowDist < cell * 1.4, 'rowDist=' + hit.rowDist);
60 });
61
62 it('perspective: farther rows (nearer horizon) have larger rowDist', () => {
63 const near = FloorCast.floorHit(160, 220, 320, 240, 48, 48, 0, 0, null, 32);
64 const far = FloorCast.floorHit(160, 130, 320, 240, 48, 48, 0, 0, null, 32);
65 assert.ok(far.rowDist > near.rowDist * 1.5);
66 });
67
68 it('sampleNearest wraps and picks texel', () => {
69 const rgba = new Uint8ClampedArray([
70 10, 0, 0, 255, 20, 0, 0, 255,
71 30, 0, 0, 255, 40, 0, 0, 255
72 ]);
73 assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 0.1, 0.1), [10, 0, 0, 255]);
74 assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 0.6, 0.1), [20, 0, 0, 255]);
75 assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 1.1, 0.1), [10, 0, 0, 255]);
76 assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, -0.1, 0.6), [40, 0, 0, 255]);
77 });
78
79 it('render of checkerboard floor yields many unique colours (not flat)', () => {
80 const w = 160, h = 120;
81 const img = { data: new Uint8ClampedArray(w * h * 4) };
82 const tex = makeChecker(64, 64, 8);
83 const level = openLevel('check');
84 const player = { x: 48, y: 48, rotation: 0.2, pitch: 0.35 };
85 FloorCast.render(img, w, h, player, level, { check: tex }, 32);
86 const floorUnique = FloorCast.countUniqueRgb(img, w, h, 40, Math.floor(h * 0.55), 120, h);
87 assert.ok(floorUnique >= 6, 'floor unique colours=' + floorUnique + ' (need textured sampling)');
88 const ceilUnique = FloorCast.countUniqueRgb(img, w, h, 40, 0, 120, Math.floor(h * 0.35));
89 assert.ok(ceilUnique >= 4, 'ceiling unique colours=' + ceilUnique);
90 });
91
92 it('render with real stone.png yields many source texel colours on the floor', () => {
93 const meta = JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures/stone.meta.json'), 'utf8'));
94 const buf = fs.readFileSync(path.join(__dirname, 'fixtures/stone.rgba'));
95 const stone = { rgba: new Uint8ClampedArray(buf.buffer, buf.byteOffset, buf.byteLength), w: meta.w, h: meta.h };
96 assert.ok(stone.w >= 64 && stone.h >= 64);
97 assert.equal(stone.rgba.length, stone.w * stone.h * 4);
98 const w = 320, h = 240;
99 const img = { data: new Uint8ClampedArray(w * h * 4) };
100 const level = openLevel('stone');
101 const player = { x: 48, y: 48, rotation: 0.15, pitch: 0.4 };
102 FloorCast.render(img, w, h, player, level, { stone: stone }, 32);
103 const floorUnique = FloorCast.countUniqueRgb(img, w, h, 80, Math.floor(h * 0.55), 240, h - 4);
104 // Stone has ~80 unique colours; with perspective + shade we still need clearly > flat gray.
105 assert.ok(floorUnique >= 12, 'stone floor unique=' + floorUnique + ' (was ~2 when posZ was wrong)');
106 });
107});