3

我正在寻找一个函数来确保给定的盒子或球体将在 WebGL 画布中可见,并且它将适合画布区域。我正在使用透视相机,相机已经指向物体的中间。我知道这可以通过更改 FOV 角度或沿视轴移动相机来实现。

知道如何使用 ThreeJS 实现这一点吗?

4

2 回答 2

2

这就是我最终实现它的方式:

var camera = new THREE.PerspectiveCamera(35,1,1, 100000);
var controls = new THREE.TrackballControls( me.camera , container);
[...]


/**
 * point the current camera to the center
 * of the graphical object (zoom factor is not affected)
 *
 * the camera is moved in its  x,z plane so that the orientation 
 * is not affected either
 */
function pointCameraTo (node) {

    var me = this;

    // Refocus camera to the center of the new object
    var COG = shapeCenterOfGravity(node);

    var v = new THREE.Vector3();
    v.subVectors(COG,me.controls.target);

    camera.position.addVectors(camera.position,v);

    // retrieve camera orientation and pass it to trackball  
    camera.lookAt(COG);
    controls.target.set( COG.x,COG.y,COG.z );  

};

/**
 *  Zoom to object
 */
function zoomObject (node) {

   var me = this; 

   var bbox = boundingBox(node);
   if (bbox.empty()) {
       return;
   }
   var COG =  bbox.center();

   pointCameraTo(node);

   var sphereSize = bbox.size().length() * 0.5;
   var distToCenter = sphereSize/Math.sin( Math.PI / 180.0 * me.camera.fov * 0.5);

   // move the camera backward 

   var target = controls.target;
   var vec = new THREE.Vector3();
   vec.subVectors( camera.position, target );
   vec.setLength( distToCenter );
   camera.position.addVectors(  vec , target );
   camera.updateProjectionMatrix();
   render3D();
};

这方面的一个例子可以在https://github.com/OpenWebCAD/node-occ-geomview/blob/master/client/geom_view.js中看到

于 2013-05-23T06:21:38.123 回答
0

这是我的做法{使用轨迹球放大/缩小平移等}

function init(){
......
......
controls = new THREE.TrackballControls(camera);

controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;

controls.noZoom = false;
controls.noPan = false;

controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;

controls.keys = [65, 83, 68];

controls.addEventListener('change', render);

.....
.....
render(scene, camera);
animate();
}
function render() 

{
    renderer.render(scene, camera);
    //stats.update();
}

function animate() {
    requestAnimationFrame(animate);
    controls.update();
}
于 2014-10-31T04:42:10.383 回答