File size: 922 Bytes
5f32ba4
3b7903d
5f32ba4
 
3b7903d
 
 
 
 
 
 
 
 
 
 
1b5e137
3b7903d
 
 
 
 
 
1b5e137
 
 
 
 
3b7903d
1b5e137
ec3121c
3b7903d
 
5f32ba4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import type { Scene } from "../core/Scene";

class Loader {
    static async LoadAsync(url: string, scene: Scene, onProgress?: (progress: number) => void): Promise<void> {
        const req = await fetch(url, {
            mode: "cors",
            credentials: "omit",
        });

        if (req.status != 200) {
            throw new Error(req.status + " Unable to load " + req.url);
        }

        const reader = req.body!.getReader();
        const contentLength = parseInt(req.headers.get("content-length") as string);
        const data = new Uint8Array(contentLength);

        let bytesRead = 0;

        while (true) {
            const { done, value } = await reader.read();
            if (done) break;

            data.set(value, bytesRead);
            bytesRead += value.length;

            onProgress?.(bytesRead / contentLength);
        }

        scene.setData(data);
    }
}

export { Loader };