BlueGit

timfa raytracer

Files

raytracer / test / lighting.test.js

lighting.test.js

lighting.test.js 4.2 KiB Download Raw
1const { describe, it } = require('node:test');
2const assert = require('node:assert/strict');
3const Lighting = require('../src/lighting.js');
4const FloorCast = require('../src/floorCast.js');
5const LevelFormat = require('../src/levelFormat.js');
6const fs = require('fs');
7const path = require('path');
8
9describe('Lighting', () => {
10 it('attenuation reaches 0 at range and 1 at distance 0', () => {
11 assert.equal(Lighting.attenuation(0, 100, 2), 1);
12 assert.equal(Lighting.attenuation(100, 100, 2), 0);
13 });
14
15 it('faceNormal: north-ray hit faces outward +Y (south face)', () => {
16 assert.deepEqual(Lighting.faceNormal('north'), { x: 0, y: 1, z: 0 });
17 assert.deepEqual(Lighting.faceNormal('south'), { x: 0, y: -1, z: 0 });
18 assert.deepEqual(Lighting.faceNormal('west'), { x: -1, y: 0, z: 0 });
19 assert.deepEqual(Lighting.faceNormal('east'), { x: 1, y: 0, z: 0 });
20 });
21
22 it('wall facing corridor receives light (N·L > 0) with corrected normals', () => {
23 // Ray went east, hit west face of wall at x≈96; light in corridor to the west
24 const n = Lighting.faceNormal('west');
25 const lit = Lighting.shadeRgb(
26 { x: 95.4, y: 48, z: 16 }, n,
27 [{ x: 70, y: 48, z: 22, color: [1, 1, 1], intensity: 2, range: 120, falloff: 1 }],
28 0.15, null
29 );
30 assert.ok(lit.r > 0.6, 'wall lit r=' + lit.r);
31 });
32
33 it('LOS blocked by solid between open cells', () => {
34 assert.equal(Lighting.hasLineOfSight(8, 16, 72, 16, [0, 1, 0], 3, 1, 32), false);
35 assert.equal(Lighting.hasLineOfSight(8, 16, 24, 16, [0, 0, 0], 3, 1, 32), true);
36 });
37
38 it('decodeNormalRgb maps 128,128,255 toward +Z', () => {
39 const n = Lighting.decodeNormalRgb(128, 128, 255);
40 assert.ok(n.z > 0.9);
41 });
42
43 it('worldDirToView: light on camera-left (−Y at yaw0 facing0) → view −X', () => {
44 const v = Lighting.worldDirToView(0, -1, 0, 0, 0);
45 assert.ok(v.x < -0.7, JSON.stringify(v));
46 });
47
48 it('NPC left half brighter than right with white lamp on camera-left', () => {
49 const ambient = 0.12;
50 const lamp = { x: 100, y: 60, z: 18, color: [1, 1, 1], intensity: 2.5, range: 120, falloff: 1.5 };
51 const facing = 0, yaw = 0;
52 // Flat front normal + geo tilt
53 const left = Lighting.shadePaperdollPixel(
54 220, 220, 220, 128, 128, 255,
55 { x: 100, y: 85, z: 16 }, [lamp], ambient, yaw, facing, -0.8
56 );
57 const right = Lighting.shadePaperdollPixel(
58 220, 220, 220, 128, 128, 255,
59 { x: 100, y: 115, z: 16 }, [lamp], ambient, yaw, facing, 0.8
60 );
61 assert.ok(left.r > right.r + 25, 'left=' + left.r + ' right=' + right.r);
62 });
63
64 it('NPC right half brighter with lamp on camera-right (mirrored)', () => {
65 const ambient = 0.12;
66 const lamp = { x: 100, y: 140, z: 18, color: [1, 1, 1], intensity: 2.5, range: 120, falloff: 1.5 };
67 const left = Lighting.shadePaperdollPixel(
68 220, 220, 220, 128, 128, 255,
69 { x: 100, y: 85, z: 16 }, [lamp], ambient, 0, 0, -0.8
70 );
71 const right = Lighting.shadePaperdollPixel(
72 220, 220, 220, 128, 128, 255,
73 { x: 100, y: 115, z: 16 }, [lamp], ambient, 0, 0, 0.8
74 );
75 assert.ok(right.r > left.r + 25, 'left=' + left.r + ' right=' + right.r);
76 });
77
78 it('floor lighting has no hard column seam (max jump small)', () => {
79 const pack = LevelFormat.parse(fs.readFileSync(path.join(__dirname, '../build/levels.json'), 'utf8'));
80 const level = LevelFormat.toRuntimeLevel(pack.levels[0]);
81 const tex = { w: 1, h: 1, rgba: new Uint8ClampedArray([180, 180, 180, 255]) };
82 const w = 320, h = 240;
83 const img = { data: new Uint8ClampedArray(w * h * 4) };
84 global.Lighting = Lighting;
85 FloorCast.render(img, w, h, { x: 48, y: 48, rotation: 0.15, pitch: 0.25 }, level, { stone: tex, wood: tex }, 32);
86 const jump = Lighting.maxColumnJump(function (x) {
87 var s = 0, n = 0;
88 for (var y = Math.floor(h * 0.65); y < h; y++)
89 { s += img.data[(y * w + x) * 4]; n++; }
90 return s / n;
91 }, w);
92 assert.ok(jump < 18, 'floor column jump=' + jump);
93 // No near-black punch in central floor band (wall-null hole regression)
94 var black = 0, total = 0;
95 for (var x = 40; x < w - 40; x++)
96 {
97 for (var y = Math.floor(h * 0.7); y < h - 2; y++)
98 {
99 var o = (y * w + x) * 4;
100 total++;
101 if (img.data[o] + img.data[o + 1] + img.data[o + 2] < 8)
102 { black++; }
103 }
104 }
105 assert.ok(black / total < 0.02, 'black floor fraction=' + (black / total));
106 });
107});