File size: 2,523 Bytes
0e2d59b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader'
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader'
import { GLTF, GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import * as THREE from 'three'

const fbxLoader = new FBXLoader()
export async function LoadFBXFile(
    url: string,
    onLoading?: (loaded: number) => void
): Promise<THREE.Group> {
    return new Promise((resolve, reject) => {
        // load a resource
        fbxLoader.load(
            // resource URL
            url,
            // called when resource is loaded
            function (object) {
                resolve(object)
            },
            // called when loading is in progresses
            function (xhr) {
                console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
                onLoading?.((xhr.loaded / xhr.total) * 100)
            },
            // called when loading has errors
            function (error) {
                console.log('An error happened')
                reject(error)
            }
        )
    })
}

// instantiate a loader
const loader = new OBJLoader()

export async function LoadObjFile(url: string): Promise<THREE.Group> {
    return new Promise((resolve, reject) => {
        // load a resource
        loader.load(
            // resource URL
            url,
            // called when resource is loaded
            function (object) {
                resolve(object)
            },
            // called when loading is in progresses
            function (xhr) {
                console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
            },
            // called when loading has errors
            function (error) {
                console.log('An error happened')
                reject(error)
            }
        )
    })
}

export async function LoadGLTFile(url: string): Promise<GLTF> {
    return new Promise((resolve, reject) => {
        // load a resource
        new GLTFLoader().load(
            // resource URL
            url,
            // called when resource is loaded
            function (object) {
                resolve(object)
            },
            // called when loading is in progresses
            function (xhr) {
                console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
            },
            // called when loading has errors
            function (error) {
                console.log('An error happened')
                reject(error)
            }
        )
    })
}