MeshoptDecoder Memory Leak

As discussed in this meshoptimizer issue, and from my own findings, when using decodeGltfBufferAsync the memory allocated by the MeshoptDecoder does not get released.

Arseny’s solution for cleaning up the wasm memory is creating a web worker then terminating it once it has finished decoding the buffer.

Here is a suggestion to change meshoptCompression.ts which should help solve this issue.

public decodeGltfBufferAsync(source: Uint8Array, count: number, stride: number, mode: "ATTRIBUTES" | "TRIANGLES" | "INDICES", filter?: string): Promise<Uint8Array> {
    return this._decoderModulePromise!.then(async () => {
        MeshoptDecoder.useWorkers(1);
        const result = await MeshoptDecoder.decodeGltfBufferAsync(count, stride, source, mode, filter);
        MeshoptDecoder.useWorkers(0);
        return result;
    });
}

This would require updating the meshopt_decoder.js script on the Babylon CDN.

cc @bghgary

Do you want to create a PR?

Sure thing.

Fix memory leak in MeshoptCompression by OrigamiDev-Pete · Pull Request #14995 · BabylonJS/Babylon.js (github.com)