1

目前,我正在获取对象边界框的每个角并将其转换为标准化设备坐标 (NDC),并跟踪最大和最小 NDC。然后我计算 NDC 的中间值,在世界上找到它并让我的相机观察它。

<Determine max and minimum NDCs>

centerX = (maxX + minX) / 2;
centerY = (maxY + minY) / 2;

point.set(centerX, centerY, 0);
projector.unprojectVector(point, camera);
direction = point.sub(camera.position).normalize();
point = camera.position.clone().add(direction.multiplyScalar(distance));

camera.lookAt(point);
camera.updateMatrixWorld();

这是一个近似的方法对吗?我在几个地方看到了它的建议。我问是因为每次我将对象居中时,当再次计算它们时(在进行任何其他更改之前),最小和最大 NDC 应该相等,但它们不是。我接近但不相等的数字(忽略负号),随着我越来越近,数字之间的“错误”变得越来越大。IE前几个中心的错误是:0.0022566539084770687、0.00541687811360958、0.011035676399427596、0.025670088917273515、0.06396864345885889等。

有没有我遗漏的步骤会导致这种情况?

我将此代码用作 while 循环的一部分,以在屏幕上最大化和居中对象。(我正在对其进行编程,以便用户可以输入一个航向和一个标高,并且相机将被定位,以便它在该航向和标高处查看对象。几周后,我确定(现在)它更容易这样做。)

然而,当我将相机靠近我的物体时,这似乎开始分崩离析。例如,经过几次迭代后,我的最大 X NDC 为 0.9989318709122867,我的最小 X NDC 为 -0.9552042384799428。但是,当我查看计算点时,我看起来太靠右了,在下一次迭代中,我的最大 X NDC 是 0.9420058636660581,我的最小 X NDC 是 1.0128126740876888。

4

2 回答 2

1

Your approach to this problem is incorrect. Rather than thinking about this in terms of screen coordinates, think about it terms of the scene.

You need to work out how much the camera needs to move so that a ray from it hits the centre of the object. Imagine you are standing in a field and opposite you are two people Alex and Burt, Burt is standing 2 meters to the right of Alex. You are currently looking directly at Alex but want to look at Burt without turning. If you know the distance and direction between them, 2 meters and to the right. You merely need to move that distance and direction, i.e. right and 2 meters.

In a mathematical context you need to do the following:

Get the centre of the object you are focusing on in 3d space, and then project a plane parallel to your camera, i.e. a tangent to the direction the camera is facing, which sits on that point.

Next from your camera raycast to the plane in the direction the camera is facing, the resultant difference between the centre point of the object and the point you hit the plane from the camera is the amount you need to move the camera. This should work irrespective of the direction or position of the camera and object.

于 2013-08-09T08:04:45.303 回答
0

你正在玩什么是第一个问题。鸡还是鸡蛋。每次更改相机属性时,您都会有效地更改对象在 NDC 空间中的投影位置。所以即使你认为你已经接近了,你也永远不会到达那里。

换个角度看问题。将您的相机放置在某处并尝试使其尽可能规范(即,给它一个 1 的纵横比)并将您的对象放置在相机 z 轴周围。这不可能吗?

于 2013-03-06T21:33:52.223 回答