forked from ngokevin/aframe-component-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
216 lines (191 loc) · 6.8 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
/**
* Capture Mouse component.
*
* Control entity rotation directly with captured mouse.
*
* @namespace capture-mouse
* @param {bool} [enabled=true] - To completely enable or disable the controls
* @param {number} [sensitivity=1] - How fast the object rotates in response to mouse
*/
// To avoid recalculation at every mouse movement tick
var PI_2 = Math.PI / 2;
module.exports.component = {
dependencies: ['position','rotation'],
schema: {
enabled: { default: true },
sensitivity: { default: 1 },
},
/**
* Called once when component is attached. Generally for initial setup.
*/
init: function () {
// The canvas where the scene is painted
this.sceneEl = document.querySelector('a-scene');
this.canvasEl = this.sceneEl.canvas;
this.setupMouseControls();
this.setupHMDControls();
this.attachEventListeners();
this.bindFunctions();
this.sceneEl.addBehavior(this);
this.previousPosition = new THREE.Vector3();
this.deltaPosition = new THREE.Vector3();
},
setupMouseControls: function () {
this.pitchObject = new THREE.Object3D();
this.yawObject = new THREE.Object3D();
this.yawObject.position.y = 10;
this.yawObject.add(this.pitchObject);
this.lockIsSupported = 'pointerLockElement' in document ||
'mozPointerLockElement' in document ||
'webkitPointerLockElement' in document;
},
setupHMDControls: function () {
this.dolly = new THREE.Object3D();
this.euler = new THREE.Euler();
this.controls = new THREE.VRControls(this.dolly);
this.zeroQuaternion = new THREE.Quaternion();
},
attachEventListeners: function () {
if (this.lockIsSupported) {
document.addEventListener('pointerlockchange', this.onLockChange.bind(this), false);
document.addEventListener('mozpointerlockchange', this.onLockChange.bind(this), false);
document.addEventListener('webkitpointerlockchange', this.onLockChange.bind(this), false);
document.addEventListener('pointerlockerror', this.onLockError, false);
document.addEventListener('mozpointerlockerror', this.onLockError, false);
document.addEventListener('webkitpointerlockerror', this.onLockError, false);
this.canvasEl.onclick = this.captureMouse.bind(this);
}
},
bindFunctions: function () {
this.onMouseMoveL = this.onMouseMove.bind(this);
},
remove: function () {
this.releaseMouse();
this.sceneEl.removeBehavior(this);
},
captureMouse: function () {
this.sceneEl.requestPointerLock = this.sceneEl.canvas.requestPointerLock ||
this.sceneEl.canvas.mozRequestPointerLock ||
this.sceneEl.canvas.webkitRequestPointerLock;
// Ask the browser to lock the pointer
this.sceneEl.requestPointerLock();
},
releaseMouse: function () {
document.exitPointerLock = document.exitPointerLock ||
document.mozExitPointerLock ||
document.webkitExitPointerLock;
document.exitPointerLock();
},
onLockChange: function (e) {
var scene = this.el.sceneEl;
if (document.pointerLockElement === scene ||
document.mozPointerLockElement === scene ||
document.webkitPointerLockElement === scene) {
// Pointer was just locked
// Enable the mousemove listener
document.addEventListener('mousemove', this.onMouseMoveL, false);
} else {
// Pointer was just unlocked
// Disable the mousemove listener
console.log("pointer unlock");
document.removeEventListener('mousemove', this.onMouseMoveL, false);
}
},
onLockError: function (e) {
console.trace(e);
},
update: function () {
if (!this.data.enabled) { return; }
this.controls.update();
this.updateOrientation();
this.updatePosition();
},
updateOrientation: (function () {
var hmdEuler = new THREE.Euler();
hmdEuler.order = 'YXZ';
return function () {
var pitchObject = this.pitchObject;
var yawObject = this.yawObject;
var hmdQuaternion = this.calculateHMDQuaternion();
hmdEuler.setFromQuaternion(hmdQuaternion);
this.el.setAttribute('rotation', {
x: THREE.Math.radToDeg(hmdEuler.x) + THREE.Math.radToDeg(pitchObject.rotation.x),
y: THREE.Math.radToDeg(hmdEuler.y) + THREE.Math.radToDeg(yawObject.rotation.y),
z: THREE.Math.radToDeg(hmdEuler.z)
});
};
})(),
calculateHMDQuaternion: (function () {
var hmdQuaternion = new THREE.Quaternion();
return function () {
var dolly = this.dolly;
if (!this.zeroed && !dolly.quaternion.equals(this.zeroQuaternion)) {
this.zeroOrientation();
this.zeroed = true;
}
hmdQuaternion.copy(this.zeroQuaternion).multiply(dolly.quaternion);
return hmdQuaternion;
};
})(),
updatePosition: (function () {
var position = new THREE.Vector3();
var quaternion = new THREE.Quaternion();
var scale = new THREE.Vector3();
return function () {
var el = this.el;
var deltaPosition = this.calculateDeltaPosition();
var currentPosition = el.getComputedAttribute('position');
this.el.object3D.matrixWorld.decompose(position, quaternion, scale);
deltaPosition.applyQuaternion(quaternion);
el.setAttribute('position', {
x: currentPosition.x + deltaPosition.x,
y: currentPosition.y + deltaPosition.y,
z: currentPosition.z + deltaPosition.z
});
};
})(),
calculateDeltaPosition: function () {
var dolly = this.dolly;
var deltaPosition = this.deltaPosition;
var previousPosition = this.previousPosition;
deltaPosition.copy(dolly.position);
deltaPosition.sub(previousPosition);
previousPosition.copy(dolly.position);
return deltaPosition;
},
updateHMDQuaternion: (function () {
var hmdQuaternion = new THREE.Quaternion();
return function () {
var dolly = this.dolly;
this.controls.update();
if (!this.zeroed && !dolly.quaternion.equals(this.zeroQuaternion)) {
this.zeroOrientation();
this.zeroed = true;
}
hmdQuaternion.copy(this.zeroQuaternion).multiply(dolly.quaternion);
return hmdQuaternion;
};
})(),
zeroOrientation: function () {
var euler = new THREE.Euler();
euler.setFromQuaternion(this.dolly.quaternion.clone().inverse());
// Cancel out roll and pitch. We want to only reset yaw
euler.z = 0;
euler.x = 0;
this.zeroQuaternion.setFromEuler(euler);
},
onMouseMove: function (e) {
if (!this.data.enabled) {return;}
var movementX = e.movementX ||
e.mozMovementX ||
e.webkitMovementX ||
0;
var movementY = e.movementY ||
e.mozMovementY ||
e.webkitMovementY ||
0;
this.yawObject.rotation.y -= movementX * 0.002 * this.data.sensitivity;
this.pitchObject.rotation.x -= movementY * 0.002 * this.data.sensitivity;
this.pitchObject.rotation.x = Math.max(-PI_2, Math.min(PI_2, this.pitchObject.rotation.x));
}
};