meshopt

    meshoptimizer's glTF buffer decoder (EXT_meshopt_compression) in MoonBit, decoding as meshoptimizer 1.3.0 does

    meshoptimizer
    gltf
    mesh
    decoder
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0 AND MIT
    Last updated
    5 hours ago
    Downloads
    2

    No dependencies

    #meshopt

    meshoptimizer's decoders in MoonBit: the buffers glTF compresses with EXT_meshopt_compression / KHR_meshopt_compression, decoded as meshoptimizer 1.3.0's MeshoptDecoder.decodeGltfBuffer 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/meshopt

    /// A buffer view of `count` elements of `size` bytes, or None if `source` is malformed.
    fn view(
    source : FixedArray[Byte],
    count : Int,
    size : Int,
    mode : @meshopt.Mode,
    filter : @meshopt.Filter,
    ) -> FixedArray[Byte]? {
    // Check before allocating: the counts come from an untrusted glTF.
    guard @meshopt.source_fits(count, size, source.length(), mode, filter) else {
    return None
    }
    let target = FixedArray::make(count * size, b'\x00')
    let ok = @meshopt.decode_gltf_buffer(
    target,
    count,
    size,
    source,
    0,
    source.length(),
    mode,
    filter,
    )
    if ok { Some(target) } else { None }
    }

    • Mode: the extension's mode, Attributes, Triangles or Indices.
    • Filter: its filter, NoFilter ("NONE"), Octahedral, Quaternion, Exponential or Color.
    • decode_gltf_buffer returns false where meshoptimizer throws or asserts; the target is then partly written.

    There is no encoder.

    #Matching meshoptimizer

    The repository compares the decoder with meshoptimizer 1.3.0's MeshoptDecoder (meshopt:golden): no difference on the js and wasm-gc backends.

    #License

    Apache-2.0 for the port; meshoptimizer is under the MIT License (see NOTICE).

    Filter

    pub(all) enum Filter {
    NoFilter
    Octahedral
    Quaternion
    Exponential
    Color
    }

    A compressed buffer view's filter, applied to decoded attributes: NoFilter ("NONE"), Octahedral, Quaternion, Exponential and Color.

    Mode

    pub(all) enum Mode {
    Attributes
    Triangles
    Indices
    }

    A compressed buffer view's mode (the extension's strings): Attributes the vertex codec, Triangles the index codec, Indices the index sequence codec.

    decode_gltf_buffer

    fn decode_gltf_buffer(target : FixedArray[Byte], count : Int, size : Int, source : FixedArray[Byte], source_offset : Int, source_length : Int, mode : Mode, filter : Filter) -> Bool

    MeshoptDecoder.decodeGltfBuffer: count elements of size bytes from source[source_offset:source_offset + source_length] into the start of target, then filter. False where meshoptimizer throws or asserts (malformed or too short); target is then partly written.

    source_fits

    fn source_fits(count : Int, size : Int, source_length : Int, mode : Mode, filter : Filter) -> Bool

    Whether source_length bytes could hold count elements of size bytes in mode and filter: callers check it before making room for the output.