From 03969580de0275f5bf4e4e1d3bc9785f5c560116 Mon Sep 17 00:00:00 2001 From: hpinkos Date: Tue, 31 Jul 2018 16:04:00 -0400 Subject: [PATCH] globe pick wc --- Source/Scene/Camera.js | 2 +- Source/Scene/Globe.js | 34 +++++++++++++++---- Source/Scene/SceneTransitioner.js | 4 +-- Source/Scene/ScreenSpaceCameraController.js | 2 +- .../Scene/ScreenSpaceCameraControllerSpec.js | 2 +- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Source/Scene/Camera.js b/Source/Scene/Camera.js index 50b48178c9d3..fcefe4ad9aff 100644 --- a/Source/Scene/Camera.js +++ b/Source/Scene/Camera.js @@ -1013,7 +1013,7 @@ define([ mousePosition.y = scene.drawingBufferHeight / 2.0; var ray = this.getPickRay(mousePosition, pickGlobeScratchRay); - rayIntersection = globe.pick(ray, scene, scratchRayIntersection); + rayIntersection = globe.pickWorldCoordinates(ray, scene, scratchRayIntersection); if (scene.pickPositionSupported) { depthIntersection = scene.pickPositionWorldCoordinates(mousePosition, scratchDepthIntersection); diff --git a/Source/Scene/Globe.js b/Source/Scene/Globe.js index 29b77bbac8f5..5acc74e2413f 100644 --- a/Source/Scene/Globe.js +++ b/Source/Scene/Globe.js @@ -407,14 +407,11 @@ define([ * @param {Ray} ray The ray to test for intersection. * @param {Scene} scene The scene. * @param {Cartesian3} [result] The object onto which to store the result. - * @returns {Cartesian3|undefined} The intersection or undefined if none was found. + * @returns {Cartesian3|undefined} The intersection or undefined if none was found. The returned position is in projected coordinates for 2D and Columbus View. * - * @example - * // find intersection of ray through a pixel and the globe - * var ray = viewer.camera.getPickRay(windowCoordinates); - * var intersection = globe.pick(ray, scene); + * @private */ - Globe.prototype.pick = function(ray, scene, result) { + Globe.prototype.pickWorldCoordinates = function(ray, scene, result) { //>>includeStart('debug', pragmas.debug); if (!defined(ray)) { throw new DeveloperError('ray is required'); @@ -472,6 +469,31 @@ define([ return intersection; }; + var cartoScratch = new Cartographic(); + /** + * Find an intersection between a ray and the globe surface that was rendered. The ray must be given in world coordinates. + * + * @param {Ray} ray The ray to test for intersection. + * @param {Scene} scene The scene. + * @param {Cartesian3} [result] The object onto which to store the result. + * @returns {Cartesian3|undefined} The intersection or undefined if none was found. + * + * @example + * // find intersection of ray through a pixel and the globe + * var ray = viewer.camera.getPickRay(windowCoordinates); + * var intersection = globe.pick(ray, scene); + */ + Globe.prototype.pick = function(ray, scene, result) { + result = this.pickWorldCoordinates(ray, scene, result); + if (defined(result) && scene.mode !== SceneMode.SCENE3D) { + result = Cartesian3.fromElements(result.y, result.z, result.x, result); + var carto = scene.mapProjection.unproject(result, cartoScratch); + result = scene.globe.ellipsoid.cartographicToCartesian(carto, result); + } + + return result; + }; + var scratchGetHeightCartesian = new Cartesian3(); var scratchGetHeightIntersection = new Cartesian3(); var scratchGetHeightCartographic = new Cartographic(); diff --git a/Source/Scene/SceneTransitioner.js b/Source/Scene/SceneTransitioner.js index b69d36c93c8d..457e16bedbde 100644 --- a/Source/Scene/SceneTransitioner.js +++ b/Source/Scene/SceneTransitioner.js @@ -530,7 +530,7 @@ define([ var globe = scene.globe; if (defined(globe)) { - var pickPos = globe.pick(ray, scene, scratchCVTo2DPickPos); + var pickPos = globe.pickWorldCoordinates(ray, scene, scratchCVTo2DPickPos); if (defined(pickPos)) { Matrix4.multiplyByPoint(Camera.TRANSFORM_2D_INVERSE, pickPos, endPos); endPos.z += Cartesian3.distance(startPos, endPos); @@ -634,7 +634,7 @@ define([ var globe = scene.globe; if (defined(globe)) { - var pickedPos = globe.pick(ray, scene, scratch3DTo2DPickPosition); + var pickedPos = globe.pickWorldCoordinates(ray, scene, scratch3DTo2DPickPosition); if (defined(pickedPos)) { var height = Cartesian3.distance(camera2D.position2D, pickedPos); pickedPos.x += height; diff --git a/Source/Scene/ScreenSpaceCameraController.js b/Source/Scene/ScreenSpaceCameraController.js index 02037bbbb2e7..319e30b9c811 100644 --- a/Source/Scene/ScreenSpaceCameraController.js +++ b/Source/Scene/ScreenSpaceCameraController.js @@ -832,7 +832,7 @@ define([ } var ray = camera.getPickRay(mousePosition, pickGlobeScratchRay); - var rayIntersection = globe.pick(ray, scene, scratchRayIntersection); + var rayIntersection = globe.pickWorldCoordinates(ray, scene, scratchRayIntersection); var pickDistance = defined(depthIntersection) ? Cartesian3.distance(depthIntersection, camera.positionWC) : Number.POSITIVE_INFINITY; var rayDistance = defined(rayIntersection) ? Cartesian3.distance(rayIntersection, camera.positionWC) : Number.POSITIVE_INFINITY; diff --git a/Specs/Scene/ScreenSpaceCameraControllerSpec.js b/Specs/Scene/ScreenSpaceCameraControllerSpec.js index 6b71846515ae..c83d06b879f4 100644 --- a/Specs/Scene/ScreenSpaceCameraControllerSpec.js +++ b/Specs/Scene/ScreenSpaceCameraControllerSpec.js @@ -64,7 +64,7 @@ defineSuite([ this.getHeight = function(cartographic) { return 0.0; }; - this.pick = function() { + this.pickWorldCoordinates = function() { return new Cartesian3(0.0, 0.0, 1.0); }; this._surface = {