所以我有 4 个世界点作为 Vector3
对象。
我还有一个固定旋转的透视相机。
我想保持我的相机的旋转,并保持它以 0,0,0
为中心,并且可以保证我的点将允许这样做,因为它们也是围绕原点生成的。我只需要将相机移回,直到它们都在视野中。
我是 Unity 的新手并且通常在 3D 中思考,所以我不太确定我在这里寻找什么,但我觉得这应该很简单?
我想我可以将相机向后移动,直到我得到一个 0 - 1
之间的 WorldToViewportPoint
但我想应用我现有的平滑移动功能需要一个起点和终点,所以我想要一种在移动相机之前计算它的方法。
如有任何指点,我们将不胜感激。
谢谢
最佳答案
通过使用三角函数和一些相机函数来找到相机的水平和垂直 fov,您可以找到相机向前移动以查看每个点所需的距离,然后取最小值。然后,将相机向前移动该距离。
这将处理相机的任何方向/位置!
更多细节在评论中:
using System.Linq; // convenience
float GetClosestCameraDist(Vector3 point, float vCot, float hCot)
{
// point to view (camera's local space)
// v/hCot = cotan of half fovs
// how far point should be from cam along cam forward to ensure
// point is within fov along camera x/y axes
float d = Mathf.Max(Mathf.Abs(point.y) * vCot,
Mathf.Abs(point.x) * hCot);
// return how far cam needs to move from its current position
// along cam forward
// should be negative if it goes cam-> ... origin
// positive if it goes origin ... cam->
return point.z - d;
}
Vector3 GetClosestCameraPos(Camera cam, List<Vector3> points)
{
Transform tCam = cam.transform;
// cam vertical/horizontal fov in radians
float vFov = cam.fieldOfView * Mathf.Deg2Rad;
float hFov = Camera.VerticalToHorizontalFieldOfView(cam.fieldOfView,
cam.aspect) * Mathf.Deg2Rad;
float vCot = 1f / Mathf.Tan(0.5f*vFov);
float hCot = 1f / Mathf.Tan(0.5f*hFov);
// calculate lowest needed distance from current position to valid position
// along cam forward
// lowest because the larger this is, the more forward camera is
float c = points.Select(p => GetClosestCameraDist(tCam.InverseTransformPoint(p),
vCot, hCot)).Min();
// go that distance along cam forward from current cam position
// to find closest-to-points valid position.
return c * tCam.forward + tCam.position;
}
此外,如果你想让相机稍微靠后一点,最简单的方法就是将hFov
和vFov
的值调小一些,让数学表现得像相机的视野更小。类似下面的内容:
float vFov = // ...
float hFov = // ...
// Will bring the camera back further such that each point is within
// the middle 90% horizontal and 90% vertical rectangle.
hFov *= 0.9f;
vFov *= 0.9f;
// rest of GetClosestCameraPos
关于c# - 给定 4 个世界点,我如何计算相机位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70027402/