-
-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathobject-model.js
More file actions
53 lines (46 loc) · 1.42 KB
/
object-model.js
File metadata and controls
53 lines (46 loc) · 1.42 KB
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
/**
* object-model
*
* Loader for THREE.js JSON format. Somewhat confusingly, there are two different THREE.js formats,
* both having the .json extension. This loader supports only THREE.ObjectLoader, which typically
* includes multiple meshes or an entire scene.
*
* Check the console for errors, if in doubt. You may need to use `json-model` or
* `blend-character-model` for some .js and .json files.
*
* See: https://clara.io/learn/user-guide/data_exchange/threejs_export
*/
AFRAME.registerComponent('object-model', {
schema: {
src: { type: 'asset' },
crossorigin: { default: '' }
},
init: function () {
this.model = null;
},
update: function () {
let loader;
const data = this.data;
if (!data.src) return;
this.remove();
loader = new THREE.ObjectLoader();
if (data.crossorigin) loader.setCrossOrigin(data.crossorigin);
loader.load(data.src, (object) => {
// Enable skinning, if applicable.
object.traverse((o) => {
if (o instanceof THREE.SkinnedMesh && o.material) {
o.material.skinning = !!((o.geometry && o.geometry.bones) || []).length;
}
});
this.load(object);
});
},
load: function (model) {
this.model = model;
this.el.setObject3D('mesh', model);
this.el.emit('model-loaded', {format: 'json', model: model});
},
remove: function () {
if (this.model) this.el.removeObject3D('mesh');
}
});