1

My problem now is that when I switch from an orthogonal camera to a perspective camera, I can't always calculate the camera's position correctly. Here is my code:

function toPerspective() {
        var fov_y = 60;
        var depht_s = Math.tan(fov_y / 2.0 * Math.PI / 180.0) * 2.0;
        var size_y = cameraOrtho.top * 2;
        var Z = size_y / depht_s;
        var normal = cameraPerspective.position.normalize();
        cameraPerspective.position.copy(normal.multiplyScalar(Z));
        //controls.update();
        cameraPerspective.updateProjectionMatrix();

    };
function toOrthographic() {

        var fov_y = 60;
        var depht_s = Math.tan(fov_y / 2.0 * Math.PI / 180.0) * 2.0;
        var Z = activeCamera.position.distanceTo(controls.target);
        var size_y = depht_s * Z;
        var size_x = depht_s * Z * aspect;

        cameraOrtho.left = -size_x / 2;
        cameraOrtho.right = size_x / 2;
        cameraOrtho.top = size_y / 2;
        cameraOrtho.bottom = -size_y / 2;
        cameraOrtho.zoom = 1;
        //cameraOrtho.updateProjectionMatrix();
    };
function onKeyDown(event) {

        switch (event.keyCode) {
            case 79: /*O*/
                if (type === "Ortho") return;

                var old = controls.target.clone();
                cameraOrtho.position.copy(cameraPerspective.position);
                activeCamera = cameraOrtho;
                controls.object = activeCamera;
                toOrthographic();
                controls.update();
                activeCamera.updateProjectionMatrix();
                type = "Ortho"
                break;

            case 80: /*P*/
                if (type === "Perspective") return;
                var old = controls.target.clone();
                cameraPerspective.position.copy(cameraOrtho.position);
                activeCamera = cameraPerspective;
                controls.object = activeCamera;
                toPerspective();
                controls.update();
                activeCamera.updateProjectionMatrix();
                type = "Perspective"
                break;
        }
        console.log(activeCamera);

    }

Perspective camera switch to orthogonal camera method from this link: How to switch between Perspective and Orthographic cameras keeping size of desired object.

With the perspective camera, using OrbitControls to right-click the Pan, then switching to the orthogonal camera again will show the offset. But I don't know what the problem is.I hope someone can help me, thank you!

孟Mark
  • 11
  • 2

0 Answers0