在我看来,ARKit 2.0提供的跟踪不是很准确。当用SCNPlane覆盖跟踪图像时,SCNPlane总是重叠或下冲。我用不同的图像和不同的源代码示例测试了图像跟踪。不推荐使用的ARToolKit可以更精确地跟踪。
iPhone 7,IOS 12.0。
源代码是典型的:
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
var session: ARSession {
return sceneView.session
}
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let configuration = ARImageTrackingConfiguration()
guard let trackingImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else {
print("Could not load images")
return
}
configuration.trackingImages = trackingImages
configuration.maximumNumberOfTrackedImages = 1
session.run(configuration, options: [])
}
public func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
let node = SCNNode()
if let imageAnchor = anchor as? ARImageAnchor {
let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
if imageAnchor.referenceImage.name == "test" {
plane.firstMaterial?.diffuse.contents = UIColor.red
plane.firstMaterial?.transparency = 0.5
}
let planeNode = SCNNode(geometry: plane)
planeNode.eulerAngles.x = -.pi / 2
node.addChildNode(planeNode)
}
return node
}
}
有任何意见或建议吗?