@@ -1,4 +1,5 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
import { Buffer } from "node:buffer";
|
||||
|
||||
for (const path of ["/", "/deep/nested/3d/"]) {
|
||||
test("inspects and transforms locally at " + path, async ({ page }) => {
|
||||
@@ -21,6 +22,22 @@ for (const path of ["/", "/deep/nested/3d/"]) {
|
||||
});
|
||||
}
|
||||
|
||||
test("parses and flattens glTF scene transforms in the worker", async ({
|
||||
page,
|
||||
}) => {
|
||||
await page.goto("/");
|
||||
await page.locator('input[type="file"]').setInputFiles({
|
||||
name: "translated.gltf",
|
||||
mimeType: "model/gltf+json",
|
||||
buffer: gltfFixture(),
|
||||
});
|
||||
await expect(page.locator("p.status")).toContainText("glTF parsed locally");
|
||||
await expect(
|
||||
page.getByRole("heading", { name: "translated.gltf" }),
|
||||
).toBeVisible();
|
||||
await expect(page.getByRole("row", { name: /X 10 12 2/u })).toBeVisible();
|
||||
});
|
||||
|
||||
test("keeps the installed application available offline", async ({
|
||||
page,
|
||||
context,
|
||||
@@ -40,3 +57,43 @@ test("keeps the installed application available offline", async ({
|
||||
await context.setOffline(false);
|
||||
}
|
||||
});
|
||||
|
||||
function gltfFixture(): Buffer {
|
||||
const binary = Buffer.alloc(42);
|
||||
const view = new DataView(
|
||||
binary.buffer,
|
||||
binary.byteOffset,
|
||||
binary.byteLength,
|
||||
);
|
||||
[0, 0, 0, 1, 0, 0, 0, 1, 0].forEach((value, index) =>
|
||||
view.setFloat32(index * 4, value, true),
|
||||
);
|
||||
view.setUint16(36, 0, true);
|
||||
view.setUint16(38, 1, true);
|
||||
view.setUint16(40, 2, true);
|
||||
const document = {
|
||||
asset: { version: "2.0" },
|
||||
scene: 0,
|
||||
scenes: [{ nodes: [0] }],
|
||||
buffers: [
|
||||
{
|
||||
uri: `data:application/octet-stream;base64,${binary.toString("base64")}`,
|
||||
byteLength: binary.length,
|
||||
},
|
||||
],
|
||||
bufferViews: [
|
||||
{ buffer: 0, byteOffset: 0, byteLength: 36 },
|
||||
{ buffer: 0, byteOffset: 36, byteLength: 6 },
|
||||
],
|
||||
accessors: [
|
||||
{ bufferView: 0, componentType: 5126, count: 3, type: "VEC3" },
|
||||
{ bufferView: 1, componentType: 5123, count: 3, type: "SCALAR" },
|
||||
],
|
||||
meshes: [{ primitives: [{ attributes: { POSITION: 0 }, indices: 1 }] }],
|
||||
nodes: [
|
||||
{ name: "Parent", translation: [10, 0, 0], children: [1] },
|
||||
{ name: "Scaled", mesh: 0, scale: [2, 1, 1] },
|
||||
],
|
||||
};
|
||||
return Buffer.from(JSON.stringify(document));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
|
||||
test("keeps the primary workspace inside a narrow viewport", async ({
|
||||
page,
|
||||
}) => {
|
||||
await page.goto("/deep/nested/3d/");
|
||||
await expect(page.locator("main").first()).toBeVisible();
|
||||
await expect(
|
||||
page.locator("main .loading, main .workbench-loading"),
|
||||
).toHaveCount(0);
|
||||
|
||||
const widths = await page.evaluate(() => ({
|
||||
content: document.documentElement.scrollWidth,
|
||||
viewport: document.documentElement.clientWidth,
|
||||
}));
|
||||
expect(widths.viewport).toBeLessThanOrEqual(430);
|
||||
expect(widths.content).toBeLessThanOrEqual(widths.viewport + 1);
|
||||
});
|
||||
@@ -2,9 +2,11 @@ import { bytesToBase64 } from "@add-ideas/toolbox-helpers";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
exportBinaryStl,
|
||||
exportAsciiPly,
|
||||
exportObj,
|
||||
modelStats,
|
||||
parseModel,
|
||||
removeDegenerateTriangles,
|
||||
transformModel,
|
||||
} from "../../src/core/model";
|
||||
|
||||
@@ -115,11 +117,76 @@ describe("bounded 3D parsing", () => {
|
||||
);
|
||||
expect(model.indices).toEqual(Uint32Array.of(0, 1, 2));
|
||||
expect(model.materials).toEqual(["Local"]);
|
||||
expect(model.materialDetails?.[0]).toMatchObject({
|
||||
name: "Local",
|
||||
baseColorFactor: [0.2, 0.4, 0.6, 1],
|
||||
metallicFactor: 0.25,
|
||||
roughnessFactor: 0.7,
|
||||
alphaMode: "OPAQUE",
|
||||
});
|
||||
|
||||
const glb = makeGlb(gltfDocument(undefined, buffer.length), buffer);
|
||||
expect(parseModel("triangle.glb", glb, "GLB").indices).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("flattens the selected glTF scene hierarchy and mesh instances", () => {
|
||||
const buffer = gltfBuffer();
|
||||
const document = gltfDocument(
|
||||
"data:application/octet-stream;base64," + bytesToBase64(buffer),
|
||||
buffer.length,
|
||||
);
|
||||
Object.assign(document, {
|
||||
scene: 0,
|
||||
scenes: [{ nodes: [0, 2] }, { nodes: [3] }],
|
||||
nodes: [
|
||||
{ name: "Parent", translation: [10, 0, 0], children: [1] },
|
||||
{ name: "Scaled child", mesh: 0, scale: [2, 1, 1] },
|
||||
{ name: "Second instance", mesh: 0, translation: [0, 5, 0] },
|
||||
{ name: "Other scene", mesh: 0, translation: [100, 0, 0] },
|
||||
],
|
||||
});
|
||||
const model = parseModel(
|
||||
"scene.gltf",
|
||||
encode(JSON.stringify(document)),
|
||||
"glTF",
|
||||
);
|
||||
const stats = modelStats(model);
|
||||
expect(stats).toMatchObject({ vertices: 6, triangles: 2, nodes: 3 });
|
||||
expect(stats.bounds.min).toEqual([0, 0, 0]);
|
||||
expect(stats.bounds.max).toEqual([12, 6, 0]);
|
||||
expect(stats.surfaceArea).toBeCloseTo(1.5);
|
||||
expect(model.nodes).toEqual(["Parent", "Scaled child", "Second instance"]);
|
||||
});
|
||||
|
||||
it("applies glTF matrices and repairs mirrored winding", () => {
|
||||
const buffer = gltfBuffer();
|
||||
const document = gltfDocument(
|
||||
"data:application/octet-stream;base64," + bytesToBase64(buffer),
|
||||
buffer.length,
|
||||
);
|
||||
Object.assign(document, {
|
||||
scene: 0,
|
||||
scenes: [{ nodes: [0] }],
|
||||
nodes: [
|
||||
{
|
||||
mesh: 0,
|
||||
matrix: [-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 3, 0, 0, 1],
|
||||
},
|
||||
],
|
||||
});
|
||||
const model = parseModel(
|
||||
"mirror.gltf",
|
||||
encode(JSON.stringify(document)),
|
||||
"glTF",
|
||||
);
|
||||
expect(model.indices).toEqual(Uint32Array.of(0, 2, 1));
|
||||
expect(modelStats(model).bounds).toMatchObject({
|
||||
min: [2, 0, 0],
|
||||
max: [3, 1, 0],
|
||||
});
|
||||
expect(model.normals[2]).toBeCloseTo(1);
|
||||
});
|
||||
|
||||
it("rejects remote glTF buffers and safely transforms normals/winding", () => {
|
||||
const remote = gltfDocument("https://example.invalid/model.bin", 42);
|
||||
expect(() =>
|
||||
@@ -137,6 +204,27 @@ describe("bounded 3D parsing", () => {
|
||||
});
|
||||
expect(modelStats(transformed).surfaceArea).toBeCloseTo(2);
|
||||
expect(transformed.indices).toEqual(Uint32Array.of(0, 2, 1));
|
||||
const nonUniform = transformModel(model, {
|
||||
translate: [0, 0, 0],
|
||||
rotate: [0, 0, 0],
|
||||
scale: [2, 3, 1],
|
||||
});
|
||||
expect(modelStats(nonUniform).surfaceArea).toBeCloseTo(3);
|
||||
});
|
||||
|
||||
it("removes only degenerate triangles and exports bounded ASCII PLY", () => {
|
||||
const model = parseModel(
|
||||
"repair.obj",
|
||||
encode("v 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\nf 1 1 2\n"),
|
||||
"OBJ",
|
||||
);
|
||||
expect(modelStats(model).degenerateTriangles).toBe(1);
|
||||
const repaired = removeDegenerateTriangles(model);
|
||||
expect(modelStats(repaired)).toMatchObject({
|
||||
triangles: 1,
|
||||
degenerateTriangles: 0,
|
||||
});
|
||||
expect(exportAsciiPly(repaired)).toContain("element face 1");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -164,7 +252,16 @@ function gltfDocument(uri: string | undefined, length: number) {
|
||||
{ bufferView: 0, componentType: 5126, count: 3, type: "VEC3" },
|
||||
{ bufferView: 1, componentType: 5123, count: 3, type: "SCALAR" },
|
||||
],
|
||||
materials: [{ name: "Local" }],
|
||||
materials: [
|
||||
{
|
||||
name: "Local",
|
||||
pbrMetallicRoughness: {
|
||||
baseColorFactor: [0.2, 0.4, 0.6, 1],
|
||||
metallicFactor: 0.25,
|
||||
roughnessFactor: 0.7,
|
||||
},
|
||||
},
|
||||
],
|
||||
meshes: [
|
||||
{
|
||||
primitives: [{ attributes: { POSITION: 0 }, indices: 1, material: 0 }],
|
||||
|
||||
Reference in New Issue
Block a user