Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020.2 has been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[Solved]Corner-Bounds camera-frustum

Discussion in 'Editor & General Support' started by FrankvHoof, May 1, 2019.

  1. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    115
    I'm trying to test one of my systems, which calculates whether an object-(/entity-)position is within the bounds of my frustum.
    In order to do this, I'm creating 8 entities (for the 8 positions on the square), and checking these.
    The positions on the edges are fine, but the ones in the corners are somehow outside of the frustum for the camera, and I'm not sure why..

    Relevant Code (Create9PatchOnFOV is called with (fovH-5)/2 and (fovV-5)/2):

    Code (CSharp):
    1.  
    2. private float fovV { get { return testCamera.fieldOfView; } }
    3. private float fovH { get { return math.degrees(2 * math.atan(math.tan(math.radians(fovV) / 2f) * testCamera.aspect)); } }
    4.  
    5. private List<Entity> Create9PatchOnFOV(float dist, float fovHori, float fovVert)
    6. {
    7.     List<Entity> result = new List<Entity>(9);
    8.     for (int i = -1; i <= 1; i++)
    9.         for (int j = -1; j <= 1; j++)
    10.         {
    11.              if (i == 0 && j == 0)
    12.                 continue; // Skip Middle (not part of FOV)
    13.             Vector3 a = PolarToCartesian(dist, i * fovVert, j * fovHori);
    14.             Debug.Log(a.ToLogString());
    15.             result.Add(CreateEntityAtPosition(a));
    16.         }                    
    17.     return result;
    18. }
    19.  
    20. private Vector3 PolarToCartesian(float dist, float angX, float angY)
    21. {
    22.     return Quaternion.Euler(angX, angY, 0) * new Vector3(0, 0, dist);
    23. }
    24.  
    The calculated positions (for a distance of 10) are:
    (image includes middle, which was stripped later)

    Which I believe to be the positions on a dome in front of the camera (and thus should all be in view), but the ones in the corners (index 0, 2, 6 & 8) are apparently not in view of the camera..
    How do I rectify these?
     
    Last edited: May 1, 2019
  2. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    115
    Found my issue.
    I need to calculate depth just once.
     
unityunity