-
Notifications
You must be signed in to change notification settings - Fork 304
/
gltf-model-legacy.js
52 lines (43 loc) · 1.28 KB
/
gltf-model-legacy.js
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
const fetchScript = require('../../lib/fetch-script')();
const LOADER_SRC = 'https://cdn.jsdelivr.net/gh/mrdoob/three.js@r86/examples/js/loaders/GLTFLoader.js';
const loadLoader = (function () {
let promise;
return function () {
promise = promise || fetchScript(LOADER_SRC);
return promise;
};
}());
/**
* Legacy loader for glTF 1.0 models.
* Asynchronously loads THREE.GLTFLoader from jsdelivr.
*/
module.exports = AFRAME.registerComponent('gltf-model-legacy', {
schema: {type: 'model'},
init: function () {
this.model = null;
this.loader = null;
this.loaderPromise = loadLoader().then(() => {
this.loader = new THREE.GLTFLoader();
this.loader.setCrossOrigin('Anonymous');
});
},
update: function () {
const self = this;
const el = this.el;
const src = this.data;
if (!src) { return; }
this.remove();
this.loaderPromise.then(() => {
this.loader.load(src, function gltfLoaded (gltfModel) {
self.model = gltfModel.scene;
self.model.animations = gltfModel.animations;
el.setObject3D('mesh', self.model);
el.emit('model-loaded', {format: 'gltf', model: self.model});
});
});
},
remove: function () {
if (!this.model) { return; }
this.el.removeObject3D('mesh');
}
});