gpt4 book ai didi

camera - 三个JS。如何实现 ZoomALL 并确保给定的框填充 Canvas 区域?

转载 作者:行者123 更新时间:2023-12-02 02:04:30 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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 中看到。

关于camera - 三个JS。如何实现 ZoomALL 并确保给定的框填充 Canvas 区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15761644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com