cesium: Camera.computeViewRectangle doesn't work in 2D or CV

Reported on the forum: https://groups.google.com/forum/?hl=en#!topic/cesium-dev/Ildibeod608

var viewer = new Cesium.Viewer('cesiumContainer', {
    sceneMode: Cesium.SceneMode.SCENE2D
});

viewer.scene.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(-90, 43, 50000),
    duration: 0,
});

viewer.camera.moveEnd.addEventListener(logViewRectangle);

function logViewRectangle() {
    console.log('camera.computeViewRectangle() is ' + JSON.stringify(viewer.scene.camera.computeViewRectangle()));
}

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Reactions: 2
  • Comments: 23 (13 by maintainers)

Most upvoted comments

I’ve used this. (Cesium.Rectangle)

var rect = viewer.camera.computeViewRectangle(viewer.scene.globe.ellipsoid, scratchRectangle);
        
if(rect == undefined)
{
	var cl2 = new Cesium.Cartesian2(0, 0);
	var leftTop = viewer.scene.camera.pickEllipsoid(cl2, ellipsoid);
		
	var cr2 = new Cesium.Cartesian2(viewer.scene.canvas.width, viewer.scene.canvas.height);
	var rightDown = viewer.scene.camera.pickEllipsoid(cr2, ellipsoid);
	
	leftTop = ellipsoid.cartesianToCartographic(leftTop);
	rightDown = ellipsoid.cartesianToCartographic(rightDown);
	rect = new Cesium.Rectangle(leftTop.longitude, rightDown.latitude, rightDown.longitude, leftTop.latitude);
}

I just ran into this issue as well. Are there any updates on a fix?

You can see in my workaround a few posts back that I use camera.pickEllipsoid to find a Cartesian3 on the surface based on screen-space (pixel) coordinates. I use the edges of the canvas, but you could use some fractional offset to determine if, say, more than 20% of the screen on one side is empty space. viewer.scene.camera.pickEllipsoid(new Cesium.Cartesian2(pixWidth * 0.2, pixHeight * 0.2)) would give you a Cartesian3 of a point 20% of the way in from the left side of the canvas, and 20% down from the top. If you get an undefined result, it means that you’ve zoomed out enough that this point is out in space, not over the globe.