Is there a simple way to determine if a point is within the cameras view?
I ask because WorldPointToScreen has problems where things behind me render to the screen like they where in the front.
Is there a simple way to determine if a point is within the cameras view?
I ask because WorldPointToScreen has problems where things behind me render to the screen like they where in the front.
probably use line of sight, but i dont know how yet.
If the object has a renderer, you can access these functions:
OnBecameVisible: OnBecameVisible is called when the object became visible by any camera.
OnBecameInvisible: OnBecameInvisible is called when the object is no longer visible by any camera.
Oh, this variable too:
isVisible: Is this renderer visible in any camera? (Read Only)
You should question why you’re having this problem. There’s no reason why it shouldn’t work, though WorldToViewportPoint is easier. If you didn’t care about the near and far clip planes, and only wanted to know if the infinite pyramid in front of the camera’s position contained a point, this would work:
bool InfiniteCameraCanSeePoint (Camera camera, Vector3 point) {
Vector3 viewportPoint = camera.WorldToViewportPoint(point);
return (viewportPoint.z > 0 (new Rect(0, 0, 1, 1)).Contains(viewportPoint));
}
Otherwise, you could use GeometryUtility, which would work nicely for thick shapes, not just points.
Do keep in mind that none of these things take occlusion into account. RayCasting from the camera will probably work fine though.
IsVisible works, but side note, it counts the scene camera in that which threw me off for alittle while
Thanks Jessy that works like a charm. I figured ViewportPoint had what I needed just didnt know exactly what to do to solve it in that direction
Based on some of the code I’ve come across around the net I’ve come out with these functions for kind of framing on an object with a camera
For this thread, maybe only the IsPointVisible function is needed but the other is handy if you want to just frame an object by bounding box
private bool IsPointVisible(Bounds bounds, Camera cam)
{
var points = new List<Vector3>();
var boundPoint1 = bounds.min;
var boundPoint2 = bounds.max;
points.Add(cam.WorldToViewportPoint(boundPoint1));
points.Add(cam.WorldToViewportPoint(boundPoint2));
points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint1.x, boundPoint1.y, boundPoint2.z)));
points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint1.x, boundPoint2.y, boundPoint1.z)));
points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint2.x, boundPoint1.y, boundPoint1.z)));
points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint1.x, boundPoint2.y, boundPoint2.z)));
points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint2.x, boundPoint1.y, boundPoint2.z)));
points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint2.x, boundPoint2.y, boundPoint1.z)));
//Assume that the point IS visible till it's not
var pointVisible = true;
foreach (var point in points)
{
if (!new Rect(0, 0, 1, 1).Contains(point))
{
//Point isn't visible so we'll want to step back, if one point isn't visible then we don't need to continue
pointVisible = false;
break;
}
}
return pointVisible;
}
private void BringIntoView(Bounds bounds, Camera cam, float finalPush)
{
//finalPush => last step back after doing the stepping camera front or backwards
//If the points are visible already we want to step forward till it's not then back slightly so we have a good framing
var stepDirection = cam.transform.forward;
if (IsPointVisible(bounds, cam))
{
while (IsPointVisible(bounds, cam))
{
cam.transform.position = cam.transform.position + (stepDirection * (Time.deltaTime * 2));
}
}
if (!IsPointVisible(bounds, cam))
{
stepDirection = -cam.transform.forward;
while (!IsPointVisible(bounds, cam))
{
cam.transform.position = cam.transform.position + (stepDirection * (Time.deltaTime * 2));
}
}
//Do once more for padding
cam.transform.position = cam.transform.position + (-cam.transform.forward * finalPush);
}
Hopefully someone will enjoy this