所以我有 4 個世界點作為Vector3物件。
我還有一個固定旋轉的相機。
我想保持我的相機的旋轉,并保持它的中心0,0,0并且可以保證我的點將允許這樣做,因為它們也是圍繞原點生成的。我只需要將相機向后移動,直到它們都在視野中。
我是 Unity 的新手,一般用 3D 思考,所以我不太確定我在這里尋找什么,但我覺得這應該很容易?
我想我可以將相機向后移動,直到得到WorldToViewportPoint介于兩者之間的值,0 - 1但我想應用現有的平滑移動函式,該函式需要一個起點和終點,所以我想要一種在移動相機之前計算它的方法.
任何指標將不勝感激。
謝謝
uj5u.com熱心網友回復:
目前無法對此進行測驗,但是使用三角學和一些相機功能,您可以找到沿著相機向前查看每個點所需的原點距離,然后取最小值。然后,沿著相機的前方走那個距離。
這應該處理相機的任何方向/位置!
更多細節在評論中:
using System.Linq; // convenience
float GetClosestCameraDist(Camera cam, Vector3 point)
{
// cam vertical/horizontal fov in radians
float vFov = cam.fieldOfView * Mathf.Deg2Rad;
float hFov = Camera.VerticalToHorizontalFieldOfView(cam.fieldOfView,
cam.aspect) * Mathf.Deg2Rad;
// how far point should be from cam along cam forward to ensure
// point is within fov along camera axes
float d = Mathf.Max(
Mathf.Abs(point.y) / Mathf.Tan(vFov/2f),
Mathf.Abs(point.x) / Mathf.Tan(hFov/2f) );
// return how far cam should be from origin 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;
// 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(cam,
tCam.InverseTransformPoint(p) )).Min();
// go that distance along cam forward from current cam position
// to find closest-to-points valid position.
return c * tCam.forward tCam.position;
}
當然不是最有效的演算法,但只有 4 分它不應該是可怕的。通過僅沿相機的上/右軸執行距相機前視線最遠的 2 個點,速度可能會更快。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/360539.html
