mirror of
https://github.com/IgorAherne/Spellcast3d.git
synced 2026-12-01 08:06:27 -10:00
106 lines
5.9 KiB
JavaScript
106 lines
5.9 KiB
JavaScript
// The Omni-Texture tools' own parts (static/js/stages/maskbar.js): what counts as masked, the square "Render Masked"
|
|
// sends and the camera it sees through (the viewer's lens, bent or not), the steps around the one shown, and how hand
|
|
// paint is saved. node --test code/tests/web/maskbar.test.mjs
|
|
import assert from 'node:assert/strict';
|
|
import { registerHooks } from 'node:module';
|
|
import { test } from 'node:test';
|
|
|
|
// The page's import map (index.html), for Node.
|
|
const STATIC = new URL('../../spellcast/static/', import.meta.url);
|
|
registerHooks({
|
|
resolve(specifier, context, next) {
|
|
if (specifier === 'three') return { url: new URL('vendor/three/build/three.module.js', STATIC).href, shortCircuit: true };
|
|
if (specifier.startsWith('three/addons/')) {
|
|
return { url: new URL(`vendor/three/examples/jsm/${specifier.slice('three/addons/'.length)}`, STATIC).href, shortCircuit: true };
|
|
}
|
|
return next(specifier, context);
|
|
},
|
|
});
|
|
globalThis.addEventListener ??= () => {};
|
|
globalThis.document ??= { addEventListener() {}, removeEventListener() {} };
|
|
const THREE = await import('three');
|
|
const { LensCamera } = await import(new URL('js/viewer3d.js', STATIC));
|
|
const {
|
|
cameraOf, compactMask, maskOf, perspectiveOf, saveHandPaint, squareShare, stepsAround,
|
|
} = await import(new URL('js/stages/maskbar.js', STATIC));
|
|
|
|
const near = (a, b) => Math.abs(a - b) < 1e-12;
|
|
|
|
test('the mask is where the overlay covers at least half way', () => {
|
|
// painted whole, an edge texel past half way, one just at half way, one under it, untouched
|
|
const { mask, count } = maskOf(new Uint8Array([255, 200, 128, 127, 0]));
|
|
assert.deepEqual([...mask], [255, 255, 255, 0, 0]);
|
|
assert.equal(count, 3);
|
|
});
|
|
|
|
test('the mask of a texture larger than 2048 is sent at 2048, masked where any of its texels is', () => {
|
|
const small = new Uint8Array(2048 * 2048);
|
|
small[5] = 255;
|
|
assert.deepEqual(compactMask(small, 2048), [small, 2048]); // as it is: nothing to halve
|
|
const large = new Uint8Array(4096 * 4096);
|
|
large[0] = 255; // the first texel alone
|
|
large[4096 * 3 + 7] = 255; // row 3, column 7: the pixel at row 1, column 3
|
|
const [mask, size] = compactMask(large, 4096);
|
|
assert.equal(size, 2048);
|
|
assert.equal(mask.length, 2048 * 2048);
|
|
const masked = [];
|
|
mask.forEach((value, at) => { if (value) masked.push([Math.floor(at / 2048), at % 2048, value]); });
|
|
assert.deepEqual(masked, [[0, 0, 255], [1, 3, 255]]);
|
|
assert.equal(compactMask(new Uint8Array(8192 * 8192 / 64), 1024)[1], 1024);
|
|
});
|
|
|
|
test('the square crop of the viewport is as high as its shorter side', () => {
|
|
assert.equal(squareShare(1600, 900), 1); // wide: the full height
|
|
assert.equal(squareShare(1050, 1350), 1050 / 1350); // tall: the width's share of it
|
|
});
|
|
|
|
test('"Inverse perspective": off is a camera\'s own view; on, its slider goes through no perspective at a quarter to inverse', () => {
|
|
assert.deepEqual([0, 0.25, 0.5, 1].map((amount) => perspectiveOf({ on: true, amount })), [1, 0, -1, -3]);
|
|
assert.equal(perspectiveOf({ on: false, amount: 1 }), 1);
|
|
});
|
|
|
|
test('the camera "Render Masked" sends is the viewport\'s square through the viewer\'s lens, in the model\'s own space', () => {
|
|
const camera = new LensCamera(32, 16 / 9, 0.01, 100);
|
|
camera.position.set(0, 0, 4); // looking at the point it turns about, the origin
|
|
const model = new THREE.Group();
|
|
model.scale.setScalar(2); // (the model's own units are half the viewer's)
|
|
const viewer = { camera, current: new THREE.Group().add(model), renderer: { domElement: { clientWidth: 1600, clientHeight: 900 } } };
|
|
const half = Math.tan((32 * Math.PI) / 360);
|
|
const plain = cameraOf(viewer);
|
|
assert.deepEqual(Object.keys(plain), ['eye', 'forward', 'up', 'tan'], 'a camera sends no lens');
|
|
assert.deepEqual([plain.eye, plain.forward, plain.up].map((v) => v.map((n) => n + 0)), [[0, 0, 2], [0, 0, -1], [0, 1, 0]]);
|
|
assert.ok(near(plain.tan, half));
|
|
// Bent: half the square's side at depth z is wide + tan * z, and what lies at the point orbited (depth 2 in the
|
|
// model's units) keeps the size the camera gave it.
|
|
camera.perspective = perspectiveOf({ on: true, amount: 0.5 });
|
|
const bent = cameraOf(viewer);
|
|
assert.ok(near(bent.tan, -half) && near(bent.wide, half * 4));
|
|
assert.ok(near(bent.wide + bent.tan * 2, plain.tan * 2));
|
|
// A tall viewport: the square is its width's share of the height, lens and all.
|
|
viewer.renderer.domElement = { clientWidth: 900, clientHeight: 1800 };
|
|
const tall = cameraOf(viewer);
|
|
assert.ok(near(tall.tan, -half / 2) && near(tall.wide, half * 2));
|
|
});
|
|
|
|
test('the steps before and after the model shown', () => {
|
|
const steps = ['a', 'b', 'c'];
|
|
assert.deepEqual(stepsAround(steps, 'b'), { index: 1, before: 'a', after: 'c' });
|
|
assert.deepEqual(stepsAround(steps, 'a'), { index: 0, before: null, after: 'b' });
|
|
assert.deepEqual(stepsAround(steps, 'c'), { index: 2, before: 'b', after: null });
|
|
assert.deepEqual(stepsAround(undefined, 'a'), { index: -1, before: null, after: null });
|
|
});
|
|
|
|
test('hand paint is saved as its patch into the step painted, and nothing is sent without one', async () => {
|
|
const sent = [];
|
|
globalThis.fetch = async (path, init) => {
|
|
sent.push([path, init.method, init.body]);
|
|
return { status: 200, text: async () => '{"id": "p_1"}' };
|
|
};
|
|
const step = { project: 'Demo', propId: 'p_1', glb: 'omnitexture/ot_001/retouch_002/omnitexture.glb' };
|
|
assert.equal(await saveHandPaint(step, { width: 2, height: 2, data: new Uint8Array(16), patch: null }), null);
|
|
assert.equal(sent.length, 0);
|
|
const patch = new Uint8Array(16).fill(255, 0, 4);
|
|
assert.deepEqual(await saveHandPaint(step, { width: 2, height: 2, data: new Uint8Array(16), patch }), { id: 'p_1' });
|
|
assert.deepEqual(sent, [['/api/projects/Demo/props/p_1/omnitexture/paint?glb=omnitexture%2Fot_001%2Fretouch_002%2Fomnitexture.glb&width=2&height=2', 'PUT', patch]]);
|
|
});
|