three.js orthographic camera: zoom all for a cube with perspective

2.8k views Asked by At

I have developed a simple three.js application that renders a cube. I have created three files: index.html, viewer_style.css and viewer.js.

The content of index.html is the following:

<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
        <title>My first three.js app</title>
        <link rel='stylesheet' type='text/css' href='viewer_style.css'>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="js/three.js"></script>
        <script src="js/controls/OrbitControls.js"></script>
        <script src="js/renderers/Projector.js"></script>
    </head>
    <body>
        <script src="viewer.js"></script>
    </body>
</html>

And the content of viewer.js is the following:

// SCENE
var scene = new THREE.Scene();

// CAMERA
var frustumHeight;
var aspect = window.innerWidth / window.innerHeight;
var camera = new THREE.OrthographicCamera(- frustumHeight * aspect / 2, frustumHeight * aspect / 2, frustumHeight / 2, - frustumHeight / 2, 1, 2000 );
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 100;

// CUBE        
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshPhongMaterial({color: 0xbaf5e8, flatShading: true});
var cube = new THREE.Mesh( geometry, material );
cube.receiveShadow = true;
scene.add(cube);

// BOUNDING BOX
var helper_bbox = new THREE.BoxHelper(cube);
helper_bbox.update();
scene.add(helper_bbox);

// AXES
var helper_axes = new THREE.AxisHelper();
scene.add(helper_axes);

// FIT ALL:
var bbox_radius = helper_bbox.geometry.boundingSphere.radius;
if(aspect < 1){
    frustumHeight = 2 * bbox_radius;
}
else{
    frustumHeight = 2 * bbox_radius / aspect;
}
camera.left = - frustumHeight * aspect / 2;
camera.right = frustumHeight * aspect / 2;
camera.top = frustumHeight / 2;
camera.bottom = - frustumHeight / 2;
camera.updateProjectionMatrix();

// RENDERER
var renderer = new THREE.WebGLRenderer({alpha: true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;

// LIGHTS
var ambientLight = new THREE.AmbientLight(0x101010, 1.0); 
scene.add(ambientLight); 
directionalLight = new THREE.DirectionalLight(0xffffff, 1.0); 
directionalLight.position.set(1.0, 1.0, 1.0).normalize(); 
scene.add(directionalLight); 
directionalLight_2 = new THREE.DirectionalLight(0xffffff, 1.0); 
directionalLight_2.position.set(-1.0, -1.0, 1.0).normalize(); 
scene.add(directionalLight_2); 

// CONTROLS
document.body.appendChild(renderer.domElement);
var controls = new THREE.OrbitControls(camera);

window.addEventListener( 'resize', onWindowResize, false );

function animate(){
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}

function onWindowResize(){   
    var aspect = window.innerWidth / window.innerHeight;
    camera.left   = - frustumHeight * aspect / 2;
    camera.right  =   frustumHeight * aspect / 2;
    camera.top    =   frustumHeight / 2;
    camera.bottom = - frustumHeight / 2;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);  
    camera.lookAt(scene.position);
}

animate();

I intend to fit the cube to the scene with a certain margin using the radius of the bounding sphere of the cube. It seems to work correctly, as it can be seen in the following image:

enter image description here

However, I changed the camera position in viewer.js to the following:

camera.position.x = 100;
camera.position.y = 100;
camera.position.z = 100;

In this situation, I get something like this:

enter image description here

In this case, the cube is not fitted to the screen. I think this is due to the fact that I am measuring the radius of the bounding sphere in the wrong reference system. However, I have not been able to find a solution to this issue.

Does anyone know what I am doing wrong? Am I using the right approach? Thanks in advance.

1

There are 1 answers

1
Holger Ludvigsen On BEST ANSWER

Your code is correct if you change aspect < 1 to aspect > 1, as suggested. Here is a jsfiddle with your code that demonstrates this: https://jsfiddle.net/holgerl/kk5h39qa/