- Home /
WorldToScreenPoint, wrapping around when outside screen.
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[i] = pointsIn[i].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[i] > Screen.width && Camera.main.transform.InverseTransformPoint(points[i]).x < Camera.main.transform.position.x)
{
points2dX[i] = 0;
}
if (points2dX[i] < 0 && Camera.main.transform.InverseTransformPoint(points[i]).x > Camera.main.transform.position.x)
{
points2dX[i] = Screen.width;
}
if (points2dY[i] > Screen.height && Camera.main.transform.InverseTransformPoint(points[i]).y < Camera.main.transform.position.y)
{
points2dY[i] = 0;
}
if (points2dY[i] < 0 && Camera.main.transform.InverseTransformDirection(points[i]).y > Camera.main.transform.position.y)
{
points2dY[i] = 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?
Answer by robertbu · Aug 13, 2014 at 04:24 PM
Visualize an infinite plane running in front of you. Image you are casting a ray against that plane as you rotate right. At higher angles from forward, small increases in angle make huge changes the x coordinate. As you approach 90 degrees from forward, you approach infinity. And for Unity we don't need infinity to have a problem, just something that overflows a float when doing the WorldToScreen() calculation. I believe this is the issue you are facing.
One thing you can try to mitigate the problem (assuming your game mechanic will allow it) is to narrow the field of view of your camera and move the camera back from your points to compensate (keep their relative position and distance as seen by the camera). The resulting geometry means that a points will need to be further out of view before you suffer a problem. The greater you narrow the field of view, the further the point will need to be out of view before you suffer a problem.
A real solution to the problem would be to clip the points passed to (or near) the bounding rect of the screen. The difficulty of this task depends on your geometry. For an axes aligned camera looking at points on a plane parallel to the camera plane, it is easy. For an arbitrary camera rotation and/or points in arbitrary 3D space, it is much more difficult task. One reference: