So I’m drawing a 2d box on screen, around 8 points in 3d space, but manually finding the min, and max value, as bounds would do it.
Vector3[] points = new Vector3[8];
for (int i = 0; i < 8; i++)
{
points _= pointsIn*.position;*_
}
float[] points2dX = new float[8]
{
Camera.main.WorldToScreenPoint(points[0]).x,
Camera.main.WorldToScreenPoint(points[1]).x,
Camera.main.WorldToScreenPoint(points[2]).x,
Camera.main.WorldToScreenPoint(points[3]).x,
Camera.main.WorldToScreenPoint(points[4]).x,
Camera.main.WorldToScreenPoint(points[5]).x,
Camera.main.WorldToScreenPoint(points[6]).x,
Camera.main.WorldToScreenPoint(points[7]).x
};
float[] points2dY = new float[8]
{
Camera.main.WorldToScreenPoint(points[0]).y,
Camera.main.WorldToScreenPoint(points[1]).y,
Camera.main.WorldToScreenPoint(points[2]).y,
Camera.main.WorldToScreenPoint(points[3]).y,
Camera.main.WorldToScreenPoint(points[4]).y,
Camera.main.WorldToScreenPoint(points[5]).y,
Camera.main.WorldToScreenPoint(points[6]).y,
Camera.main.WorldToScreenPoint(points[7]).y
};
But if one, or more, of the points go outside screen space, I still want the box to be drawn correctly. However, because of the fish eye effect (I’m guessing), when the point goes too far off screen, the values that WorldToScreenPoint returns, approach -/+ infinity, and eventually wrap around, producing wrong reuslts. So far I’ve been able to overcome the wrap around problem, by checking the WorldToScreenPoint coords, to the local 3d position of the object.
for (int i = 0; i < 8; i++)
{
if (points2dX > Screen.width && Camera.main.transform.InverseTransformPoint(points*).x < Camera.main.transform.position.x)*
{
points2dX = 0;
}
if (points2dX < 0 && Camera.main.transform.InverseTransformPoint(points*).x > Camera.main.transform.position.x)*
{
points2dX = Screen.width;
}
if (points2dY > Screen.height && Camera.main.transform.InverseTransformPoint(points*).y < Camera.main.transform.position.y)*
{
points2dY = 0;
}
if (points2dY < 0 && Camera.main.transform.InverseTransformDirection(points*).y > Camera.main.transform.position.y)*
{
points2dY = Screen.height;
}
It doesn’t always work, though. Sometimes it gets it wrong, and the points are indeed offset, when out of the screen. So my question is, is there a way to completely overcome the fish eye effect (Without using orthographic camera), or is there a better way, to get the correct screen position, of the points?