From a9528482e61c2e186cc3379ccd59b5931a0dbd71 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Wed, 27 May 2020 00:41:14 -0400 Subject: [PATCH] Fixes for CV --- Source/Scene/ScreenSpaceCameraController.js | 101 +++++++++++++++----- 1 file changed, 75 insertions(+), 26 deletions(-) diff --git a/Source/Scene/ScreenSpaceCameraController.js b/Source/Scene/ScreenSpaceCameraController.js index e771f0fd437e..53a0687df675 100644 --- a/Source/Scene/ScreenSpaceCameraController.js +++ b/Source/Scene/ScreenSpaceCameraController.js @@ -1179,14 +1179,29 @@ function getZoomDistanceUnderground(controller, ray, height) { var scratchCartographic = new Cartographic(); -function getDistanceFromClosestSurface(controller, position) { +function getHeight(controller) { var ellipsoid = controller._ellipsoid; - var cartographic = ellipsoid.cartesianToCartographic( - position, - scratchCartographic - ); - var height = cartographic.height; + var scene = controller._scene; + var camera = scene.camera; + var mode = scene.mode; + var height = 0.0; + if (mode === SceneMode.SCENE3D) { + var cartographic = ellipsoid.cartesianToCartographic( + camera.position, + scratchCartographic + ); + if (defined(cartographic)) { + height = cartographic.height; + } + } else { + height = camera.position.z; + } + return height; +} + +function getDistanceFromClosestSurface(controller) { + var height = getHeight(controller); var globeHeight = defaultValue(controller._globeHeight, 0.0); var distanceFromSurface = Math.abs(height - globeHeight); var distanceFromUndergroundSurface = Math.abs( @@ -1243,6 +1258,7 @@ function translateCV(controller, startPosition, movement) { var scene = controller._scene; var camera = scene.camera; + var cameraUnderground = controller._cameraUnderground; var startMouse = Cartesian2.clone( movement.startPosition, translateCVStartMouse @@ -1261,7 +1277,18 @@ function translateCV(controller, startPosition, movement) { } } - if (origin.x > camera.position.z && defined(globePos)) { + if ( + (cameraUnderground || origin.x > camera.position.z) && + defined(globePos) + ) { + if (cameraUnderground) { + getStrafeStartPositionUnderground( + controller, + startRay, + globePos, + globePos + ); + } Cartesian2.clone(startPosition, controller._strafeMousePosition); Cartesian2.clone(startPosition, controller._strafeEndMousePosition); Cartesian3.clone(globePos, controller._strafeStartPosition); @@ -1420,6 +1447,7 @@ function rotateCVOnPlane(controller, startPosition, movement) { function rotateCVOnTerrain(controller, startPosition, movement) { var scene = controller._scene; var camera = scene.camera; + var cameraUnderground = controller._cameraUnderground; var center; var ray; @@ -1454,6 +1482,13 @@ function rotateCVOnTerrain(controller, startPosition, movement) { Cartesian3.add(position, center, center); } + if (controller._cameraUnderground) { + if (!defined(ray)) { + ray = camera.getPickRay(startPosition, rotateCVWindowRay); + } + getTiltCenterUnderground(controller, ray, center, center); + } + Cartesian2.clone(startPosition, controller._tiltCenterMousePosition); Cartesian3.clone(center, controller._tiltCenter); } @@ -1531,7 +1566,11 @@ function rotateCVOnTerrain(controller, startPosition, movement) { camera._setTransform(verticalTransform); if (dot < 0.0) { - if (movement.startPosition.y > movement.endPosition.y) { + var movementDelta = movement.startPosition.y - movement.endPosition.y; + if ( + (cameraUnderground && movementDelta < 0.0) || + (!cameraUnderground && movementDelta > 0.0) + ) { constrainedAxis = undefined; } @@ -1654,10 +1693,6 @@ function zoomCV(controller, startPosition, movement) { var distance; if (defined(intersection)) { distance = Cartesian3.distance(position, intersection); - } else { - var normal = Cartesian3.UNIT_X; - distance = - -Cartesian3.dot(normal, position) / Cartesian3.dot(normal, direction); } if (cameraUnderground) { @@ -1666,7 +1701,17 @@ function zoomCV(controller, startPosition, movement) { ray, height ); - distance = Math.min(distance, distanceUnderground); + if (defined(distance)) { + distance = Math.min(distance, distanceUnderground); + } else { + distance = distanceUnderground; + } + } + + if (!defined(distance)) { + var normal = Cartesian3.UNIT_X; + distance = + -Cartesian3.dot(normal, position) / Cartesian3.dot(normal, direction); } handleZoom( @@ -1796,17 +1841,18 @@ function strafe(controller, movement, strafeStartPosition) { Cartesian3.add(camera.position, direction, camera.position); } -var scratchCartographic = new Cartographic(); var scratchRadii = new Cartesian3(); var scratchEllipsoid = new Ellipsoid(); -var scratchIntersectionPoint = new Cartesian3(); function getClosestPickDistance(controller, ray, pickedPosition) { - var ellipsoid = controller._ellipsoid; var origin = ray.origin; - var distance = Cartesian3.distance(origin, pickedPosition); + if (controller._scene.mode !== SceneMode.SCENE3D) { + return distance; + } + + var ellipsoid = controller._ellipsoid; var heightDelta = controller.undergroundSurfaceHeight; var innerRadii = Cartesian3.fromElements( ellipsoid.radii.x + heightDelta, @@ -1842,7 +1888,7 @@ function getStrafeStartPositionUnderground( // If the picked position is too far away set the strafe speed based on the // camera's height from the closest surface (closest surface being either // the globe surface or the underground invisible surface) - distance = getDistanceFromClosestSurface(controller, ray.origin); + distance = getDistanceFromClosestSurface(controller); } return Ray.getPoint(ray, distance, result); } @@ -2179,8 +2225,6 @@ function zoom3D(controller, startPosition, movement) { var distance; if (defined(intersection)) { distance = Cartesian3.distance(ray.origin, intersection); - } else { - distance = height; } if (cameraUnderground) { @@ -2189,7 +2233,15 @@ function zoom3D(controller, startPosition, movement) { ray, height ); - distance = Math.min(distance, distanceUnderground); + if (defined(distance)) { + distance = Math.min(distance, distanceUnderground); + } else { + distance = distanceUnderground; + } + } + + if (!defined(distance)) { + distance = height; } var unitPosition = Cartesian3.normalize(camera.position, zoom3DUnitPosition); @@ -2343,10 +2395,7 @@ function tilt3DOnEllipsoid(controller, startPosition, movement) { } function getTiltCenterUnderground(controller, ray, pickedPosition, result) { - var distanceFromClosestSurface = getDistanceFromClosestSurface( - controller, - ray.origin - ); + var distanceFromClosestSurface = getDistanceFromClosestSurface(controller); var maximumDistance = CesiumMath.clamp( distanceFromClosestSurface * 5.0, @@ -2402,7 +2451,7 @@ function tilt3DOnTerrain(controller, startPosition, movement) { center = Ray.getPoint(ray, intersection.start, tilt3DCenter); } - if (controller._cameraUnderground) { + if (cameraUnderground) { if (!defined(ray)) { ray = camera.getPickRay(startPosition, tilt3DRay); }