2D to 3D rotation of SceneView

649
0
07-12-2021 04:28 AM
maglourenco
New Contributor III

Greetings everyone,

I'm trying to implement a functionality where the user can change between a 2D and 3D visualization on a SceneView (sort of like what Google Maps allows to do).

The way I'm rotating the screen is based on the SceneView center point projected on the ground, which is obviously not ideal. This creates a problem when the user rotates the screen so that the center of the screen does not hit the ground and in that case, I've to manually rotate based on the current camera location, which creates a strange user experience.

I'll provide the code, if someone knows a better way of doing this please share, it will be very much appreciated.

Thank you very much!

 

FloatingActionButton change2D3DView = findViewById(R.id.change2D3DView);
        change2D3DView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                float centreX = mSceneView.getX() + mSceneView.getWidth()  / 2;
                float centreY = mSceneView.getY() + mSceneView.getHeight() / 2;

                android.graphics.Point screenPoint = new android.graphics.Point(Math.round(centreX), Math.round(centreY));
                Point mapPoint = mSceneView.screenToBaseSurface(screenPoint);

                // In case the center of the screen does not hit ground
                if (mapPoint == null) {

                    Point groundLevel = new Point(mSceneView.getCurrentViewpointCamera().getLocation().getX(),
                            mSceneView.getCurrentViewpointCamera().getLocation().getY(), 0.0, SpatialReferences.getWgs84());

                    // Turn to 2D - pitch a 0
                    mOrbitLocationCameraController = new OrbitLocationCameraController(groundLevel, 2000);
                    mSceneView.setCameraController(mOrbitLocationCameraController);

                    // Get current pitch angle
                    double currentPitch = mSceneView.getCurrentViewpointCamera().getPitch();

                    mOrbitLocationCameraController.moveCameraAsync(0, 0, (0.0 - currentPitch), 1f).addDoneListener(new Runnable() {
                        @Override
                        public void run() {
                            mSceneView.setCameraController(new GlobeCameraController());
                            map_2d_mode = true;
                        }
                    });

                    return;
                }
                
                Point tempGroundLevel = (Point) GeometryEngine.project(mapPoint, SpatialReferences.getWgs84());
                Point groundLevel = new Point(tempGroundLevel.getX(), tempGroundLevel.getY(), 0.0, SpatialReferences.getWgs84());

                if (map_2d_mode) {
                    // Turn to 3D - set pitch to 45

                    // Get current pitch angle
                    double currentPitch = mSceneView.getCurrentViewpointCamera().getPitch();

                    Camera newCamera = mSceneView.getCurrentViewpointCamera().rotateAround(groundLevel,0, ((45.0 - currentPitch) < 0.0) ? 0.0 : (45.0 - currentPitch), 0);
                    mSceneView.setViewpointCameraAsync(newCamera, 0.7f).addDoneListener(new Runnable() {
                        @Override
                        public void run() {
                            map_2d_mode = false;
                        }
                    });

                } else {
                    // Turn to 2D - set pitch to 0

                    // Get current pitch angle
                    double currentPitch = mSceneView.getCurrentViewpointCamera().getPitch();

                    Camera newCamera = mSceneView.getCurrentViewpointCamera().rotateAround(groundLevel,0, (0.0 - currentPitch), 0);
                    mSceneView.setViewpointCameraAsync(newCamera, 0.7f).addDoneListener(new Runnable() {
                        @Override
                        public void run() {
                            map_2d_mode = true;
                        }
                    });

                }
            }
        });

 

Tags (2)
0 Kudos
0 Replies