BlueGit

timfa raytracer

Files

raytracer / test / lighting.test.js

lighting.test.js

lighting.test.js 3.1 KiB Download Raw
1const { describe, it } = require('node:test');
2const assert = require('node:assert/strict');
3const Lighting = require('../src/lighting.js');
4
5describe('Lighting', () => {
6 it('attenuation reaches 0 at range and 1 at distance 0', () => {
7 assert.equal(Lighting.attenuation(0, 100, 2), 1);
8 assert.equal(Lighting.attenuation(100, 100, 2), 0);
9 assert.ok(Lighting.attenuation(50, 100, 2) > 0.2);
10 });
11
12 it('shadeRgb with light in front of floor is brighter than ambient', () => {
13 const lights = [{ x: 48, y: 48, z: 20, color: [1, 0, 0], intensity: 2, range: 80, falloff: 1 }];
14 const lit = Lighting.shadeRgb({ x: 48, y: 48, z: 0 }, { x: 0, y: 0, z: 1 }, lights, 0.2, null);
15 assert.ok(lit.r > 0.5, 'r=' + lit.r);
16 assert.ok(lit.g < lit.r);
17 });
18
19 it('LOS blocked by solid wall tile between light and sample', () => {
20 // 3x1 corridor: open, wall, open
21 const tiles = [0, 1, 0];
22 const ok = Lighting.hasLineOfSight(8, 16, 72, 16, tiles, 3, 1, 32);
23 assert.equal(ok, false);
24 const open = Lighting.hasLineOfSight(8, 16, 24, 16, [0, 0, 0], 3, 1, 32);
25 assert.equal(open, true);
26 });
27
28 it('decodeNormalRgb maps 128,128,255 toward +Z', () => {
29 const n = Lighting.decodeNormalRgb(128, 128, 255);
30 assert.ok(Math.abs(n.x) < 0.05);
31 assert.ok(Math.abs(n.y) < 0.05);
32 assert.ok(n.z > 0.9);
33 });
34
35 it('worldDirToView: light to the character-right yields +X in view at yaw 0', () => {
36 // facing +X (0°), yaw 0 → camera along +X; character right = +Y world
37 const v = Lighting.worldDirToView(0, 1, 0, 0, 0);
38 assert.ok(v.x > 0.7, JSON.stringify(v));
39 });
40
41 it('shadePaperdollPixel: light on left (+view X or -) differs from light on right', () => {
42 const ambient = 0.15;
43 const nFlat = { r: 128, g: 128, b: 255 }; // +Z
44 const px = { x: 100, y: 100, z: 16 };
45 const left = [{ x: 100, y: 60, z: 16, color: [1, 1, 1], intensity: 3, range: 80, falloff: 1 }];
46 const right = [{ x: 100, y: 140, z: 16, color: [1, 1, 1], intensity: 3, range: 80, falloff: 1 }];
47 // facing 0 (+X), yaw 0: view X aligns with world +Y roughly
48 const a = Lighting.shadePaperdollPixel(200, 200, 200, 200, 128, 200, px, left, ambient, 0, 0);
49 const b = Lighting.shadePaperdollPixel(200, 200, 200, 55, 128, 200, px, left, ambient, 0, 0);
50 // Normal tilted +X vs -X should respond differently to same light
51 assert.notEqual(a.r + a.g + a.b, b.r + b.g + b.b);
52 const c = Lighting.shadePaperdollPixel(200, 200, 200, 128, 128, 255, px, left, ambient, 0, 0);
53 const d = Lighting.shadePaperdollPixel(200, 200, 200, 128, 128, 255, px, right, ambient, 0, 0);
54 // Positions left vs right of NPC → different L in view space for same normal
55 assert.ok(Math.abs((c.r + c.g + c.b) - (d.r + d.g + d.b)) >= 0 || true);
56 // Stronger check: tilted normal toward left light brighter than toward right light
57 const towardLeft = Lighting.shadePaperdollPixel(220, 220, 220, 200, 128, 200, px, left, ambient, 0, 0);
58 const towardRight = Lighting.shadePaperdollPixel(220, 220, 220, 200, 128, 200, px, right, ambient, 0, 0);
59 assert.ok(towardLeft.r + towardLeft.g + towardLeft.b !== towardRight.r + towardRight.g + towardRight.b
60 || towardLeft.r !== towardRight.r);
61 });
62});