假设矩形在水平面上,您可以对场景的所有 4 个角执行命中测试,并使用其中 3 个角计算矩形的宽度、高度、中心和方向。
我在 GitHub 上有一个演示应用程序可以做到这一点:
https ://github.com/mludowise/ARKitRectangleDetection
矩形角的坐标VNRectangleObservation
将与图像的大小相关,并且根据手机的旋转在不同的坐标中。您需要将它们乘以视图大小并根据手机的旋转来反转它们:
func convertFromCamera(_ point: CGPoint, view sceneView: ARSCNView) -> CGPoint {
let orientation = UIApplication.shared.statusBarOrientation
switch orientation {
case .portrait, .unknown:
return CGPoint(x: point.y * sceneView.frame.width, y: point.x * sceneView.frame.height)
case .landscapeLeft:
return CGPoint(x: (1 - point.x) * sceneView.frame.width, y: point.y * sceneView.frame.height)
case .landscapeRight:
return CGPoint(x: point.x * sceneView.frame.width, y: (1 - point.y) * sceneView.frame.height)
case .portraitUpsideDown:
return CGPoint(x: (1 - point.y) * sceneView.frame.width, y: (1 - point.x) * sceneView.frame.height)
}
}
然后您可以对所有 4 个角进行命中测试。在执行命中测试时使用类型很重要,.existingPlaneUsingExtent
这样 ARKit 会返回水平面的命中。
let tl = sceneView.hitTest(convertFromCamera(rectangle.topLeft, view: sceneView), types: .existingPlaneUsingExtent)
let tr = sceneView.hitTest(convertFromCamera(rectangle.topRight, view: sceneView), types: .existingPlaneUsingExtent)
let bl = sceneView.hitTest(convertFromCamera(rectangle.bottomLeft, view: sceneView), types: .existingPlaneUsingExtent)
let br = sceneView.hitTest(convertFromCamera(rectangle.bottomRight, view: sceneView), types: .existingPlaneUsingExtent)
然后就有点复杂了...
因为每个命中测试可能会返回 0 到 n 个结果,所以您需要过滤掉包含在不同平面上的任何命中测试。您可以通过比较每个锚点来做到这一点ARHitTestResult
:
hit1.anchor == hit2.anchor
此外,您只需要 4 个角中的 3 个来识别矩形的尺寸、位置和方向,因此如果一个角不返回任何命中测试结果也没关系。看看这里我是怎么做到的。
您可以根据左右角(顶部或底部)之间的距离计算矩形的宽度。同样,您可以根据顶角和底角之间的距离(左或右)计算矩形的高度。
func distance(_ a: SCNVector3, from b: SCNVector3) -> CGFloat {
let deltaX = a.x - b.x
let deltaY = a.y - b.y
let deltaZ = a.z - b.z
return CGFloat(sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ))
}
let width = distance(right, from: left)
let height = distance(top, from: bottom)
您可以通过从矩形的对角(topLeft & bottomRight 或 topRight & bottomLeft)获取中点来计算其位置:
let midX = (c1.x + c2.x) / 2
let midY = (c1.y + c2.y) / 2
let midZ = (c1.z + c2.z) / 2
let center = SCNVector3Make(midX, midY, midZ)
您还可以从左右角(顶部或底部)计算矩形的方向(沿 y 轴旋转):
let distX = right.x - left.x
let distZ = right.z - left.z
let orientation = -atan(distZ / distX)
然后把它们放在一起,在 AR 中显示一些东西,覆盖在矩形上。这是通过子类化显示虚拟矩形的示例SCNNode
:
class RectangleNode: SCNNode {
init(center: SCNVector3, width: CGFloat, height: CGFloat, orientation: Float) {
super.init()
// Create the 3D plane geometry with the dimensions calculated from corners
let planeGeometry = SCNPlane(width: width, height: height)
let rectNode = SCNNode(geometry: planeGeometry)
// Planes in SceneKit are vertical by default so we need to rotate
// 90 degrees to match planes in ARKit
var transform = SCNMatrix4MakeRotation(-Float.pi / 2.0, 1.0, 0.0, 0.0)
// Set rotation to the corner of the rectangle
transform = SCNMatrix4Rotate(transform, orientation, 0, 1, 0)
rectNode.transform = transform
// We add the new node to ourself since we inherited from SCNNode
self.addChildNode(rectNode)
// Set position to the center of rectangle
self.position = center
}
}