This project demonstrates how to use Apple's Vision library to identify rectangles and model them in 3D using ARKit.
If you're not already familiar with the ARKit and Vision libraries, it's worth the time to read Apple's overviews on how to use them. There are also some great tutorials out there to get up to speed. I went through the following in a couple hours to get acquainted with them myself:
The findRectangle(locationInScene location: CGPoint, frame currentFrame: ARFrame)
method inside of ViewController.swift
uses the location the user touched on the screen and the current frame from ARKit to find any rectangles in the current frame.
The custom PlaneRectangle
class converts the corners returned by the VNRectangleObservation
into 2D coordinates inside of sceneView
and performes a hitTest
on each of the corners to find where they intersect with a plane inside of the scene and calculates the rectangles dimensions, position, and orientation on the plane.
The RectangleNode
class creates a SCNPlane
from dimensions calculated in PlaneRectangle
which is then added to the scene.
It's worth noting that the rectangle's position and dimensions can only be calculated if ARKit has found a horizontal plane underneath the rectangle and if at least 3 corners of the rectangle are on that plane.