我正在开发一个ARKit ImageTracking应用,出于我们的AR体验,我们希望Tracking image应该完全可见。如果部分可见,我需要显示一个UI消息“请确保Marker完全可见”。 所以我添加了一个平面作为SCNNode,它等于标记的大小。如下 并获得了边界框或平面的四个角,但是如何检测此平面是完全可见的,还是平面的所有四个角都在相机框架中?
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
//1. If Out Target Image Has Been Detected Than Get The Corresponding Anchor
guard let currentImageAnchor = anchor as? ARImageAnchor else { return }
let x = currentImageAnchor.transform
print(x.columns.3.x, x.columns.3.y , x.columns.3.z)
//2. Get The Targets Name
let name = currentImageAnchor.referenceImage.name!
//3. Get The Targets Width & Height In Meters
let width = currentImageAnchor.referenceImage.physicalSize.width
let height = currentImageAnchor.referenceImage.physicalSize.height
//4. Create A Plane Geometry To Cover The ARImageAnchor
let planeNode = SCNNode()
planeNode.name = "markerSizePlane"
let planeGeometry = SCNPlane(width: width, height: height)
planeGeometry.firstMaterial?.diffuse.contents = UIColor.blue
planeNode.opacity = 0.25
planeNode.geometry = planeGeometry
//5. Rotate The PlaneNode To Horizontal
planeNode.eulerAngles.x = -.pi/2
//The Node Is Centered In The Anchor (0,0,0)
node.addChildNode(planeNode)
}
/* Getting corners of plane */
func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) {
var bottomLeft: SCNVector3!
var topRight: SCNVector3!
var topLeft: SCNVector3!
var bottomRight: SCNVector3!
//1. Get The Current Point Of View
guard let pointOfView = sceneView.pointOfView else { return }
scene.rootNode.enumerateChildNodes(){
node, stop in
if node.name == "markerSizePlane"{
//print("markerSizePlane position \(node.position)")
let (min, max) = node.boundingBox
bottomLeft = SCNVector3(min.x, min.y, 0)
topRight = SCNVector3(max.x, max.y, 0)
topLeft = SCNVector3(min.x, max.y, 0)
bottomRight = SCNVector3(max.x, min.y, 0)
}
}
}