BlueGit

timfa raytracer

Files

raytracer / test / compositor.test.js

compositor.test.js

compositor.test.js 1.8 KiB Download Raw
1"use strict";
2const { describe, it } = require("node:test");
3const assert = require("node:assert/strict");
4const PpdaCompositor = require("../src/ppda/compositor.js");
5
6function layer (w, h, gray, dAlpha, rgba)
7{
8 const n = w * h;
9 const diffuse = new Uint8ClampedArray(n * 4);
10 const depthGray = new Uint16Array(n);
11 const depthAlpha = new Uint16Array(n);
12 for (let i = 0; i < n; i++)
13 {
14 diffuse[i * 4] = rgba[0];
15 diffuse[i * 4 + 1] = rgba[1];
16 diffuse[i * 4 + 2] = rgba[2];
17 diffuse[i * 4 + 3] = rgba[3];
18 depthGray[i] = gray;
19 depthAlpha[i] = dAlpha;
20 }
21 return { width: w, height: h, diffuse, depthGray, depthAlpha };
22}
23
24describe("PpdaCompositor (compositor.rs rules)", () =>
25{
26 it("nearer depth wins", () =>
27 {
28 const body = layer(1, 1, 20000, 65535, [10, 0, 0, 255]);
29 const gear = layer(1, 1, 10000, 65535, [0, 20, 0, 255]);
30 const out = PpdaCompositor.compose([body, gear]);
31 assert.equal(out.winner[0], 1);
32 assert.equal(out.diffuse[1], 20);
33 assert.equal(out.depthGray[0], 10000);
34 });
35
36 it("tie within epsilon keeps lower layer (body)", () =>
37 {
38 const body = layer(1, 1, 20000, 65535, [10, 0, 0, 255]);
39 const gear = layer(1, 1, 20000 + 1, 65535, [0, 20, 0, 255]);
40 const out = PpdaCompositor.compose([body, gear]);
41 assert.equal(out.winner[0], 0);
42 assert.equal(out.diffuse[0], 10);
43 });
44
45 it("zero diffuse alpha skips layer", () =>
46 {
47 const body = layer(1, 1, 5000, 65535, [10, 0, 0, 255]);
48 const gear = layer(1, 1, 1000, 65535, [0, 20, 0, 0]);
49 const out = PpdaCompositor.compose([body, gear]);
50 assert.equal(out.winner[0], 0);
51 });
52
53 it("zero depth alpha skips layer", () =>
54 {
55 const body = layer(1, 1, 5000, 65535, [10, 0, 0, 255]);
56 const gear = layer(1, 1, 1000, 0, [0, 20, 0, 255]);
57 const out = PpdaCompositor.compose([body, gear]);
58 assert.equal(out.winner[0], 0);
59 });
60});