Three.js, center camera to view all objects that is in the scene

2.3k views Asked by At

I write an algorithm that can explode (fold and unfold) mechanical set by double clicking on then.

But i want to move the camera backward or forward after that to see all my objects in the fov. I'm trying to use frustum to calculate intersection between frustum and objects, but i don't undestand how to use planes. I'm working with OrthographicCamera.

What i do :

  1. At every frame i recalculate the new frustum (when camera move):

    projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
    frustum.setFromMatrix(projScreenMatrix);
    
  2. Then i do a loop over the 6 planes and bounding box of all objects in the scene :

    for (var i = 0; i < planes.length; i++) {
            var plane = planes[i];
            for (var j = 0; j < boxs.length; j++) {
                var box = boxs[j];
                var line = new THREE.Line3(box.min, nox.max);
                //console.log({'plane': plane, 'line': line});
                if (plane.isIntersectionLine(line))
                    // move camera
            };
    };
    

but plane.isIntersectionLine(line) is always false.

Do you have any ideas ?

Thanks

0

There are 0 answers