Javascript 如何将正交摄影机转换为透视和反向?
我正在使用combinedcamera.js和orbitcontrols.js 我有它的工作,以便相机可以切换,都可以放大和缩小。 但是,orbitcontrols会重新定位透视摄影机以模拟缩放,而正交摄影机不会这样做(它会更改fov等) 这最终会导致摄影机远平截头体平面在透视模式下移动(我希望如此),而它不会在正交模式下移动(我希望它移动) 我已经解决了这个问题,通过重新定位透视和正交摄影机。正交摄影机不使用其位置来确定缩放,因此存在问题 问题是,当我在相机之间切换时,它们似乎没有相同的缩放量Javascript 如何将正交摄影机转换为透视和反向?,javascript,camera,three.js,perspective,orthographic,Javascript,Camera,Three.js,Perspective,Orthographic,我正在使用combinedcamera.js和orbitcontrols.js 我有它的工作,以便相机可以切换,都可以放大和缩小。 但是,orbitcontrols会重新定位透视摄影机以模拟缩放,而正交摄影机不会这样做(它会更改fov等) 这最终会导致摄影机远平截头体平面在透视模式下移动(我希望如此),而它不会在正交模式下移动(我希望它移动) 我已经解决了这个问题,通过重新定位透视和正交摄影机。正交摄影机不使用其位置来确定缩放,因此存在问题 问题是,当我在相机之间切换时,它们似乎没有相同的缩放
我想问题是,如何让正交摄影机依靠摄影机位置来确定缩放量,使其看起来始终具有与透视摄影机相似的缩放效果?好吧,在进行了大量实验后,我找到了一种非常简单的方法来获得足够接近的结果。 我意外地发现,如果将“远视锥”值设置为25,它将工作得非常完美。。所以我做了一个等式来补偿,如果值不同。这已经足够近了,但也许有人能看到哪里可以改进 我在combinedcamera.js中替换了
halfHeight /= this.zoom;
halfWidth /= this.zoom;
与
我在combinedcamera.js中添加了这一行
this.cameraO.far = this.cameraP.far+((this.cameraP.far/25)*this.zoom)-0.5;
就在这之前
this.cameraO.updateProjectionMatrix();
这是完整的部分
THREE.CombinedCamera.prototype.toOrthographic = function () {
// Switches to the Orthographic camera estimating viewport from Perspective
var fov = this.fov;
var aspect = this.cameraP.aspect;
var near = this.cameraP.near;
var far = this.cameraP.far;
// The size that we set is the mid plane of the viewing frustum
var hyperfocus = ( near + far ) / 2;
var halfHeight = Math.tan( fov * Math.PI / 180 / 2 ) * hyperfocus;
var planeHeight = 2 * halfHeight;
var planeWidth = planeHeight * aspect;
var halfWidth = planeWidth / 2;
halfHeight /= ((this.cameraP.far/25)*this.zoom);
halfWidth /= ((this.cameraP.far/25)*this.zoom);
this.cameraO.left = -halfWidth;
this.cameraO.right = halfWidth;
this.cameraO.top = halfHeight;
this.cameraO.bottom = -halfHeight;
// this.cameraO.left = -farHalfWidth;
// this.cameraO.right = farHalfWidth;
// this.cameraO.top = farHalfHeight;
// this.cameraO.bottom = -farHalfHeight;
// this.cameraO.left = this.left / this.zoom;
// this.cameraO.right = this.right / this.zoom;
// this.cameraO.top = this.top / this.zoom;
// this.cameraO.bottom = this.bottom / this.zoom;
this.cameraO.far = this.cameraP.far+((this.cameraP.far/25)*this.zoom)-0.5;
this.cameraO.updateProjectionMatrix();
this.near = this.cameraO.near;
this.far = this.cameraO.far;
this.projectionMatrix = this.cameraO.projectionMatrix;
this.inPerspectiveMode = false;
this.inOrthographicMode = true;
};
我还将透视摄影机的this.zoom更改为仅1
THREE.CombinedCamera.prototype.toPerspective = function () {
// Switches to the Perspective Camera
this.near = this.cameraP.near;
this.far = this.cameraP.far;
this.cameraP.fov = this.fov / 1 ;
this.cameraP.updateProjectionMatrix();
this.projectionMatrix = this.cameraP.projectionMatrix;
this.inPerspectiveMode = true;
this.inOrthographicMode = false;
};
此外,我还必须调整轨道控制装置
this.dollyIn = function ( dollyScale ) {
if ( dollyScale === undefined ) {
dollyScale = getZoomScale();
}
scale /= dollyScale;
scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom * dollyScale ) );
};
this.dollyOut = function ( dollyScale ) {
if ( dollyScale === undefined ) {
dollyScale = getZoomScale();
}
scale *= dollyScale;
scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / dollyScale ) );
};
this.dollyIn = function ( dollyScale ) {
if ( dollyScale === undefined ) {
dollyScale = getZoomScale();
}
scale /= dollyScale;
scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom * dollyScale ) );
};
this.dollyOut = function ( dollyScale ) {
if ( dollyScale === undefined ) {
dollyScale = getZoomScale();
}
scale *= dollyScale;
scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / dollyScale ) );
};