Search Unity

arfoundation vertical plane recognition, position, rotation on plane normal

Discussion in 'AR' started by Blarp, Apr 20, 2020.

  1. Blarp

    Blarp

    Joined:
    May 13, 2014
    Posts:
    270
    I was looking around for something like this and found very little concise information in one spot. So putting this here so it can help anyone in the future. It's basically for hanging augmented reality paintings on the wall, or things of that nature.

    I'm basically doing vertical plane recognition and placing a quad against it, and rotating the quad to be flush with the AR plane normal.

    Code (csharp):
    1.  
    2.     void Update()
    3.     {
    4.         if(moveBool)
    5.         {
    6.             //center of the screen
    7.             screenCenter = new Vector2(Screen.width / 2, Screen.height / 2);
    8.             //raycast screen center
    9.             hitsRaycastManager = new List<ARRaycastHit>();
    10.             arRaycastManager.Raycast(screenCenter, hitsRaycastManager, TrackableType.PlaneWithinInfinity);
    11.             //get best raycast position
    12.             placementPose = hitsRaycastManager[0].pose;
    13.             //place model at the raycast position
    14.             gameObj.transform.position = placementPose.position;
    15.             //grab the AR plane ID of the raycasted AR plane
    16.             placedPlaneId = hitsRaycastManager[0].trackableId;
    17.             ARPlane arPlane = aRPlaneManager.GetPlane(placedPlaneId);
    18.             //get ar plane's normal position
    19.             Vector3 planeNormal = arPlane.normal;
    20.             //rotation variable for looking at plane normal, upright
    21.             Quaternion orientToPlane = Quaternion.LookRotation(planeNormal, Vector3.up);
    22.             //assign rotation to painting.
    23.             gameObj.transform.rotation = orientToPlane;
    24.             //flip the rotation or else it's backwards
    25.             gameObj.transform.rotation *= Quaternion.Euler(0,180f,0);
    26.         }
    27.     }
    28.  
    29.  
     
    Doraemon231 and linojon like this.
  2. linojon

    linojon

    Joined:
    Aug 25, 2014
    Posts:
    118
    You can use the hit.pose.up vector for the plane normal. On a horizontal plane, it makes sense for "up" to be the plane normal. On a vertical plane "up" is perpendicular to the wall

    Vector3 normal = -hit.pose.up;
    Quaternion rotation = Quaternion.LookRotation(normal, Vector3.up);
    Vector3 position = hit.pose.position;