Skip to content

Commit 04b99aa

Browse files
authored
Merge pull request #6859 from AnalyticalGraphicsInc/globe-pick
Fix globe.pick for 2D/CV
2 parents 39cf44d + 0570dde commit 04b99aa

7 files changed

+52
-17
lines changed

CHANGES.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Change Log
44
### 1.49 - 2018-09-03
55

66
##### Breaking Changes :mega:
7-
* Removed `ClippingPlaneCollection.clone`
7+
* Removed `ClippingPlaneCollection.clone` [#6872](https://github.com/AnalyticalGraphicsInc/cesium/pull/6872)
8+
* Changed `Globe.pick` to return a position in ECEF coordinates regardless of the current scene mode. This will only effect you if you were working around a bug to make `Globe.pick` work in 2D and Columbus View. Use `Globe.pickWorldCoordinates` to get the position in world coordinates that correlate to the current scene mode. [#6859](https://github.com/AnalyticalGraphicsInc/cesium/pull/6859)
89

910
##### Additions :tada:
1011
* Added `ClippingPlaneCollection.planeAdded` and `ClippingPlaneCollection.planeRemoved` events. `planeAdded` is raised when a new plane is added to the collection and `planeRemoved` is raised when a plane is removed. [#6875](https://github.com/AnalyticalGraphicsInc/cesium/pull/6875)
@@ -13,6 +14,9 @@ Change Log
1314
##### Fixes :wrench:
1415
* The Geocoder widget now takes terrain altitude into account when calculating its final destination.
1516
* The Viewer widget now takes terrain altitude into account when zooming or flying to imagery layers.
17+
* Fixed `getPickRay` in 2D. [#2480](https://github.com/AnalyticalGraphicsInc/cesium/issues/2480)
18+
* Fixed `Globe.pick` for 2D and Columbus View [#6859](https://github.com/AnalyticalGraphicsInc/cesium/pull/6859)
19+
* Fixed imagery layer feature picking in 2D and Columbus view [#6859](https://github.com/AnalyticalGraphicsInc/cesium/pull/6859)
1620
* Fixed bug that caused a new `ClippingPlaneCollection` to be created every frame when used with a model entity [#6872](https://github.com/AnalyticalGraphicsInc/cesium/pull/6872)
1721
* Fixed crash when rendering translucent objects when all shadow maps in the scene set `fromLightSource` to false. [#6883](https://github.com/AnalyticalGraphicsInc/cesium/pull/6883)
1822

Source/Scene/Camera.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ define([
10131013
mousePosition.y = scene.drawingBufferHeight / 2.0;
10141014

10151015
var ray = this.getPickRay(mousePosition, pickGlobeScratchRay);
1016-
rayIntersection = globe.pick(ray, scene, scratchRayIntersection);
1016+
rayIntersection = globe.pickWorldCoordinates(ray, scene, scratchRayIntersection);
10171017

10181018
if (scene.pickPositionSupported) {
10191019
depthIntersection = scene.pickPositionWorldCoordinates(mousePosition, scratchDepthIntersection);
@@ -2414,7 +2414,7 @@ define([
24142414
function pickMap2D(camera, windowPosition, projection, result) {
24152415
var ray = camera.getPickRay(windowPosition, pickEllipsoid2DRay);
24162416
var position = ray.origin;
2417-
position.z = 0.0;
2417+
position = Cartesian3.fromElements(position.y, position.z, 0.0, position);
24182418
var cart = projection.unproject(position);
24192419

24202420
if (cart.latitude < -CesiumMath.PI_OVER_TWO || cart.latitude > CesiumMath.PI_OVER_TWO) {
@@ -2536,7 +2536,7 @@ define([
25362536

25372537
Cartesian3.clone(camera.directionWC, result.direction);
25382538

2539-
if (camera._mode === SceneMode.COLUMBUS_VIEW) {
2539+
if (camera._mode === SceneMode.COLUMBUS_VIEW || camera._mode === SceneMode.SCENE2D) {
25402540
Cartesian3.fromElements(result.origin.z, result.origin.x, result.origin.y, result.origin);
25412541
}
25422542

Source/Scene/Globe.js

+28-6
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,11 @@ define([
407407
* @param {Ray} ray The ray to test for intersection.
408408
* @param {Scene} scene The scene.
409409
* @param {Cartesian3} [result] The object onto which to store the result.
410-
* @returns {Cartesian3|undefined} The intersection or <code>undefined</code> if none was found.
410+
* @returns {Cartesian3|undefined} The intersection or <code>undefined</code> if none was found. The returned position is in projected coordinates for 2D and Columbus View.
411411
*
412-
* @example
413-
* // find intersection of ray through a pixel and the globe
414-
* var ray = viewer.camera.getPickRay(windowCoordinates);
415-
* var intersection = globe.pick(ray, scene);
412+
* @private
416413
*/
417-
Globe.prototype.pick = function(ray, scene, result) {
414+
Globe.prototype.pickWorldCoordinates = function(ray, scene, result) {
418415
//>>includeStart('debug', pragmas.debug);
419416
if (!defined(ray)) {
420417
throw new DeveloperError('ray is required');
@@ -472,6 +469,31 @@ define([
472469
return intersection;
473470
};
474471

472+
var cartoScratch = new Cartographic();
473+
/**
474+
* Find an intersection between a ray and the globe surface that was rendered. The ray must be given in world coordinates.
475+
*
476+
* @param {Ray} ray The ray to test for intersection.
477+
* @param {Scene} scene The scene.
478+
* @param {Cartesian3} [result] The object onto which to store the result.
479+
* @returns {Cartesian3|undefined} The intersection or <code>undefined</code> if none was found.
480+
*
481+
* @example
482+
* // find intersection of ray through a pixel and the globe
483+
* var ray = viewer.camera.getPickRay(windowCoordinates);
484+
* var intersection = globe.pick(ray, scene);
485+
*/
486+
Globe.prototype.pick = function(ray, scene, result) {
487+
result = this.pickWorldCoordinates(ray, scene, result);
488+
if (defined(result) && scene.mode !== SceneMode.SCENE3D) {
489+
result = Cartesian3.fromElements(result.y, result.z, result.x, result);
490+
var carto = scene.mapProjection.unproject(result, cartoScratch);
491+
result = scene.globe.ellipsoid.cartographicToCartesian(carto, result);
492+
}
493+
494+
return result;
495+
};
496+
475497
var scratchGetHeightCartesian = new Cartesian3();
476498
var scratchGetHeightIntersection = new Cartesian3();
477499
var scratchGetHeightCartographic = new Cartographic();

Source/Scene/SceneTransitioner.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ define([
530530

531531
var globe = scene.globe;
532532
if (defined(globe)) {
533-
var pickPos = globe.pick(ray, scene, scratchCVTo2DPickPos);
533+
var pickPos = globe.pickWorldCoordinates(ray, scene, scratchCVTo2DPickPos);
534534
if (defined(pickPos)) {
535535
Matrix4.multiplyByPoint(Camera.TRANSFORM_2D_INVERSE, pickPos, endPos);
536536
endPos.z += Cartesian3.distance(startPos, endPos);
@@ -634,7 +634,7 @@ define([
634634

635635
var globe = scene.globe;
636636
if (defined(globe)) {
637-
var pickedPos = globe.pick(ray, scene, scratch3DTo2DPickPosition);
637+
var pickedPos = globe.pickWorldCoordinates(ray, scene, scratch3DTo2DPickPosition);
638638
if (defined(pickedPos)) {
639639
var height = Cartesian3.distance(camera2D.position2D, pickedPos);
640640
pickedPos.x += height;

Source/Scene/ScreenSpaceCameraController.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,12 @@ define([
512512
object._zoomMouseStart = Cartesian2.clone(startPosition, object._zoomMouseStart);
513513

514514
if (defined(object._globe)) {
515-
pickedPosition = mode !== SceneMode.SCENE2D ? pickGlobe(object, startPosition, scratchPickCartesian) : camera.getPickRay(startPosition, scratchZoomPickRay).origin;
515+
if (mode === SceneMode.SCENE2D) {
516+
pickedPosition = camera.getPickRay(startPosition, scratchZoomPickRay).origin;
517+
pickedPosition = Cartesian3.fromElements(pickedPosition.y, pickedPosition.z, pickedPosition.x);
518+
} else {
519+
pickedPosition = pickGlobe(object, startPosition, scratchPickCartesian);
520+
}
516521
}
517522
if (defined(pickedPosition)) {
518523
object._useZoomWorldPosition = true;
@@ -552,6 +557,7 @@ define([
552557

553558
if ((camera.position.x < 0.0 && savedX > 0.0) || (camera.position.x > 0.0 && savedX < 0.0)) {
554559
pickedPosition = camera.getPickRay(startPosition, scratchZoomPickRay).origin;
560+
pickedPosition = Cartesian3.fromElements(pickedPosition.y, pickedPosition.z, pickedPosition.x);
555561
object._zoomWorldPosition = Cartesian3.clone(pickedPosition, object._zoomWorldPosition);
556562
}
557563
}
@@ -692,7 +698,7 @@ define([
692698
}
693699

694700
var rayDirection = ray.direction;
695-
if (mode === SceneMode.COLUMBUS_VIEW) {
701+
if (mode === SceneMode.COLUMBUS_VIEW || mode === SceneMode.SCENE2D) {
696702
Cartesian3.fromElements(rayDirection.y, rayDirection.z, rayDirection.x, rayDirection);
697703
}
698704

@@ -716,6 +722,9 @@ define([
716722
var start = camera.getPickRay(movement.startPosition, translate2DStart).origin;
717723
var end = camera.getPickRay(movement.endPosition, translate2DEnd).origin;
718724

725+
start = Cartesian3.fromElements(start.y, start.z, start.x, start);
726+
end = Cartesian3.fromElements(end.y, end.z, end.x, end);
727+
719728
var direction = Cartesian3.subtract(start, end, scratchTranslateP0);
720729
var distance = Cartesian3.magnitude(direction);
721730

@@ -832,7 +841,7 @@ define([
832841
}
833842

834843
var ray = camera.getPickRay(mousePosition, pickGlobeScratchRay);
835-
var rayIntersection = globe.pick(ray, scene, scratchRayIntersection);
844+
var rayIntersection = globe.pickWorldCoordinates(ray, scene, scratchRayIntersection);
836845

837846
var pickDistance = defined(depthIntersection) ? Cartesian3.distance(depthIntersection, camera.positionWC) : Number.POSITIVE_INFINITY;
838847
var rayDistance = defined(rayIntersection) ? Cartesian3.distance(rayIntersection, camera.positionWC) : Number.POSITIVE_INFINITY;

Specs/Scene/CameraSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2261,7 +2261,7 @@ defineSuite([
22612261
var ray = camera.getPickRay(windowCoord);
22622262

22632263
var cameraPosition = camera.position;
2264-
var expectedPosition = new Cartesian3(cameraPosition.x + 2.0, cameraPosition.y + 2, cameraPosition.z);
2264+
var expectedPosition = new Cartesian3(cameraPosition.z, cameraPosition.x + 2.0, cameraPosition.y + 2.0);
22652265
expect(ray.origin).toEqualEpsilon(expectedPosition, CesiumMath.EPSILON14);
22662266
expect(ray.direction).toEqual(camera.directionWC);
22672267
});

Specs/Scene/ScreenSpaceCameraControllerSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ defineSuite([
6464
this.getHeight = function(cartographic) {
6565
return 0.0;
6666
};
67-
this.pick = function() {
67+
this.pickWorldCoordinates = function() {
6868
return new Cartesian3(0.0, 0.0, 1.0);
6969
};
7070
this._surface = {

0 commit comments

Comments
 (0)