-
Notifications
You must be signed in to change notification settings - Fork 1
/
aframe-spherical-controls-component.js
298 lines (256 loc) · 8.45 KB
/
aframe-spherical-controls-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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
/* global AFRAME */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
/**
* Spherical controls component for A-Frame.
*/
AFRAME.registerComponent('spherical-controls', {
schema: {
radius: {
type: 'number',
default: 1.1
},
minRadius: {
type: 'number',
default: 0
},
maxRadius: {
type: 'number',
default: 0
},
speed: {
type: 'number',
default: 1
},
lat: {
type: 'number',
default: 0,
},
lng: {
type: 'number',
default: 0,
},
upVector: {
type: 'vec3',
default: {x: 0, y: 1, z: 0}
},
vrMode: {
type: 'boolean',
default: false
},
tilt: {
default: 0
},
enabled: {
type: 'boolean',
default: true
},
},
init: function () {
const el = this.el;
const data = this.data;
this.enabled = !data.vrMode;
if (data.vrMode) {
el.sceneEl.addEventListener('enter-vr', () => {
if (!AFRAME.utils.device.checkHeadsetConnected() &&
!AFRAME.utils.device.isMobile()) { return; }
this.enabled = true;
});
el.sceneEl.addEventListener('exit-vr', () => {
if (!AFRAME.utils.device.checkHeadsetConnected() &&
!AFRAME.utils.device.isMobile()) { return; }
this.enabled = false;
});
}
this.paused = false;
this.camera = el.sceneEl.camera;
this.origin = new THREE.Vector3();
this.position = new THREE.Vector3(0, 1, 0);
this.position.setLength(this.data.radius);
this.forward = new THREE.Vector3(0, 0, 1);
this.look = new THREE.Vector3(
-data.upVector.x,
-data.upVector.y,
-data.upVector.z
);
},
update: function (oldData) {
const data = this.data;
if (oldData.lat !== data.lat || oldData.lng !== data.lng) {
const pos = this.latLngToPosition(data.lat, -data.lng).multiplyScalar(data.radius);
this.position.copy(pos);
}
},
tick: (function () {
const matrix = new THREE.Matrix4();
const rotationMatrix = new THREE.Matrix4();
return function (time, delta) {
if (!this.data.enabled || this.paused || !this.enabled || this.speed <= 0) return;
delta = delta / 1000;
const data = this.data;
const velocity = data.speed * delta;
// set length of forward z-axis
// var forward = this.getForward().setLength(velocity.length());
const forward = this._getForward().setLength(velocity);
// change position by forward
if (this.position.add(forward)) {
const length = this.position.length();
// set max and min height
if (length < data.radius - data.minRadius) {
this.position.setLength(data.radius - data.minRadius);
} else if (length > data.radius + data.maxRadius) {
this.position.setLength(data.radius + data.maxRadius);
}
}
// thats were cross products are used most
// https://classroom.udacity.com/courses/cs291/lessons/158750187/concepts/1694147620923#
// find the frame of reference
// calculate with the cross product the real forward/look vector
// up or normal vector
const up = this.position.clone().sub(this.origin).normalize();
// tangent vector
const tangent = up.clone().cross(this.look).normalize();
// look vector or binormal/bitangent vector
const look = tangent.clone().cross(up).normalize();
// object.quaternion.setFromUnitVectors(this.forward, look);
this.look = look;
const c = matrix.elements;
// aplpy x, y and z axis basis vector
// 4th column is position
c[0] = tangent.x, c[1] = tangent.y, c[2] = tangent.z, c[3] = 0; // tangent vector
c[4] = up.x, c[5] = up.y, c[6] = up.z, c[7] = 0; // up Vector
c[8] = look.x, c[9] = look.y, c[10] = look.z, c[11] = 0; // look vector
c[12] = this.position.x, c[13] = this.position.y, c[14] = this.position.z, c[15] = 1;
rotationMatrix.makeRotationX(THREE.Math.degToRad(this.data.tilt));
matrix.multiply(rotationMatrix);
const object = this.el.object3D;
object.matrixAutoUpdate = false;
object.matrix = matrix;
object.updateMatrixWorld(); // also apply to child
};
})(),
_getForward: (function () {
const zaxis = new THREE.Vector3();
return function () {
this.camera.getWorldDirection(zaxis);
return zaxis;
};
}()),
getLatLngAzimuth: function () {
const position = this.position.clone();
const nextPosition = position.clone().add(this._getForward());
const latLng = this.positionToLatLng(position.x, position.y, position.z);
const nextLatLng = this.positionToLatLng(nextPosition.x, nextPosition.y, nextPosition.z);
const azimuth = Math.atan2(-(nextLatLng.lng - latLng.lng), nextLatLng.lat - latLng.lat);
return {
lat: THREE.Math.radToDeg(latLng.lat),
lng: THREE.Math.radToDeg(latLng.lng),
azimuth: THREE.Math.radToDeg(azimuth)
};
},
positionToLatLng: function (x, y, z) {
const radius = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
const lat = Math.asin(y / radius); // or acos(z / radius)
var lng = Math.atan2(x, z) - Math.PI / 2; // or atan2(y, x)
// reset longitude to the positive datum of the world
if (lng < -Math.PI) {
lng += 2 * Math.PI;
}
return {
lat: lat,
lng: lng
};
},
latLngToPosition: function (lat, lng) {
// center lat and lng
const nlat = THREE.Math.degToRad(lat);
// 0 is in the middle however the sphere starts on the left, thats why we need to offset
const nlng = THREE.Math.degToRad(lng + 0);
return new THREE.Vector3(
Math.cos(nlat) * Math.cos(nlng),
Math.sin(nlat),
Math.cos(nlat) * Math.sin(nlng)
);
},
remove: function () { },
pause: function () {
this.paused = true;
},
play: function () {
this.paused = false;
}
});
/***/ })
/******/ ]);