The Flight Demos use the mouse as the primary flight control. This gives you more precise control over pitch and bank. To do this, the mouse controls utilize a variation of the three.js pointer lock controls module.
At the beginning of the program, add the following to the variables:
//- POINTER LOCK CONTROLS ------//----------------------------------------------
// Adjustments
let InpMos = new THREE.Vector2(); // Mouse Inputs
//. Setup ......................................................................
let controls = new PointerLockControls(renderer.domElement,InpMos);
let blocker = document.getElementById("blocker");
let instructions = document.getElementById("instructions");
instructions.addEventListener("click", function () {controls.lock();});
controls.addEventListener("lock", function () {
instructions.style.display = "none";
blocker.style.display = "none";
});
controls.addEventListener("unlock", function () {
blocker.style.display = "block";
instructions.style.display = "";
})
let _changeEvent = {type: 'change'};
let _lockEvent = {type: 'lock'};
let _unlockEvent = {type: 'unlock'};
Within the program, insert the following:
/*******************************************************************************
*
* POINTER LOCK CONTROLS
*
*******************************************************************************/
// This is a stripped-down and modified version of three.js PointerLockControls module
class PointerLockControls extends Controls {
constructor(domElement=null,InpMos) {
super(domElement,InpMos);
this.domElement = domElement;
this.InpMos = InpMos;
this.isLocked = false;
// event listeners
this._onMouseMove = onMouseMove.bind(this);
this._onPointerlockChange = onPointerlockChange.bind(this);
this._onPointerlockError = onPointerlockError.bind(this);
if (this.domElement !== null) this.connect();
}
connect() {
this.domElement.ownerDocument.addEventListener('mousemove', this._onMouseMove);
this.domElement.ownerDocument.addEventListener('pointerlockchange', this._onPointerlockChange);
this.domElement.ownerDocument.addEventListener('pointerlockerror', this._onPointerlockError);
}
disconnect() {
this.domElement.ownerDocument.removeEventListener('mousemove', this._onMouseMove);
this.domElement.ownerDocument.removeEventListener('pointerlockchange', this._onPointerlockChange);
this.domElement.ownerDocument.removeEventListener('pointerlockerror', this._onPointerlockError);
}
dispose() {
this.disconnect();
}
lock() {
this.domElement.requestPointerLock();
}
unlock() {
this.domElement.ownerDocument.exitPointerLock();
}
}
//- Event Listeners ------------------------------------------------------------
function onMouseMove(event) {
if (this.enabled === false || this.isLocked === false) return;
const movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
const movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
this.InpMos.x = movementX; // This modifies the InpMos variable for your own use
this.InpMos.y = movementY;
this.dispatchEvent(_changeEvent);
}
function onPointerlockChange() {
if (this.domElement.ownerDocument.pointerLockElement === this.domElement) {
this.dispatchEvent(_lockEvent);
this.isLocked = true;
} else {
this.dispatchEvent(_unlockEvent);
this.isLocked = false;
}
}
function onPointerlockError() {
console.error('THREE.PointerLockControls: Unable to use Pointer Lock API');
}