@draco-rust/obj
v0.3.0
Published
Wavefront OBJ reading and writing in WebAssembly: meshes with normals, texture coordinates, groups and materials.
Maintainers
Readme
@draco-rust/obj
Reads and writes Wavefront OBJ in WebAssembly: positions, normals, texture coordinates, groups and material references. It is part of draco-rust and drives the converter.
~42 KiB gzip.
Usage
import init, { read_obj, write_obj } from "@draco-rust/obj";
await init();
const result = read_obj(objBytes);
if (!result.success) throw new Error(result.error);
const { positions, indices, normals, uvs } = result.meshes[0];
const written = write_obj({ positions, indices, normals, uvs }, {});
const obj = written.text; // the OBJ text; written.bytes holds it as UTF-8Polygons are triangulated. write_obj takes one mesh or an array of them, each
written as an object of its own.
On workers
A large file blocks the calling thread while it is read or written.
@draco-rust/obj/pool runs the same functions on workers:
import { createPool } from "@draco-rust/obj/pool";
const pool = createPool({ workers: 2 }); // default: cores - 1, at most 4
const result = await pool.read(bytes); // what read_obj returns
const written = await pool.write(meshes, {}); // what write_obj returns
pool.dispose();The workers start as work arrives, and the wasm is compiled once for all of
them. Inputs are copied to the worker, or handed over with { transfer: true };
{ signal } takes an AbortSignal, which rejects the promise at once. A task
a worker has started still runs to its end and its result is dropped. With
workers: 0, or where there are no workers, the pool runs on the calling
thread. It works on Web Workers, Node's worker threads (Node 20.16 and later)
and from a CDN, as the decoder's does.
For loading from a CDN or in Node, see the
decoder's README; the same
init options apply, with @draco-rust/obj/index_bg.wasm as the file.
Licensed under Apache-2.0.
This build
@draco-rust/[email protected] was built from draco-rust commit 4b72cfc8, with draco-core 2.3.2, draco-io 0.6.0, draco-gltf 0.7.0.
