forked from takahirox/aframe-outline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (58 loc) · 1.74 KB
/
index.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
/**
* @author Takahiro / https://github.com/takahirox
*/
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before' +
'AFRAME was available.');
}
require('three/examples/js/effects/OutlineEffect');
AFRAME.registerComponent('outline', {
schema: {
thickness: {type:'number', default: 0.003},
color: {type:'color', default: '#000'},
alpha: {type:'number', default: 1.0}
},
init: function () {
this.effect = null;
},
update: function () {
this.setupOutlineEffect();
},
remove: function () {
this.disableOutlineEffect();
},
setupOutlineEffect: function () {
if (this.effect !== null) {
this.effect.enabled = true;
return;
}
var data = this.data;
var el = this.el;
var sceneEl = el.sceneEl;
var renderer = sceneEl.renderer;
var effect = sceneEl.effect;
if (renderer === undefined || effect === undefined) { return; }
var outlineEffect = new THREE.OutlineEffect(renderer, {
defaultThickness: data.thickness,
defaultColor: new THREE.Color(data.color),
defaultAlpha: data.alpha
});
// override scene effect
// this's very sensitive to sceneEl impl
var keys = Object.keys(renderer);
for (var i = 0, il = keys.length; i < il; i++) {
var key = keys[i];
if (outlineEffect[key] === undefined) {
outlineEffect[key] = typeof renderer[key] === 'function'
? renderer[key].bind(renderer)
: renderer[key];
}
}
this.effect = outlineEffect;
sceneEl.effect = new THREE.VREffect(outlineEffect);
},
disableOutlineEffect: function () {
if (this.effect === null) { return; }
this.effect.enabled = false;
}
});