ARAnchor
是包含对真是世界位置和方向的转换,anchor 是看不见的,它仅仅是一个对象在 ARKit 的场景中,ARKit 用一个空的 SCNNode 来代表 ARAnchor, 所需要做的就是把 content 作为子节点添加到它上面去。
ARPlaneAnchor
包含了位置和方向,和另外一些平面的信息,包括,中点,大小。
观察平面
为了让 ARKit 观察平面,需要设置 ARConfiguration 对象
config.planeDetection = .horizontal
也可以设置为检测竖直的平面
config.planeDetection = .vertical
Handling new plane anchor
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor)
当有新的锚点添加的时候将会触发,当触发这个方法的时候可以添加内容到这个锚点上
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
DispatchQueue.main.async {
let newNode = self.createARPlaneNode(planeAnchor: planeAnchor, color: UIColor.yellow.withAlphaComponent(0.5))
node.addChildNode(newNode)
}
}
ARKit 可能没有检测到平面完整的信息,这个时候需要用户移动产生新的信息,需要根据新的信息作出更新
func updateARPlaneNode(planeNode: SCNNode, planeAchor: ARPlaneAnchor) {
let planeGeometry = planeNode.geometry as! SCNPlane
planeGeometry.width = CGFloat(planeAchor.extent.x)
planeGeometry.height = CGFloat(planeAchor.extent.z)
planeNode.position = SCNVector3Make(planeAchor.center.x, 0, planeAchor.center.z)
}
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
guard let planeNode = anchor as? ARPlaneAnchor else { return }
DispatchQueue.main.async {
self.updateARPlaneNode(planeNode: node.childNodes[0], planeAchor: planeNode)
}
}
image.png
更新 FoucsNode 位置
func updateFoucsNode() {
let result = self.sceneView.hitTest(self.focusPoint, types: [.existingPlaneUsingExtent])
if result.count == 1 {
if let match = result.first {
let t = match.worldTransform
self.focusNode.position = SCNVector3.init(t.columns.3.x, t.columns.3.y, t.columns.3.z)
self.gameState = .swipeToPlay
}
} else {
self.gameState = .pointToSurface
}
}
func renderer(_ renderer: SCNSceneRenderer,
updateAtTime time: TimeInterval) {
DispatchQueue.main.async {
self.updateStatus()
self.updateFoucsNode()
}
}
网友评论