3

I have some code that converts a perspective camera to an orthographic camera. The problem is that when I make the conversion, the model becomes very tiny and hard to see.

I have calculated the zoom factor for the orthographic camera, based on the distance and the FOV. Are there any other properties that I need to set on the orthographic camera (e.g. clipping plane, etc..)?

I believe the position remains the same. I'm not sure what else I need to calculate.

    fieldOfView = viewInfo.fov;
var getCameraPosition = function() {
    return viewer._viewport._implementation.getCamera()._nativeCamera.position;
};

// Calculate the delta position between the camera and the object
var getPositionDelta = function(position1, position2) {
    return {
        x: position1.x - position2.x,
        y: position1.y - position2.y,
        z: position1.z - position2.z
    }
};

var getDistance = function(positionDelta, cameraDirection) {
    return dot(positionDelta, cameraDirection);
};

distance = getDistance(positionDelta, cameraDirection),
var depth = distance;

var viewportWidth = view.getDomRef().getBoundingClientRect().width;
var viewportHeight = view.getDomRef().getBoundingClientRect().height;
var aspect = viewportWidth / viewportHeight;

var height_ortho = depth * 2 * Math.atan( fieldOfView * (Math.PI/180) / 2 )
var width_ortho  = height_ortho * aspect;

var near = viewInfo.near, far = viewInfo.far;
var newCamera = new THREE.OrthographicCamera(
    width_ortho  / -2, width_ortho   /  2,
    height_ortho /  2, height_ortho  / -2,
    near, far );


newCamera.position.copy( viewInfo.position );
var sCamera = new vk.threejs.OrthographicCamera(); //framework creatio of threejs cam

sCamera.setZoomFactor(orthoZoomFactor);
sCamera.setCameraRef(newCamera);
view.getViewport().setCamera(sCamera);

I also tried setting the same camera properties (e.g. clipping planes etc) of the perspective for the orthographic and I still had the same problem.

I guess I am missing some property or calculation required to put the object in the same position as when it was in perspective camera view.

David
  • 567
  • 4
  • 16
sarah
  • 147
  • 1
  • 3
  • 11

1 Answers1

7

Let's assume you have a perspective view with a given vertical field of view angle fov_y (in degrees) and you know the size of the viewport width and height. Furthermore, you have the near and far plane. These are the values which you use to setup the THREE.PerspectiveCamera:

perspCamera = new THREE.PerspectiveCamera( fov_y, width / height, near, far );

Also, you know the position of the object and the position of the camera. An object doesn't have only a single position, but you have to choose a representative position for its depth.

First you have to calculate the depth of the object.

var v3_object = .... // THREE.Vector3 : positon of the object
var v3_camera = perspCamera.position;

var line_of_sight = new THREE.Vector3();
perspCamera.getWorldDirection( line_of_sight );

var v3_distance = v3_object.clone().sub( v3_camera );
depth = v3_distance.dot( line_of_sight );

Then you have to calculate the "size" of the rectangle which is projected to the viewport at the depth:

enter image description here

aspect = width / height;

height_ortho = depth * 2 * Math.atan( fov_y*(Math.PI/180) / 2 )
width_ortho  = height_ortho * aspect;

With these values the THREE.OrthographicCamera can be setup like this:

var orthoCamera = new THREE.OrthographicCamera(
    width_ortho  / -2, width_ortho   /  2,
    height_ortho /  2, height_ortho  / -2,
    near, far );
orthoCamera.position.copy( perspCamera.position ); 



The positon and direction of the perspective camera can be committed to the orthographic camera like this:

orthoCamera.position.copy( perspCamera.position );
orthoCamera.quaternion.copy( perspCamera.quaternion ); 

See also stackoverflow question Three.js - Find the current LookAt of a camera?

Eirah
  • 5
  • 1
  • 4
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thank you for your reply. I have tried this but my model moves back. the zoom is very far. am i missing something? – sarah Feb 13 '18 at 20:15
  • If i rotate my model when in perspective view and switch back to orthographic view, my model disappears. – sarah Feb 14 '18 at 00:40
  • not sure how to. can you please share code to update position and lookAt. thanks – sarah Feb 14 '18 at 07:24
  • Thanks. is the lookAt position the same for perspective and ortho? will it always be 0,0,-1? – sarah Feb 14 '18 at 09:23
  • @sarah Yes, see [Three.js - Find the current LookAt of a camera?](https://stackoverflow.com/questions/27957645/three-js-find-the-current-lookat-of-a-camera) – Rabbid76 Feb 14 '18 at 09:26
  • thank you this resolves the rotation. but still my model shrinks. please see images in edited question. thanks – sarah Feb 14 '18 at 19:41
  • 1
    @Rabbid76 Did you test this code? See [this](https://stackoverflow.com/questions/26905929/three-js-2xmeshes-using-same-vector-as-position/26916159#26916159). Also, just do this: `cam2.quaternion.copy( cam1.quaternion )`. – WestLangley Feb 15 '18 at 02:10
  • 2
    @Rabbid76 `orthoCamera.position.copy( perspCamera.position ); orthoCamera.quaternion.copy( perspCamera.quaternion );` – WestLangley Feb 15 '18 at 07:05
  • @WestLangley Thank you, again – Rabbid76 Feb 15 '18 at 07:13