-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
42 lines (40 loc) · 1.28 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
/* global AFRAME */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
/**
* Viewable component for A-Frame.
*/
AFRAME.registerComponent('viewable', {
dependencies: ['rotation'],
schema: {
maxpitch: {default: null},
maxyaw: {default: null}
},
init: function () {
// Convert max pitch/yaw degrees to radians for easier evaluation against object3D values
this.maxPitchRadians = (this.data.maxpitch ? this.degreesToRadian(this.data.maxpitch) : null);
this.maxYawRadians = (this.data.maxpitch ? this.degreesToRadian(this.data.maxyaw) : null);
},
tick: function (t) {
this.update();
},
update: function () {
if(this.maxPitchRadians){
this.fixAxisToMaximum('y', this.maxPitchRadians);
}
if(this.maxYawRadians){
this.fixAxisToMaximum('x', this.maxYawRadians);
}
},
fixAxisToMaximum: function (axis, maximum) {
// If user has tried to move beyond the maximum for this axis
// Reset to the maximum (positive/negative)
if(Math.abs(this.el.object3D.rotation[axis]) > maximum){
this.el.object3D.rotation[axis] = (this.el.object3D.rotation[axis]< 0 ? -maximum : maximum)
}
},
degreesToRadian(d){
return (d * Math.PI) / 180;
}
});