Skip to main content

Module u3d

Module u3d 

Source
Expand description

A U3D (ECMA-363) writer — the 3D stream a PDF /3D annotation carries.

Hand-rolled, no crate, wasm-clean, like the PDF writer beside it. It writes exactly what a 3D PDF of a part needs and nothing else: MODEL nodes, each owning either a triangle MESH (a CLOD mesh generator whose base mesh is the whole mesh — no progressive resolution updates) or a LINE SET, one lit texture shader and one material per colour, and a shading modifier per node choosing them. No lights, cameras, textures, bones or animation: the PDF’s own view dictionaries carry the cameras and the lighting scheme.

§The bit encoder

Every block payload goes through the U3D bitstream (ECMA-363 §10): a 16-bit arithmetic coder with static (uniform) and dynamic (adaptive) contexts. Until the first compressed value, an uncompressed U8 leaves the coder in its initial state and is emitted as its own eight bits, so a block holding only uncompressed fields is plain little-endian bytes. BitWriter follows the Intel reference implementation (CIFXBitStreamX, Apache-2.0) operation for operation — the order of the renormalisation steps, when a histogram halves, which contexts a field shares — because the reader on the other end of a PDF is descended from that code, and an arithmetic coder that is “equivalent” but not identical decodes to garbage. BitReader is the matching decoder, kept for the round-trip tests.

§The file

The block order is the reference converter’s own for the same content: the file header, a priority update, a NODE modifier chain per model (the model node and its shading modifier), a MODEL RESOURCE chain per model (the generator’s declaration, and — for a line set — its continuation), the shaders, the materials, a second priority update, then each mesh’s base-mesh continuation.

Structs§

Lines
Line segments between positions, each with its Model::shaders index.
Material
A surface colour: one lit texture shader and one material in the file.
Mesh
A triangle mesh. normals[i] is the normal at positions[i]; each triangle carries the index of the Model::shaders entry that colours it.
Model
One model node and the resource it shows. name is the node name a PDF /3DNode dictionary refers to; the resource is named {name}.res.
Scene

Enums§

Geometry

Functions§

validate
Why a scene cannot be written.
write
Serialise scene as a U3D file.