Search Unity

Point in camera view

Discussion in 'Scripting' started by PaulAsh, Jan 2, 2011.

  1. PaulAsh

    PaulAsh

    Joined:
    Nov 29, 2010
    Posts:
    108
    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.
     
  2. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    probably use line of sight, but i dont know how yet.
     
  3. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
  4. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Oh, this variable too:

    isVisible: Is this renderer visible in any camera? (Read Only)
     
  5. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    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:

    Code (csharp):
    1. bool InfiniteCameraCanSeePoint (Camera camera, Vector3 point) {
    2.     Vector3 viewportPoint = camera.WorldToViewportPoint(point);
    3.     return (viewportPoint.z > 0  (new Rect(0, 0, 1, 1)).Contains(viewportPoint));
    4. }
    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.
     
    Last edited: Jan 2, 2011
    atomicjoe likes this.
  6. PaulAsh

    PaulAsh

    Joined:
    Nov 29, 2010
    Posts:
    108
    IsVisible works, but side note, it counts the scene camera in that which threw me off for alittle while
     
  7. PaulAsh

    PaulAsh

    Joined:
    Nov 29, 2010
    Posts:
    108
    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
     
  8. TEvashkevich-ES

    TEvashkevich-ES

    Joined:
    Dec 6, 2017
    Posts:
    3
    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 :)
    Code (CSharp):
    1. private bool IsPointVisible(Bounds bounds, Camera cam)
    2. {
    3.     var points = new List<Vector3>();
    4.     var boundPoint1 = bounds.min;
    5.     var boundPoint2 = bounds.max;
    6.     points.Add(cam.WorldToViewportPoint(boundPoint1));
    7.     points.Add(cam.WorldToViewportPoint(boundPoint2));
    8.     points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint1.x, boundPoint1.y, boundPoint2.z)));
    9.     points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint1.x, boundPoint2.y, boundPoint1.z)));
    10.     points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint2.x, boundPoint1.y, boundPoint1.z)));
    11.     points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint1.x, boundPoint2.y, boundPoint2.z)));
    12.     points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint2.x, boundPoint1.y, boundPoint2.z)));
    13.     points.Add(cam.WorldToViewportPoint(new Vector3(boundPoint2.x, boundPoint2.y, boundPoint1.z)));
    14.  
    15.     //Assume that the point IS visible till it's not
    16.     var pointVisible = true;
    17.     foreach (var point in points)
    18.     {
    19.         if (!new Rect(0, 0, 1, 1).Contains(point))
    20.         {
    21.             //Point isn't visible so we'll want to step back, if one point isn't visible then we don't need to continue
    22.             pointVisible = false;
    23.             break;
    24.         }
    25.     }
    26.     return pointVisible;
    27. }
    28.  
    29. private void BringIntoView(Bounds bounds, Camera cam, float finalPush)
    30. {
    31.     //finalPush => last step back after doing the stepping camera front or backwards
    32.     //If the points are visible already we want to step forward till it's not then back slightly so we have a good framing
    33.     var stepDirection = cam.transform.forward;
    34.     if (IsPointVisible(bounds, cam))
    35.     {
    36.         while (IsPointVisible(bounds, cam))
    37.         {
    38.             cam.transform.position = cam.transform.position + (stepDirection * (Time.deltaTime * 2));
    39.         }
    40.     }
    41.  
    42.     if (!IsPointVisible(bounds, cam))
    43.     {
    44.         stepDirection = -cam.transform.forward;
    45.         while (!IsPointVisible(bounds, cam))
    46.         {
    47.             cam.transform.position = cam.transform.position + (stepDirection * (Time.deltaTime * 2));
    48.         }
    49.     }
    50.  
    51.     //Do once more for padding
    52.     cam.transform.position = cam.transform.position + (-cam.transform.forward * finalPush);
    53. }
    Hopefully someone will enjoy this