-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
343 lines (275 loc) · 10.1 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* global AFRAME */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
AFRAME.registerComponent('hint-scale', {
schema: {
rightEl: {type: 'selector'},
leftEl: {type: 'selector'},
text: {default: '1.0x'}
},
init: function () {
var el = this.el;
this.rightEl = this.leftEl = null;
var middle = this.middle = document.createElement('a-sphere');
var text = this.text = document.createElement('a-entity');
var line = this.line = document.createElement('a-entity');
this.cameraObject = this.el.parentElement.querySelector('[camera]').object3D;
el.appendChild(middle);
el.appendChild(text);
el.appendChild(line);
this.rightEl = document.getElementById('righthand');
this.leftEl = document.getElementById('lefthand');
middle.setAttribute('radius', '0.003');
middle.setAttribute('color', '#000');
line.setAttribute('line', {color: 'black'});
text.setAttribute('text', {color: 'black', align: 'center', value: '1.0x', width: 0.5});
},
update: function (oldData) {
if (oldData.text !== this.data.text) {
this.text.setAttribute('text', {value: this.data.text});
}
},
tick: (function () {
return function () {
var linePosL = new THREE.Vector3();
var linePosR = new THREE.Vector3();
var mid = new THREE.Vector3();
if (this.el.getAttribute('visible') === true) {
var posL = this.leftEl.getAttribute('position');
var posR = this.rightEl.getAttribute('position');
mid.subVectors(posL, posR).multiplyScalar(0.5).add(posR);
this.middle.setAttribute('position', mid);
mid.y += 0.025;
this.text.setAttribute('position', mid);
this.text.object3D.lookAt(this.cameraObject.position);
linePosR.copy(posR);
linePosL.copy(posL);
this.line.setAttribute('line', {start: linePosL, end: linePosR});
}
}
})()
});
/**
* Camera Transform Controls component for A-Frame.
*/
var UP = new THREE.Vector3(0, 1, 0);
AFRAME.registerComponent('camera-transform-controls', {
schema: {
enabled: {default: true},
cameraRigId: {default: 'cameraRig'},
onStart: {default: 'gripdown'},
onEnd: {default: 'gripup'},
showHint: {default: true}
},
init: function () {
this.cameraRigEl = document.getElementById(this.data.cameraRigId);
var hintEl = this.hintEl = document.createElement('a-entity');
hintEl.setAttribute('hint-scale', '');
hintEl.setAttribute('visible', false);
this.cameraRigEl.appendChild(hintEl);
this.currentDragCenter = new THREE.Vector3();
this.panningController = null;
this.controllers = {
left: {
entity: null,
dragging: false,
dragStartPoint: new THREE.Vector3()
},
right: {
entity: null,
dragging: false,
dragStartPoint: new THREE.Vector3()
}
};
this.originalPosition = new THREE.Vector3();
this.originalScale = new THREE.Vector3();
this.originalRotation = new THREE.Vector3();
this.isLeftButtonDown = false;
this.isRightButtonDown = false;
this.cameraScaleEventDetail = {cameraScaleFactor: 1};
},
/**
* Reset original camera rig transforms if disabling camera scaler.
*/
update: function (oldData) {
var cameraRigEl = this.cameraRigEl;
if (!cameraRigEl) {return;}
// Enabling. Store original transformations.
if (!oldData.enabled && this.data.enabled) {
this.originalPosition.copy(cameraRigEl.object3D.position);
this.originalScale.copy(cameraRigEl.object3D.scale);
this.originalRotation.copy(cameraRigEl.object3D.rotation);
}
// Disabling, reset to original transformations.
if (oldData.enabled && !this.data.enabled) {
cameraRigEl.setAttribute('position', this.originalPosition);
cameraRigEl.setAttribute('scale', this.originalScale);
cameraRigEl.setAttribute('rotation', this.originalRotation.clone());
}
},
tick: function () {
this.hintEl.setAttribute('visible', false);
if (!this.data.enabled) { return; }
if (!this.isLeftButtonDown && !this.isRightButtonDown) { return; }
if (this.isLeftButtonDown && this.isRightButtonDown) {
this.twoHandInteraction();
this.hintEl.setAttribute('visible', this.data.showHint);
} else {
this.processPanning();
}
},
onButtonDown: function (evt) {
var left;
var target;
if (!this.cameraRigEl.object3D) { return; }
target = evt.target;
left = target === this.leftHandEl;
if (left) {
this.isLeftButtonDown = true;
this.panningController = this.controllers.left;
} else {
this.isRightButtonDown = true;
this.panningController = this.controllers.right;
}
this.panningController.entity.object3D.getWorldPosition(
this.panningController.dragStartPoint);
this.released = this.isLeftButtonDown && this.isRightButtonDown;
},
onButtonUp: function (evt) {
var left;
var target;
target = evt.target;
left = evt.target === this.leftHandEl;
if (left) {
this.panningController = this.controllers.right;
this.isLeftButtonDown = false;
} else {
this.panningController = this.controllers.left;
this.isRightButtonDown = false;
}
this.panningController.entity.object3D.getWorldPosition(
this.panningController.dragStartPoint);
if (!this.isLeftButtonDown && !this.isRightButtonDown) {
this.cameraScaleEventDetail.cameraScaleFactor = this.cameraRigEl.object3D.scale.x;
this.el.emit('camerascale', this.cameraScaleEventDetail);
}
this.released = true;
},
/**
* With two hands, translate/rotate/zoom.
*/
twoHandInteraction: (function () {
var centerVec3 = new THREE.Vector3();
var currentDistanceVec3 = new THREE.Vector3();
var currentPositionLeft = new THREE.Vector3();
var currentPositionRight = new THREE.Vector3();
var midPoint = new THREE.Vector3();
var prevDistanceVec3 = new THREE.Vector3();
return function () {
var currentAngle;
var currentDistance;
var deltaAngle;
var deltaDistance;
var translation;
this.leftHandEl.object3D.getWorldPosition(currentPositionLeft);
this.rightHandEl.object3D.getWorldPosition(currentPositionRight);
if (this.released) {
this.prevAngle = signedAngleTo(currentPositionLeft, currentPositionRight);
this.initAngle = this.prevAngle = Math.atan2(
currentPositionLeft.x - currentPositionRight.x,
currentPositionLeft.z - currentPositionRight.z);
midPoint.copy(currentPositionLeft)
.add(currentPositionRight)
.multiplyScalar(0.5);
this.prevDistance = prevDistanceVec3.copy(currentPositionLeft)
.sub(currentPositionRight)
.length();
this.released = false;
}
currentDistance = currentDistanceVec3.copy(currentPositionLeft)
.sub(currentPositionRight)
.length();
deltaDistance = this.prevDistance - currentDistance;
//Get center point using local positions.
centerVec3.copy(this.leftHandEl.object3D.position)
.add(this.rightHandEl.object3D.position)
.multiplyScalar(0.5);
// Set camera rig scale.
this.cameraRigEl.object3D.scale.addScalar(deltaDistance);
this.cameraRigEl.setAttribute('scale', this.cameraRigEl.object3D.scale);
this.hintEl.setAttribute('hint-scale', {text: this.cameraRigEl.object3D.scale.x.toFixed(2) + 'x'});
// Set camera rig position.
translation = centerVec3
.applyQuaternion(this.cameraRigEl.object3D.quaternion)
.multiplyScalar(deltaDistance);
this.cameraRigEl.object3D.position.sub(translation);
this.cameraRigEl.setAttribute('position', this.cameraRigEl.object3D.position);
// Set camera rig rotation.
currentAngle = Math.atan2(currentPositionLeft.x - currentPositionRight.x,
currentPositionLeft.z - currentPositionRight.z);
deltaAngle = currentAngle - this.prevAngle;
this.rotateScene(midPoint, deltaAngle);
this.prevAngle = currentAngle - deltaAngle;
}
})(),
rotateScene: (function () {
var dirVec3 = new THREE.Vector3();
return function (midPoint, deltaAngle) {
var cameraRigEl = this.cameraRigEl;
var rotation;
// Rotate the direction.
dirVec3.copy(cameraRigEl.object3D.position)
.sub(midPoint)
.applyAxisAngle(UP, -deltaAngle);
cameraRigEl.object3D.position.copy(midPoint).add(dirVec3);
cameraRigEl.setAttribute('position', cameraRigEl.object3D.position);
rotation = cameraRigEl.getAttribute('rotation');
rotation.y -= deltaAngle * THREE.Math.RAD2DEG;
cameraRigEl.setAttribute('rotation', rotation);
};
})(),
/**
* One hand panning.
*/
processPanning: (function () {
var currentPosition = new THREE.Vector3();
var deltaPosition = new THREE.Vector3();
return function () {
var dragStartPoint = this.panningController.dragStartPoint;
this.panningController.entity.object3D.getWorldPosition(currentPosition);
deltaPosition.copy(dragStartPoint).sub(currentPosition);
// Apply panning.
this.cameraRigEl.object3D.position.add(deltaPosition);
this.cameraRigEl.setAttribute('position', this.cameraRigEl.object3D.position);
};
})(),
registerHand: function (entity, hand) {
this.controllers[hand].entity = entity;
entity.addEventListener(this.data.onStart, this.onButtonDown.bind(this));
entity.addEventListener(this.data.onEnd, this.onButtonUp.bind(this));
if (hand === 'left') {
this.leftHandEl = entity;
} else {
this.rightHandEl = entity;
}
}
});
AFRAME.registerComponent('camera-transform-controls-hand', {
schema: {
hand: {default: 'right'}
},
play: function () {
this.el.sceneEl.components['camera-transform-controls'].registerHand(this.el, this.data.hand);
}
});
function signedAngleTo (fromVec3, toVec3) {
var angle;
var cross;
angle = fromVec3.angleTo(toVec3);
cross = fromVec3.clone().cross(toVec3);
if (UP.dot(cross) < 0) { // Or > 0.
angle = -angle;
}
return angle;
}