@draco-rust/encoder
v0.3.0
Published
Draco geometry encoder in WebAssembly, a safe Rust port of Google Draco: compresses meshes and point clouds to .drc.
Maintainers
Readme
@draco-rust/encoder
Encodes meshes and point clouds to Draco
(.drc) in WebAssembly. It is part of draco-rust,
a safe Rust port of Google's Draco, and with default options writes the same
bytes as Google's encoder. It is not affiliated with Google. To decode, use
@draco-rust/decoder.
~133 KiB gzip.
Usage
import init, { encode_draco } from "@draco-rust/encoder";
await init();
const result = encode_draco(
{ positions, indices, normals, uvs, colors }, // typed arrays; all but positions optional
{ position_bits: 14, normal_bits: 10, texcoord_bits: 12, encoding_speed: 5, decoding_speed: 5 },
);
if (!result.success) throw new Error(result.error);
const drc = result.bytes; // Uint8Arraypositions is a Float32Array of xyz triples and indices a Uint32Array of
triangle corners. Leave indices out, or set point_cloud: true, to write a
point cloud. Two options act on point clouds only:
prediction_searchchooses each attribute's prediction by its estimated cost.spatial_point_orderorders the points along a Hilbert curve.
Both write smaller files, at the cost of output no longer byte-identical to Google's encoder.
On workers
@draco-rust/encoder/pool encodes on workers, so a large mesh does not block
the calling thread:
import { createPool } from "@draco-rust/encoder/pool";
const pool = createPool({ workers: 2 });
const result = await pool.encode(mesh, { position_bits: 14 }); // what encode_draco 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/encoder/index_bg.wasm as the file.
Licensed under Apache-2.0, like Draco.
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.
