Skip to content

Commit

Permalink
Merge pull request #803 from hpinkos/homeButtonCameraFlight
Browse files Browse the repository at this point in the history
change widget home button to use camera flights
  • Loading branch information
bagnell committed Jun 11, 2013
2 parents a17679d + 309f7ed commit 45cd217
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Source/Scene/AnimationCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ define([
throw new DeveloperError('duration is required.');
}

if (options.duration !== 0) {
if (options.duration > 0) {
var delayDuration = defaultValue(options.delayDuration, 0);
var easingFunction = defaultValue(options.easingFunction, Tween.Easing.Linear.None);

Expand Down
82 changes: 66 additions & 16 deletions Source/Scene/CameraFlightPath.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*global define*/
define([
'../Core/Cartesian2',
'../Core/Cartesian3',
'../Core/clone',
'../Core/defaultValue',
Expand All @@ -14,6 +15,7 @@ define([
'../Scene/SceneMode',
'../ThirdParty/Tween'
], function(
Cartesian2,
Cartesian3,
clone,
defaultValue,
Expand Down Expand Up @@ -404,6 +406,9 @@ define([
* @see Scene#getFrameState
* @see Scene#getAnimations
*/
var dirScratch = new Cartesian3();
var rightScratch = new Cartesian3();
var upScratch = new Cartesian3();
CameraFlightPath.createAnimation = function(frameState, description) {
description = defaultValue(description, defaultValue.EMPTY_OBJECT);
var destination = description.destination;
Expand All @@ -422,11 +427,70 @@ define([
var up = description.up;
var duration = defaultValue(description.duration, 3000.0);
var onComplete = description.onComplete;
var frustum = frameState.camera.frustum;

if (frameState.mode === SceneMode.SCENE2D) {
if ((Cartesian2.equalsEpsilon(frameState.camera.position, destination, CesiumMath.EPSILON6)) && (CesiumMath.equalsEpsilon(Math.max(frustum.right - frustum.left, frustum.top - frustum.bottom), destination.z, CesiumMath.EPSILON6))) {
return {
duration : 0,
onComplete : onComplete
};
}
} else if (Cartesian3.equalsEpsilon(destination, frameState.camera.position, CesiumMath.EPSILON6)) {
return {
duration : 0,
onComplete : onComplete
};
}

if (Cartesian3.equalsEpsilon(destination, frameState.camera.position, CesiumMath.EPSILON6)) {
if (duration <= 0) {
var newOnComplete = function() {
var position = destination;
if (frameState.mode === SceneMode.SCENE3D) {
if (typeof description.direction === 'undefined' && typeof description.up === 'undefined'){
dirScratch = position.negate(dirScratch).normalize(dirScratch);
rightScratch = dirScratch.cross(Cartesian3.UNIT_Z, rightScratch).normalize(rightScratch);
} else {
dirScratch = description.direction;
rightScratch = dirScratch.cross(description.up, rightScratch).normalize(rightScratch);
}
upScratch = defaultValue(description.up, rightScratch.cross(dirScratch, upScratch));
} else {
if (typeof description.direction === 'undefined' && typeof description.up === 'undefined'){
dirScratch = Cartesian3.UNIT_Z.negate(dirScratch);
rightScratch = dirScratch.cross(Cartesian3.UNIT_Y, rightScratch).normalize(rightScratch);
} else {
dirScratch = description.direction;
rightScratch = dirScratch.cross(description.up, rightScratch).normalize(rightScratch);
}
upScratch = defaultValue(description.up, rightScratch.cross(dirScratch, upScratch));
}

Cartesian3.clone(position, frameState.camera.position);
Cartesian3.clone(dirScratch, frameState.camera.direction);
Cartesian3.clone(upScratch, frameState.camera.up);
Cartesian3.clone(rightScratch, frameState.camera.right);

if (frameState.mode === SceneMode.SCENE2D) {
var zoom = frameState.camera.position.z;


var ratio = frustum.top / frustum.right;

var incrementAmount = (zoom - (frustum.right - frustum.left)) * 0.5;
frustum.right += incrementAmount;
frustum.left -= incrementAmount;
frustum.top = ratio * frustum.right;
frustum.bottom = -frustum.top;
}

if (typeof onComplete === 'function') {
onComplete();
}
};
return {
duration : 0,
onComplete : description.onComplete
onComplete : newOnComplete
};
}

Expand Down Expand Up @@ -494,13 +558,6 @@ define([
throw new DeveloperError('frameState.mode cannot be SceneMode.MORPHING');
}

if (Cartesian3.equalsEpsilon(c3destination, frameState.camera.position, CesiumMath.EPSILON6)) {
return {
duration : 0,
onComplete : description.onComplete
};
}

var createAnimationDescription = clone(description);
createAnimationDescription.destination = c3destination;
return this.createAnimation(frameState, createAnimationDescription);
Expand Down Expand Up @@ -541,13 +598,6 @@ define([
var camera = frameState.camera;
camera.controller.getExtentCameraCoordinates(extent, c3destination);

if (Cartesian3.equalsEpsilon(c3destination, frameState.camera.position, CesiumMath.EPSILON6)) {
return {
duration : 0,
onComplete : description.onComplete
};
}

createAnimationDescription.destination = c3destination;
return this.createAnimation(frameState, createAnimationDescription);
};
Expand Down
4 changes: 2 additions & 2 deletions Source/Widgets/HomeButton/HomeButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ define([
* @exception {DeveloperError} container is required.
* @exception {DeveloperError} scene is required.
*/
var HomeButton = function(container, scene, transitioner, ellipsoid) {
var HomeButton = function(container, scene, transitioner, ellipsoid, flightDuration) {
if (typeof container === 'undefined') {
throw new DeveloperError('container is required.');
}
Expand All @@ -41,7 +41,7 @@ define([
}

this._container = container;
this._viewModel = new HomeButtonViewModel(scene, transitioner, ellipsoid);
this._viewModel = new HomeButtonViewModel(scene, transitioner, ellipsoid, flightDuration);

this._element = document.createElement('span');
this._element.className = 'cesium-homeButton';
Expand Down
76 changes: 48 additions & 28 deletions Source/Widgets/HomeButton/HomeButtonViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ define([
'../../Core/Matrix4',
'../../Scene/Camera',
'../../Scene/CameraColumbusViewMode',
'../../Scene/CameraFlightPath',
'../../Scene/PerspectiveFrustum',
'../../Scene/SceneMode',
'../createCommand',
Expand All @@ -25,13 +26,14 @@ define([
Matrix4,
Camera,
CameraColumbusViewMode,
CameraFlightPath,
PerspectiveFrustum,
SceneMode,
createCommand,
knockout) {
"use strict";

function viewHome(scene, ellipsoid, transitioner) {
function viewHome(scene, ellipsoid, transitioner, flightDuration) {
var mode = scene.mode;

var camera = scene.getCamera();
Expand All @@ -47,41 +49,56 @@ define([
if (typeof transitioner !== 'undefined' && mode === SceneMode.MORPHING) {
transitioner.completeMorph();
}
var flight;
var description;

if (mode === SceneMode.SCENE2D) {
camera.controller.viewExtent(Extent.MAX_VALUE);
camera.transform = new Matrix4(0, 0, 1, 0,
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 1);
description = {
destination: Extent.MAX_VALUE,
duration: flightDuration
};
flight = CameraFlightPath.createAnimationExtent(scene.getFrameState(), description);
scene.getAnimations().add(flight);
} else if (mode === SceneMode.SCENE3D) {
Cartesian3.add(camera.position, Matrix4.getTranslation(camera.transform), camera.position);
var rotation = Matrix4.getRotation(camera.transform);
rotation.multiplyByVector(camera.direction, camera.direction);
rotation.multiplyByVector(camera.up, camera.up);
rotation.multiplyByVector(camera.right, camera.right);
camera.transform = Matrix4.IDENTITY.clone();
var defaultCamera = new Camera(canvas);
defaultCamera.position.clone(camera.position);
defaultCamera.direction.clone(camera.direction);
defaultCamera.up.clone(camera.up);
defaultCamera.right.clone(camera.right);
defaultCamera.transform.clone(camera.transform);
defaultCamera.frustum.clone(camera.frustum);
description = {
destination: defaultCamera.position,
duration: flightDuration,
up: defaultCamera.up,
direction: defaultCamera.direction
};
flight = CameraFlightPath.createAnimation(scene.getFrameState(), description);
scene.getAnimations().add(flight);
} else if (mode === SceneMode.COLUMBUS_VIEW) {
var transform = new Matrix4(0.0, 0.0, 1.0, 0.0,
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0);

camera.transform = new Matrix4(0.0, 0.0, 1.0, 0.0,
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0);
var maxRadii = ellipsoid.getMaximumRadius();
var position = new Cartesian3(0.0, -1.0, 1.0).normalize().multiplyByScalar(5.0 * maxRadii);
var direction = Cartesian3.ZERO.subtract(position).normalize();
var right = direction.cross(Cartesian3.UNIT_Z);
var up = right.cross(direction);
right = direction.cross(up);
direction = up.cross(right);

var frustum = new PerspectiveFrustum();
frustum.fovy = CesiumMath.toRadians(60.0);
frustum.aspectRatio = canvas.clientWidth / canvas.clientHeight;

camera.position = position;
camera.direction = direction;
camera.up = up;
camera.right = right;
camera.frustum = frustum;
camera.transform = transform;

description = {
destination: position,
duration: flightDuration,
up: up,
direction: direction
};

flight = CameraFlightPath.createAnimation(scene.getFrameState(), description);
scene.getAnimations().add(flight);
}
}

Expand All @@ -93,22 +110,25 @@ define([
* @param {Scene} scene The scene instance to use.
* @param {SceneTransitioner} [transitioner] The scene transitioner instance to use.
* @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to be viewed when in home position.
* @param {Number} [flightDuration] The duration of the camera flight in milliseconds
*
* @exception {DeveloperError} scene is required.
*/
var HomeButtonViewModel = function(scene, transitioner, ellipsoid) {
var HomeButtonViewModel = function(scene, transitioner, ellipsoid, flightDuration) {
if (typeof scene === 'undefined') {
throw new DeveloperError('scene is required.');
}

ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
flightDuration = defaultValue(flightDuration, 1500);

this._scene = scene;
this._ellipsoid = ellipsoid;
this._transitioner = transitioner;
this._flightDuration = flightDuration;

this._command = createCommand(function() {
viewHome(scene, ellipsoid, transitioner);
viewHome(scene, ellipsoid, transitioner, flightDuration);
});

/**
Expand Down
Loading

0 comments on commit 45cd217

Please sign in to comment.