Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeviceOrientationCamera #4608

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 41 additions & 52 deletions Source/Scene/DeviceOrientationCameraController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ define([
'../Core/defaultValue',
'../Core/defined',
'../Core/destroyObject',
'../Core/Cartesian3',
'../Core/DeveloperError',
'../Core/Math',
'../Core/Matrix3',
Expand All @@ -11,6 +12,7 @@ define([
defaultValue,
defined,
destroyObject,
Cartesian3,
DeveloperError,
CesiumMath,
Matrix3,
Expand All @@ -29,80 +31,67 @@ define([

this._scene = scene;

this._lastAlpha = undefined;
this._lastBeta = undefined;
this._lastGamma = undefined;

this._alpha = undefined;
this._beta = undefined;
this._gamma = undefined;
this._deviceOrientation = {};
this._screenOrientation = window.orientation || 0;

var that = this;

function callback(e) {
var alpha = e.alpha;
if (!defined(alpha)) {
that._alpha = undefined;
that._beta = undefined;
that._gamma = undefined;
return;
}

that._alpha = CesiumMath.toRadians(alpha);
that._beta = CesiumMath.toRadians(e.beta);
that._gamma = CesiumMath.toRadians(e.gamma);
function deviceOrientationCallback(e) {
that._deviceOrientation = e;
}

function screenOrientationCallback() {
that._screenOrientation = window.orientation || 0;
}

window.addEventListener('deviceorientation', callback, false);
window.addEventListener('deviceorientation', deviceOrientationCallback, false);
window.addEventListener('orientationchange', screenOrientationCallback, false);

this._removeListener = function() {
window.removeEventListener('deviceorientation', callback, false);
window.removeEventListener('deviceorientation', deviceOrientationCallback, false);
window.removeEventListener('orientationchange', screenOrientationCallback, false);
};
}

var scratchQuaternion1 = new Quaternion();
var scratchQuaternion2 = new Quaternion();
var scratchMatrix3 = new Matrix3();
var rotMatrix = new Matrix3();
var direction = new Cartesian3();

function rotate(camera, alpha, beta, gamma, orient) {

function rotate(camera, alpha, beta, gamma) {
var direction = camera.direction;
var right = camera.right;
var up = camera.up;
// Euler angles to rotation matrix
var xRotation = Matrix3.fromRotationX(beta);
var yRotation = Matrix3.fromRotationY(gamma);
var zRotation = Matrix3.fromRotationZ(alpha);

var bQuat = Quaternion.fromAxisAngle(direction, beta, scratchQuaternion2);
var gQuat = Quaternion.fromAxisAngle(right, gamma, scratchQuaternion1);
Matrix3.multiply(yRotation, xRotation, rotMatrix);
Matrix3.multiply(zRotation, rotMatrix, rotMatrix);

var rotQuat = Quaternion.multiply(gQuat, bQuat, gQuat);

var aQuat = Quaternion.fromAxisAngle(up, alpha, scratchQuaternion2);
Quaternion.multiply(aQuat, rotQuat, rotQuat);
// Adjust for screen rotation
var screenRotation = Matrix3.fromRotationZ(-orient);
Matrix3.multiply(rotMatrix, screenRotation, rotMatrix);

var matrix = Matrix3.fromQuaternion(rotQuat, scratchMatrix3);
Matrix3.multiplyByVector(matrix, right, right);
Matrix3.multiplyByVector(matrix, up, up);
Matrix3.multiplyByVector(matrix, direction, direction);
// Set camera
Matrix3.getColumn(rotMatrix, 0, camera.right);
Matrix3.getColumn(rotMatrix, 1, camera.up);
Matrix3.getColumn(rotMatrix, 2, direction);
Cartesian3.negate(direction, camera.direction);
}

DeviceOrientationCameraController.prototype.update = function() {
if (!defined(this._alpha)) {
if (!defined(this._deviceOrientation.alpha)) {
return;
}

if (!defined(this._lastAlpha)) {
this._lastAlpha = this._alpha;
this._lastBeta = this._beta;
this._lastGamma = this._gamma;
}

var a = this._lastAlpha - this._alpha;
var b = this._lastBeta - this._beta;
var g = this._lastGamma - this._gamma;

rotate(this._scene.camera, -a, b, g);
var alpha = this._deviceOrientation.alpha ?
CesiumMath.toRadians(this._deviceOrientation.alpha) : 0;
var beta = this._deviceOrientation.beta ?
CesiumMath.toRadians(this._deviceOrientation.beta) : 0;
var gamma = this._deviceOrientation.gamma ?
CesiumMath.toRadians(this._deviceOrientation.gamma) : 0;
var orient = CesiumMath.toRadians(this._screenOrientation);

this._lastAlpha = this._alpha;
this._lastBeta = this._beta;
this._lastGamma = this._gamma;
rotate(this._scene.camera, alpha, beta, gamma, orient);
};

/**
Expand Down
71 changes: 41 additions & 30 deletions Specs/Scene/DeviceOrientationCameraControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,47 +62,58 @@ defineSuite([
return new DeviceOrientationCameraController();
}).toThrowDeveloperError();
});
it('is at rest looking down', function() {
var position = Cartesian3.clone(camera.position);

fireEvent({
alpha : 0,
gamma: 0,
beta: 0
});

expect(camera.position).toEqual(position);
expect(camera.direction).toEqualEpsilon(new Cartesian3(0, 0, -1), CesiumMath.EPSILON14);
expect(camera.up).toEqualEpsilon(Cartesian3.UNIT_Y, CesiumMath.EPSILON14);
expect(camera.right).toEqualEpsilon(Cartesian3.UNIT_X, CesiumMath.EPSILON14);
});

it('rotates for alpha', function() {
var position = Cartesian3.clone(camera.position);
var up = Cartesian3.clone(camera.up);
it('pitch to look north', function() {
var position = Cartesian3.clone(camera.position);

fireEvent({
alpha : 90.0
});
fireEvent({
beta: 90
});

expect(camera.position).toEqual(position);
expect(camera.direction).toEqualEpsilon(Cartesian3.UNIT_Y, CesiumMath.EPSILON14);
expect(camera.up).toEqualEpsilon(up, CesiumMath.EPSILON14);
expect(camera.right).toEqualEpsilon(Cartesian3.UNIT_X, CesiumMath.EPSILON14);
expect(camera.position).toEqual(position);
expect(camera.direction).toEqualEpsilon(Cartesian3.UNIT_Y, CesiumMath.EPSILON14);
expect(camera.up).toEqualEpsilon(Cartesian3.UNIT_Z, CesiumMath.EPSILON14);
expect(camera.right).toEqualEpsilon(Cartesian3.UNIT_X, CesiumMath.EPSILON14);
});

it('rotates for beta', function() {
var position = Cartesian3.clone(camera.position);
var direction = Cartesian3.clone(camera.direction);
it('yaw to look east', function() {
var position = Cartesian3.clone(camera.position);

fireEvent({
beta : 90.0
});
fireEvent({
gamma: -90
});

expect(camera.position).toEqual(position);
expect(camera.direction).toEqualEpsilon(direction, CesiumMath.EPSILON14);
expect(camera.up).toEqualEpsilon(Cartesian3.UNIT_Y, CesiumMath.EPSILON14);
expect(camera.right).toEqualEpsilon(Cartesian3.UNIT_Z, CesiumMath.EPSILON14);
expect(camera.position).toEqual(position);
expect(camera.direction).toEqualEpsilon(Cartesian3.UNIT_X, CesiumMath.EPSILON14);
expect(camera.up).toEqualEpsilon(Cartesian3.UNIT_Y, CesiumMath.EPSILON14);
expect(camera.right).toEqualEpsilon(Cartesian3.UNIT_Z, CesiumMath.EPSILON14);
});

it('rotates for gamma', function() {
var position = Cartesian3.clone(camera.position);
var right = Cartesian3.clone(camera.right);
it('roll to point east', function() {
var position = Cartesian3.clone(camera.position);

fireEvent({
gamma : 90.0
});
fireEvent({
alpha: -90
});

expect(camera.position).toEqual(position);
expect(camera.direction).toEqualEpsilon(Cartesian3.negate(Cartesian3.UNIT_Z, new Cartesian3()), CesiumMath.EPSILON14);
expect(camera.up).toEqualEpsilon(Cartesian3.UNIT_X, CesiumMath.EPSILON14);
expect(camera.right).toEqualEpsilon(right, CesiumMath.EPSILON14);
expect(camera.position).toEqual(position);
expect(camera.direction).toEqualEpsilon(new Cartesian3(0, 0, -1), CesiumMath.EPSILON14);
expect(camera.up).toEqualEpsilon(Cartesian3.UNIT_X, CesiumMath.EPSILON14);
expect(camera.right).toEqualEpsilon(new Cartesian3(0, -1, 0), CesiumMath.EPSILON14);
});

it('is destroyed', function() {
Expand Down