draco

    Draco mesh and point cloud decoder in MoonBit, decoding as draco3d 1.5.7 does

    draco
    gltf
    mesh
    point-cloud
    decoder
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    5 hours ago
    Downloads
    3

    No dependencies

    #draco

    A Draco decoder in MoonBit: meshes (as glTF's KHR_draco_mesh_compression stores them) and point clouds, decoded as Draco 1.5.7's decoder (the draco3d npm package) does. Pure MoonBit on every backend (wasm, wasm-gc, js, native), with no dependencies.

    Part of mbt-map, a WebGIS library, published on its own.

    moon add t-ujiie-g/draco

    /// The triangle count and the bytes of the attribute glTF names with `id`.
    fn read(bytes : FixedArray[Byte], id : Int) -> (Int, FixedArray[Byte])? {
    guard @draco.decode(bytes, 0, bytes.length()) is Some(mesh) else { return None }
    guard mesh.attribute(id) is Some(attribute) else { return None }
    Some((mesh.indices.length() / 3, attribute.data))
    }

    #What it gives

    • decode(src, offset, length): a DracoMesh, or None for data draco3d refuses, bitstreams before 2.0, and meshes past the caps (MAX_POINTS and the like: the input is treated as untrusted).
    • DracoMesh: indices (three point indices per face, none for a point cloud), num_points, attributes.
    • DracoAttribute: unique_id (what the glTF extension's attributes name), Draco's attribute_type (0 POSITION, 1 NORMAL, 2 COLOR, 3 TEX_COORD, 4 GENERIC) and data_type (1 INT8 … 9 FLOAT32, 10 FLOAT64, 11 BOOL), num_components, normalized, and data: little-endian values for every point, as draco3d's GetAttributeDataArrayForAllPoints gives them (quantized and octahedral attributes come out as FLOAT32).
    • DracoMesh::to_bytes(): everything in one buffer (a header of u32s, the indices, each attribute's data), the layout mbt-map's worker hands to JavaScript.

    Covered: sequential and edgebreaker meshes (standard, predictive and valence traversals), sequential and kd-tree point clouds, the prediction schemes and transforms, rANS. There is no encoder.

    #Matching draco3d

    The repository compares the decoder with draco3d 1.5.7 on seeded meshes and point clouds, Draco's own test files and PLATEAU's 3D tiles, plus corrupted copies (draco:golden): no difference on the js and wasm-gc backends.

    #License

    Apache-2.0, as Draco itself (see NOTICE).

    DracoAttribute

    pub struct DracoAttribute {
    unique_id : Int
    attribute_type : Int
    data_type : Int
    num_components : Int
    normalized : Bool
    data : FixedArray[Byte]
    }

    One attribute of a decoded mesh, its fields as mbt-map's FORMATS.md "Draco output" numbers them: unique_id (uint32 bits) what KHR_draco_mesh_compression's attributes name, Draco's attribute_type and data_type, and data (draco3d's GetAttributeDataArrayForAllPoints).

    DracoMesh

    pub struct DracoMesh {
    indices : FixedArray[Int]
    num_points : Int
    attributes : Array[DracoAttribute]
    }

    A decoded Draco mesh or point cloud: indices holds three point indices (uint32 bits) per face, as draco3d's GetTrianglesUInt32Array (none for a point cloud), and attributes every attribute in the order it was decoded.

    DracoMesh::attribute

    fn DracoMesh::attribute(self : DracoMesh, unique_id : Int) -> DracoAttribute?

    The first attribute whose unique id is unique_id, as draco3d's GetAttributeByUniqueId.

    DracoMesh::to_bytes

    fn DracoMesh::to_bytes(self : DracoMesh, max_bytes? : Int) -> FixedArray[Byte]?

    The mesh as mbt-map's FORMATS.md "Draco output" lays it out: a header of u32s, the indices, then each attribute's data padded to four bytes; None past max_bytes.

    MAX_KD_DIMENSIONS

    let MAX_KD_DIMENSIONS : Int

    The most dimensions (components of the attributes one kd-tree decoder holds) a kd-tree may have; Draco allocates 32 × dimensions + 1 stack levels of that many words up front.

    MAX_POINTS

    let MAX_POINTS : Int

    The most points a mesh or point cloud may have.

    decode

    fn decode(src : FixedArray[Byte], offset : Int, length : Int) -> DracoMesh?

    Decodes the Draco mesh or point cloud (no faces) in src[offset, offset + length) as its header says, as draco3d 1.5.7's DecodeArrayToMesh / DecodeArrayToPointCloud do; None where draco3d fails, before bitstream 2.0 and past the MAX_* caps (mbt-map's FORMATS.md "Draco output").