Professional Documents
Culture Documents
this.object = object;
this.object.rotation.reorder("YXZ");
this.element = document;
this.enabled = true;
this.deviceOrientation = {};
this.screenOrientation = 0;
this.alpha = 0;
this.alphaOffsetAngle = 0;
var CONTROLLER_STATE = {
AUTO: 0,
MANUAL_ROTATE: 1,
MANUAL_ZOOM: 2
};
var CONTROLLER_EVENT = {
CALIBRATE_COMPASS: 'compassneedscalibration',
SCREEN_ORIENTATION: 'orientationchange',
MANUAL_CONTROL: 'userinteraction', // userinteractionstart, userinteractionend
ZOOM_CONTROL: 'zoom', // zoomstart, zoomend
ROTATE_CONTROL: 'rotate', // rotatestart, rotateend
};
return function(name) {
eventData = arguments || {};
eventData.type = name;
eventData.target = this;
this.dispatchEvent(eventData);
}.bind(this);
}.bind(this)();
scope.deviceOrientation = event;
};
scope.screenOrientation = window.orientation || 0;
};
// The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''
euler.set(beta, alpha, -gamma, 'YXZ'); // 'ZXY' for the device, but 'YXZ' for us
quaternion.multiply(q1); // camera looks out the back of the device, not the top
}();
this.connect = function() {
scope.enabled = true;
};
this.disconnect = function() {
window.removeEventListener('orientationchange', onScreenOrientationChangeEvent,
false);
window.removeEventListener('deviceorientation', onDeviceOrientationChangeEvent, false);
this.element.removeEventListener('mousedown', this.onDocumentMouseDown, false);
this.element.removeEventListener('touchstart', this.onDocumentTouchStart, false);
scope.enabled = false;
};
this.update = function() {
if (gyroPresent) {
if (lockView) {
if (window.innerHeight > window.innerWidth) {
if (beta < Math.PI * 0.5) {
beta = Math.PI * 0.5;
}
} else {
// Kept here for debugging.
if (gamma < 0 ||
(beta < Math.PI * 0.25 &&
beta > Math.PI * -0.25 &&
gamma < Math.PI * 0.1 &&
gamma > Math.PI * -0.1)) {
gamma = Math.PI * -0.5;
}
}
}
}
else {
if (appState !== CONTROLLER_STATE.AUTO) {
this.updateManualMove();
}
}
};
this.updateAlphaOffsetAngle = function(angle) {
this.alphaOffsetAngle = angle;
this.update();
};
this.dispose = function() {
this.disconnect();
};
this.onDocumentMouseDown = function(event) {
//event.preventDefault();
appState = CONTROLLER_STATE.MANUAL_ROTATE;
this.freeze = true;
tmpQuat.copy(this.object.quaternion);
fireEvent(CONTROLLER_EVENT.MANUAL_CONTROL + 'start');
fireEvent(CONTROLLER_EVENT.ROTATE_CONTROL + 'start');
}.bind(this);
this.onDocumentMouseMove = function(event) {
currentX = event.pageX;
currentY = event.pageY;
}.bind(this);
this.onDocumentMouseUp = function(event) {
this.element.removeEventListener('mousemove', this.onDocumentMouseMove, false);
this.element.removeEventListener('mouseup', this.onDocumentMouseUp, false);
appState = CONTROLLER_STATE.AUTO;
this.freeze = false;
fireEvent(CONTROLLER_EVENT.MANUAL_CONTROL + 'end');
fireEvent(CONTROLLER_EVENT.ROTATE_CONTROL + 'end');
}.bind(this);
this.onDocumentTouchStart = function(event) {
//event.preventDefault();
//event.stopPropagation();
switch (event.touches.length) {
case 1: // ROTATE
if (this.enableManualDrag !== true) return;
appState = CONTROLLER_STATE.MANUAL_ROTATE;
this.freeze = true;
tmpQuat.copy(this.object.quaternion);
fireEvent(CONTROLLER_EVENT.MANUAL_CONTROL + 'start');
fireEvent(CONTROLLER_EVENT.ROTATE_CONTROL + 'start');
break;
case 2: // ZOOM
if (this.enableManualZoom !== true) return;
appState = CONTROLLER_STATE.MANUAL_ZOOM;
this.freeze = true;
tmpFOV = this.object.fov;
zoomP1.set(event.touches[0].pageX, event.touches[0].pageY);
zoomP2.set(event.touches[1].pageX, event.touches[1].pageY);
fireEvent(CONTROLLER_EVENT.MANUAL_CONTROL + 'start');
fireEvent(CONTROLLER_EVENT.ZOOM_CONTROL + 'start');
break;
}
}.bind(this);
this.onDocumentTouchMove = function(event) {
switch (event.touches.length) {
case 1:
currentX = event.touches[0].pageX;
currentY = event.touches[0].pageY;
break;
case 2:
zoomP1.set(event.touches[0].pageX, event.touches[0].pageY);
zoomP2.set(event.touches[1].pageX, event.touches[1].pageY);
break;
}
}.bind(this);
this.onDocumentTouchEnd = function(event) {
this.element.removeEventListener('touchmove', this.onDocumentTouchMove, false);
this.element.removeEventListener('touchend', this.onDocumentTouchEnd, false);
this.freeze = false;
fireEvent(CONTROLLER_EVENT.MANUAL_CONTROL + 'end');
fireEvent(CONTROLLER_EVENT.ROTATE_CONTROL + 'end');
this.freeze = false;
fireEvent(CONTROLLER_EVENT.MANUAL_CONTROL + 'end');
fireEvent(CONTROLLER_EVENT.ZOOM_CONTROL + 'end');
}
}.bind(this);
this.updateManualMove = function() {
return function() {
objQuat.copy(tmpQuat);
phi = THREE.Math.degToRad(lat);
theta = THREE.Math.degToRad(lon);
objQuat.multiply(rotQuat);
objQuat.multiply(rotQuat);
// Remove introduced z-axis rotation and add device's current z-axis rotation
objQuat.multiply(rotQuat);
this.object.quaternion.copy(objQuat);
zoomCurrent = zoomP1.distanceTo(zoomP2);
this.object.updateProjectionMatrix();
if (deviceQuat) {
tmpQuat.multiply(rotQuat);
this.object.quaternion.copy(tmpQuat);
};
}();
this.connect();
};
DeviceOrientationController.prototype = Object.create(THREE.EventDispatcher.prototype);