Files
workinf_Blender_Wasm/tools/web/probe-topology-collapse.mjs
2026-08-12 04:47:48 -04:00

37 lines
2.3 KiB
JavaScript

import fs from "node:fs";
import factory from "../../web/app/src/vendor/blender/web_engine.js";
const wasmBinary = fs.readFileSync(new URL("../../web/app/src/vendor/blender/web_engine.wasm", import.meta.url));
const blend = fs.readFileSync(new URL("../../tests/files/web/topology_scene.blend", import.meta.url));
const engine = await factory({ wasmBinary });
const handle = engine._web_engine_create();
const encoder = new TextEncoder();
function call(name, bytes) {
const pointer = engine._malloc(bytes.byteLength);
engine.HEAPU8.set(bytes, pointer);
try { return engine[name](handle, pointer, bytes.byteLength); }
finally { engine._free(pointer); }
}
function snapshot() {
const dataOut = engine._malloc(4), lengthOut = engine._malloc(4);
try {
if (engine._web_engine_get_scene_snapshot(handle, dataOut, lengthOut) !== 0) throw new Error("snapshot failed");
const pointer = engine.HEAPU32[dataOut >>> 2], length = engine.HEAPU32[lengthOut >>> 2];
return JSON.parse(new TextDecoder().decode(engine.HEAPU8.slice(pointer, pointer + length)));
} finally { engine._free(dataOut); engine._free(lengthOut); }
}
const openBytes = new Uint8Array(blend);
if (call("_web_engine_open_blend", openBytes) !== 0) throw new Error(engine.UTF8ToString(engine._web_engine_last_error_message()));
const source = snapshot();
const results = [];
for (const mesh of source.meshes.filter((candidate) => candidate.geometryStatus === "available")) {
if (call("_web_engine_open_blend", openBytes) !== 0) throw new Error(engine.UTF8ToString(engine._web_engine_last_error_message()));
const current = snapshot();
const currentCommand = encoder.encode(JSON.stringify({ type: "decimateMesh", meshId: mesh.id, profile: { schemaVersion: 1, sourceMeshRevision: current.revision, attributePolicy: "PRESERVE", mode: "COLLAPSE", ratio: 0.5, triangulate: false, useSymmetry: false } }));
const result = call("_web_engine_apply_command", currentCommand);
const output = result === 0 ? snapshot().meshes.find((candidate) => candidate.id === mesh.id) : undefined;
results.push({ meshId: mesh.id, result, message: engine.UTF8ToString(engine._web_engine_last_error_message()), vertices: output?.vertexCount ?? 0, triangles: output?.triangleCount ?? 0 });
}
console.log(JSON.stringify(results));
engine._web_engine_destroy(handle);