- Home /
How to check if a point is seeable by a camera
Hi. I need to know if a point can be seen by a certain camera or not. Now I'm converting the point world coordinates (as Vector3) to camera Viewport coordinates using Camera.WorldToViewportPoint, then I check if x and y values are outside the range [0..1] for both axis, but I suspect there is an easier way. Any ideas?
Answer by Mike 3 · Jul 14, 2010 at 10:35 AM
Your way is actually one of the easier ways actually - the alternative way I can think of is to check if the point lays in the camera's frustum, which is probably more code
yes, but I noticed my way does not take in account the possibility that a point is inside the camera viewport but behind another object. How do i check camera's frustrum? doesn't unity provide a primitive to check if a point is visible by camera?
No, calculating that in cpu is generally heavier than just drawing the entire scene (And not to mention that objects can be transparent, so generalization wouldnt be a good idea there). What you could do is, after doing your initial check, raycast from the camera to the point, and see if it doesn't hit anything - if it doesn't, the point is visible
Well, it seems that project requirements dictate that I have only to check the view volume, eventual occlusions are irrelevant. tanks anyway
Answer by atomicjoe · Aug 15, 2016 at 03:03 PM
use Camera.WorldToViewportPoint, check if X and Y are within 0..1 range AND check Z>0
(if Z is not greater than 0 it means the point is behind the camera.)
Answer by UnityPlum · Oct 11, 2021 at 04:45 PM
@patrik Hello! I know you already have some answers but incase someone needs some code for this I will provide it. Also, these answers do not take into account the point being behind an object. Here is some tested and ran code that does it.
public Camera camera;
public bool PointInCameraView(Vector3 point) {
Vector3 viewport = camera.WorldToViewportPoint(point);
bool inCameraFrustum = Is01(viewport.x) && Is01(viewport.y);
bool inFrontOfCamera = viewport.z > 0;
RaycastHit depthCheck;
bool objectBlockingPoint = false;
Vector3 directionBetween = point - camera.transform.position;
directionBetween = directionBetween.normalized;
float distance = Vector3.Distance(camera.transform.position, point);
if(Physics.Raycast(camera.transform.position, directionBetween, out depthCheck, distance + 0.05f)) {
if(depthCheck.point != point) {
objectBlockingPoint = true;
}
}
return inCameraFrustum && inFrontOfCamera && !objectBlockingPoint;
}
public bool Is01(float a) {
return a > 0 && a < 1;
}
Answer by Bezzy · Mar 12, 2018 at 10:39 PM
(This thing stings people every time it's used, haha. Solidarity!).
Answer by slake_it · Jul 23, 2019 at 09:12 AM
I think you need to ensure both x and y are within [0-1] ( not outside ) - if any value is outside 0-1 range then the point is not visible - also if z < 0 then I think object is behind camera
Your answer
Follow this Question
Related Questions
Camera problem 2 Answers
How to make camera position relative to a specific target. 1 Answer
Camera spaced normals for a surface shader 1 Answer
How do I move in the Third Person? 0 Answers