Files
frontend/Client/tests/assembly-particle-flow.test.mjs

146 lines
4.8 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import * as THREE from "three";
import {
PARTICLE_REPLACEMENT_DURATION,
PARTICLE_REPLACEMENT_MAX_DELAY,
PARTICLE_REPLACEMENT_MOTION_END,
PARTICLE_REPLACEMENT_MOTION_START,
PARTICLE_REPLACEMENT_REVEAL_END,
PARTICLE_REPLACEMENT_SOURCE_FADE_END,
PARTICLE_REPLACEMENT_SOURCE_FADE_START,
PARTICLE_REPLACEMENT_TARGET_REVEAL_START,
createAssemblyParticleFlow,
getReplacementParticleOpacity,
getReplacementSourceOpacity,
getReplacementTargetOpacity,
} from "../src/scene/assembly-particle-flow.ts";
function attributeValues(geometry, name) {
return Array.from(geometry.getAttribute(name).array);
}
function assertSamplesOnBoxSurface(values, centerX = 0) {
for (let index = 0; index < values.length; index += 3) {
const x = values[index] - centerX;
const y = values[index + 1];
const z = values[index + 2];
assert.ok(Math.abs(x) <= 1.00001);
assert.ok(Math.abs(y) <= 1.00001);
assert.ok(Math.abs(z) <= 1.00001);
assert.ok(
[x, y, z].some((value) => Math.abs(Math.abs(value) - 1) < 0.00001),
);
}
}
function box(color, x = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(2, 2, 2),
new THREE.MeshBasicMaterial({ color }),
);
mesh.position.x = x;
mesh.updateWorldMatrix(true, true);
return mesh;
}
test("replacement flow maps old surface particles through a local cloud to the new surface", () => {
const flow = createAssemblyParticleFlow();
const source = box(0xff0000);
const target = box(0x0000ff, 5);
try {
assert.equal(
flow.setMorphTargets([source], [target], new THREE.Vector3(), 2),
true,
);
assert.equal(flow.points.material.uniforms.uDissolveMode.value, 1);
const oldSurface = attributeValues(flow.points.geometry, "aOrigin");
const scatter = attributeValues(flow.points.geometry, "aScatter");
const newSurface = attributeValues(flow.points.geometry, "position");
assertSamplesOnBoxSurface(oldSurface);
assertSamplesOnBoxSurface(newSurface, 5);
assert.ok(Math.abs(scatter[0]) < 2.5);
assert.ok(Math.abs(scatter[1]) < 2.5);
assert.ok(Math.abs(scatter[2]) < 2.5);
assert.ok(
Math.hypot(
scatter[0] - oldSurface[0],
scatter[1] - oldSurface[1],
scatter[2] - oldSurface[2],
) > 0.1,
);
assert.deepEqual(
attributeValues(flow.points.geometry, "aFromColor").slice(0, 3),
[1, 0, 0],
);
assert.deepEqual(
attributeValues(flow.points.geometry, "aToColor").slice(0, 3),
[0, 0, 1],
);
} finally {
flow.dispose();
source.geometry.dispose();
source.material.dispose();
target.geometry.dispose();
target.material.dispose();
}
});
test("addition and removal use the same morph flow without a two-stage restart", () => {
const flow = createAssemblyParticleFlow();
const source = box(0x44aa88);
const target = box(0x8844aa, 3);
try {
assert.equal(
flow.setMorphTargets([], [target], new THREE.Vector3(3, 0, 0), 2),
true,
);
assert.equal(flow.points.material.uniforms.uDissolveMode.value, 1);
assertSamplesOnBoxSurface(
attributeValues(flow.points.geometry, "position"),
3,
);
assert.equal(
flow.setMorphTargets([source], [], new THREE.Vector3(), 2),
true,
);
assert.equal(flow.points.material.uniforms.uDissolveMode.value, 1);
assertSamplesOnBoxSurface(attributeValues(flow.points.geometry, "aOrigin"));
assert.ok(
Math.hypot(
...attributeValues(flow.points.geometry, "position").slice(0, 3),
) > 2,
);
} finally {
flow.dispose();
source.geometry.dispose();
source.material.dispose();
target.geometry.dispose();
target.material.dispose();
}
});
test("replacement opacity and movement timing match the reference curve", () => {
assert.equal(PARTICLE_REPLACEMENT_DURATION, 6.5);
assert.equal(PARTICLE_REPLACEMENT_REVEAL_END, 0.08);
assert.equal(PARTICLE_REPLACEMENT_SOURCE_FADE_START, 0.08);
assert.equal(PARTICLE_REPLACEMENT_SOURCE_FADE_END, 0.28);
assert.equal(PARTICLE_REPLACEMENT_MOTION_START, 0.24);
assert.equal(PARTICLE_REPLACEMENT_MOTION_END, 0.84);
assert.equal(PARTICLE_REPLACEMENT_TARGET_REVEAL_START, 0.94);
assert.equal(PARTICLE_REPLACEMENT_MAX_DELAY, 0.08);
assert.equal(getReplacementSourceOpacity(0.08), 1);
assert.ok(Math.abs(getReplacementSourceOpacity(0.18) - 0.5) < 1e-9);
assert.equal(getReplacementSourceOpacity(0.28), 0);
assert.equal(getReplacementTargetOpacity(0.94), 0);
assert.equal(getReplacementTargetOpacity(1), 1);
assert.equal(getReplacementParticleOpacity(0), 0);
assert.ok(Math.abs(getReplacementParticleOpacity(0.04) - 0.5) < 1e-9);
assert.equal(getReplacementParticleOpacity(0.94), 1);
assert.equal(getReplacementParticleOpacity(1), 0);
});