猿问

获取与相机视图对齐的 3D 空间中的点

我正在尝试使用 Frustum 来检测对象是否在屏幕的方形部分内(见下图)这是一个 UI 图像,用作目标框而不是 3D 对象。

现在我可以让 Frustum 并绘制盒子工作,但只能朝一个方向看。


我需要能够在相机转动时计算新点,但我不知道如何计算。


下面的代码有点工作,但我需要偏移位置,以便在相机移动时它们与视图对齐。


编辑:现在什么都不应该发生,但绘制框并使其工作。碰撞检测紧随其后。


// Draw aimbox

Vector3 camPosOffset = camPos + cam.transform.forward * maxDistance;


float frustumHeight = (2.0f * maxDistance * Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad));


cubeVerts[0] = new Vector3(camPos.x, camPos.y, camPos.z);

cubeVerts[1] = new Vector3(camPos.x, camPos.y, camPos.z);

cubeVerts[2] = new Vector3(camPos.x, camPos.y, camPos.z);

cubeVerts[3] = new Vector3(camPos.x, camPos.y, camPos.z);

cubeVerts[4] = new Vector3(camPosOffset.x - frustumHeight, camPosOffset.y + frustumHeight, camPosOffset.z);

cubeVerts[5] = new Vector3(camPosOffset.x + frustumHeight, camPosOffset.y + frustumHeight, camPosOffset.z);

cubeVerts[6] = new Vector3(camPosOffset.x - frustumHeight, camPosOffset.y - frustumHeight, camPosOffset.z);

cubeVerts[7] = new Vector3(camPosOffset.x + frustumHeight, camPosOffset.y - frustumHeight, camPosOffset.z);


for (int i = 0; i < edges.Length; i += 2)

{

    Debug.DrawLine(cubeVerts[edges[i]], cubeVerts[edges[i + 1]], Color.red);

}


繁花如伊
浏览 197回答 2
2回答

红糖糍粑

摆弄了将近两天后,我得到了我想要的东西。&nbsp; &nbsp; &nbsp; &nbsp; //Draw aimbox&nbsp; &nbsp; float frustumHeight = ((2.0f * maxDistance * Mathf.Tan(cam.fieldOfView/20 * 0.5f * Mathf.Deg2Rad))); //cam.fieldOfView/20, I divide by 20 because I only need a small square of the view&nbsp; &nbsp; cubeVerts[0] = cam.transform.position + cam.transform.forward * 25; //25 is the distance from the camera to the object it follows&nbsp; &nbsp; cubeVerts[1] = cam.transform.position + cam.transform.forward * 25;&nbsp; &nbsp; cubeVerts[2] = cam.transform.position + cam.transform.forward * 25;&nbsp; &nbsp; cubeVerts[3] = cam.transform.position + cam.transform.forward * 25;&nbsp; &nbsp; cubeVerts[4] = cam.transform.position + (cam.transform.right * -frustumHeight) + (cam.transform.up * frustumHeight) + (cam.transform.forward * maxDistance);&nbsp; &nbsp; cubeVerts[5] = cam.transform.position + (cam.transform.right * frustumHeight) + (cam.transform.up * frustumHeight) + (cam.transform.forward * maxDistance);&nbsp; &nbsp; cubeVerts[6] = cam.transform.position + (cam.transform.right * -frustumHeight) + (cam.transform.up * -frustumHeight) + (cam.transform.forward * maxDistance);&nbsp; &nbsp; cubeVerts[7] = cam.transform.position + (cam.transform.right * +frustumHeight) + (cam.transform.up * -frustumHeight) + (cam.transform.forward * maxDistance);&nbsp; &nbsp; for (int i = 0; i < edges.Length; i += 2)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Debug.DrawLine(cubeVerts[edges[i]], cubeVerts[edges[i + 1]], Color.red);&nbsp; &nbsp; }这里改变的视锥体视图在任何给定时间跟随相机。

DIEA

最简单的版本:使目标盒成为相机的子项。替代解决方案,也对前 4 个顶点使用 camPosOffset,如下所示:cubeVerts[0] = new Vector3(camPosOffset.x - frustumHeight, camPosOffset.y + frustumHeight, camPosOffset.z);cubeVerts[1] = new Vector3(camPosOffset.x + frustumHeight, camPosOffset.y + frustumHeight, camPosOffset.z);cubeVerts[2] = new Vector3(camPosOffset.x - frustumHeight, camPosOffset.y - frustumHeight, camPosOffset.z);cubeVerts[3] = new Vector3(camPosOffset.x + frustumHeight, camPosOffset.y - frustumHeight, camPosOffset.z);cubeVerts[4] = new Vector3(camPosOffset.x - frustumHeight, camPosOffset.y + frustumHeight, camPosOffset.z) + cam.forward * 3;cubeVerts[5] = new Vector3(camPosOffset.x + frustumHeight, camPosOffset.y + frustumHeight, camPosOffset.z) + cam.forward * 3;cubeVerts[6] = new Vector3(camPosOffset.x - frustumHeight, camPosOffset.y - frustumHeight, camPosOffset.z) + cam.forward * 3;cubeVerts[7] = new Vector3(camPosOffset.x + frustumHeight, camPosOffset.y - frustumHeight, camPosOffset.z) + cam.forward * 3;
随时随地看视频慕课网APP
我要回答