Search Unity

Resolved Check if plane faces the camera

Discussion in 'Scripting' started by Nimerion, May 17, 2020.

  1. Nimerion

    Nimerion

    Joined:
    Apr 20, 2014
    Posts:
    42
    Hello all,
    I've been working on a planet generator for awhile now.

    I am facing a problem where I've got 6 sides of a cube-sphere. (Or a cube, but you get where I'm going)

    I need to know which side is my camera looking at, so I can subdivide it.

    I am currently doing the following:

    1. I take each face's border points
    2. Work out a pivot point (center point)
    3. Based of 1 and 2 I perform the following checks:

    Code (CSharp):
    1.  float cTargetDir = Vector3.Dot(centroid.normalized + centroid, Camera.main.transform.position - centroid);
    2.  
    3.         if (cTargetDir < 0)
    4.         {
    5.             return false;
    6.         }
    7.  
    8.         foreach (float camDist in cameraDistances)
    9.         {
    10.             if (camDist < faceDistance1 ||
    11.                 camDist < faceDistance2 ||
    12.                 camDist < faceDistance3 ||
    13.                 camDist < faceDistance4)
    14.             {
    15.                 return true;
    16.             }
    17.         }
    18.  
    19.         if (GeometryUtility.TestPlanesAABB(planes, rendererBounds))
    20.         {
    21.             return true;
    22.         }
    centroid is just the center point of the plane of interest.

    What the code above does is it checks if the center point of the plane faces the camera. (Kind of works)
    But I presume, it's a very hacky and not very correct way to do that.

    And then I check if That first "IF passes", then I can check if the face is visible to my camera by 2 rules.

    If #2 represents if any point is closer o the center of the mesh than the mesh's corner points.
    If #3 represents if any the renderer of this mesh is visible by the camera.

    I am doing this so I can avoid looping through each point.


    Any ideas about what am I missing, and if I'm even doing it right would be of big help.
    If you need more information or code let me know. As I'm not the first to do a planet generator, nor it's anything unique.

    Ps. Sorry if there's already a topic like that, I haven't found it, and I've searched for awhile.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You already have the 6 centroids for the cube? Why not just check which of the 6 are closest to the camera? I have a dice rolling mechanic in a project I'm working on and this is how I check which side of the die I rolled: which face centroid is highest.
     
    khrysller and Nimerion like this.
  3. Nimerion

    Nimerion

    Joined:
    Apr 20, 2014
    Posts:
    42
    OH! This sounds like a spot on idea PraetorBlue!
    Thanks for the swift response as well, I will post how that went in the morning. (2am in the UK)
     
  4. Nimerion

    Nimerion

    Joined:
    Apr 20, 2014
    Posts:
    42
    That appears to be working just fine. Thank you PraetorBlue!
     
    PraetorBlue likes this.