-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (93 loc) · 2.54 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
mostly copied from
https://stackoverflow.com/questions/41968142/aframe-emitting-custom-events-between-components
*/
/* global AFRAME */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
var lastPos = {x: 0, y:0, z:0};
var lastRot = {x: 0, y:0, z:0};
/* look for significant rotation, 0.1 degree or more
so as to not take up too much CPU power */
function diffPosRot(currentPos, currentRot, accuracy) {
var val = Object.keys(currentPos).map(function(key){
if (key) {
if (currentPos[key].toFixed(accuracy) != lastPos[key].toFixed(accuracy)) {
lastPos[key] = currentPos[key];
return true;
}
if (currentRot[key].toFixed(accuracy) != lastRot[key].toFixed(accuracy)) {
lastRot[key] = currentRot[key];
return true;
}
}
});
for (var i = 0; i < val.length; i++){
if (val[i] == true) {
return true;
}
}
return false;
/*
for (key in currentPos) {
if (currentRot[key].toFixed(1) == lastRot[key].toFixed(1)) {
return false;
}
}*/
}
/**
* Camera Listener component for A-Frame.
*/
AFRAME.registerComponent('camera-listener', {
schema: {
dataEl: {type: 'selector'},
/* maybe pos and rot are unnecessary as they can be
queried directly */
position: {type: 'vec3'},
rotation: {type: 'vec3'}
},
/**
* Set if component needs multiple instancing.
*/
multiple: false,
tick: function () {
var cameraEl = this.el.sceneEl.camera.el;
var pos = cameraEl.getAttribute('position');
var rot = cameraEl.getAttribute('rotation');
if (diffPosRot(pos, rot, 2)) {
this.data.dataEl.emit('camera-moved', {value: {position: pos, rotation: rot}});
}
},
/**
* Called once when component is attached. Generally for initial setup.
*/
init: function () {
this.data.dataEl = this.el;
},
/**
* Called when component is attached and when component data changes.
* Generally modifies the entity based on the data.
*/
updateSchema: function (d) {
},
/**
* Called when a component is removed (e.g., via removeAttribute).
* Generally undoes all modifications to the entity.
*/
remove: function () { },
/**
* Called on each scene tick.
*/
// tick: function (t) { },
/**
* Called when entity pauses.
* Use to stop or remove any dynamic or background behavior such as events.
*/
pause: function () { },
/**
* Called when entity resumes.
* Use to continue or add any dynamic or background behavior such as events.
*/
play: function () { }
});