-
Notifications
You must be signed in to change notification settings - Fork 2
/
led-component.js
82 lines (68 loc) · 2.01 KB
/
led-component.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
74
75
76
77
78
79
80
81
82
/**
* a-frame LED component
*
* Copyright (c) 2020 Uri Shaked
*
* Released under the MIT license.
*/
(function () {
const loader = new THREE.TextureLoader();
const ledTexture = loader.load('textures/led.png');
const glowTexture = loader.load('textures/led-glow.png');
AFRAME.registerComponent('led', {
schema: {
color: { type: 'color', default: '#000' },
},
init() {
const { data, el } = this;
this.material = new THREE.SpriteMaterial({
map: ledTexture,
color: new THREE.Color(0x808080),
depthFunc: THREE.AlwaysDepth,
});
this.sphereMaterial = new THREE.MeshBasicMaterial({ color: data.color });
this.glowMaterial = new THREE.SpriteMaterial({
map: glowTexture,
color: new THREE.Color(data.color),
blending: THREE.AdditiveBlending,
depthFunc: THREE.AlwaysDepth,
});
const led = new THREE.Sprite(this.material);
const geometry = new THREE.SphereGeometry(0.3, 32, 32);
const sphere = new THREE.Mesh(geometry, this.sphereMaterial);
const glow = new THREE.Sprite(this.glowMaterial);
glow.scale.set(2, 2, 2);
this.mesh = new THREE.Group();
this.mesh.scale.set(0.1, 0.1, 0.1);
this.mesh.add(sphere);
this.mesh.add(led);
this.mesh.add(glow);
el.setObject3D('mesh', this.mesh);
},
update(oldData) {
var data = this.data;
if (Object.keys(oldData).length === 0) {
return;
}
if (data.color !== oldData.color) {
const baseColor = new THREE.Color(data.color);
const { h, s, l } = baseColor.getHSL({});
this.sphereMaterial.color = baseColor;
this.material.opacity = 0.2 + 0.8 * l;
this.glowMaterial.color.setHSL(h, s, 0.7);
this.glowMaterial.opacity = l;
}
},
remove() {
this.el.removeObject3D('mesh');
},
});
AFRAME.registerPrimitive('a-led', {
defaultComponents: {
led: {},
},
mappings: {
color: 'led.color',
},
});
})();