forked from c-frame/aframe-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a-tube.js
73 lines (65 loc) · 1.8 KB
/
a-tube.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Tube following a custom path.
*
* Usage:
*
* ```html
* <a-tube path="5 0 5, 5 0 -5, -5 0 -5" radius="0.5"></a-tube>
* ```
*/
var Primitive = module.exports.Primitive = {
defaultComponents: {
tube: {},
},
mappings: {
path: 'tube.path',
segments: 'tube.segments',
radius: 'tube.radius',
radialSegments: 'tube.radialSegments',
closed: 'tube.closed'
}
};
var Component = module.exports.Component = {
schema: {
path: {default: []},
segments: {default: 64},
radius: {default: 1},
radialSegments: {default: 8},
closed: {default: false}
},
init: function () {
var el = this.el,
data = this.data,
material = el.components.material;
if (!data.path.length) {
console.error('[a-tube] `path` property expected but not found.');
return;
}
var curve = new THREE.CatmullRomCurve3(data.path.map(function (point) {
point = point.split(' ');
return new THREE.Vector3(Number(point[0]), Number(point[1]), Number(point[2]));
}));
var geometry = new THREE.TubeGeometry(
curve, data.segments, data.radius, data.radialSegments, data.closed
);
if (!material) {
material = {};
material.material = new THREE.MeshPhongMaterial();
}
this.mesh = new THREE.Mesh(geometry, material.material);
this.el.setObject3D('mesh', this.mesh);
},
remove: function () {
if (this.mesh) this.el.removeObject3D('mesh');
}
};
module.exports.registerAll = (function () {
var registered = false;
return function (AFRAME) {
if (registered) return;
AFRAME = AFRAME || window.AFRAME;
AFRAME.registerComponent('tube', Component);
AFRAME.registerPrimitive('a-tube', Primitive);
registered = true;
};
}());