当应用在ARKit中检测到至少一架飞机时,如何收到通知?
我目前正在做类似这样的事情,但它没有解雇/它没有关系:
extension MyViewController: ARSCNViewDelegate, ARSessionDelegate {
internal func setupAR() {
let scene = SCNScene()
sceneView.scene = scene
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal]
sceneView.session.delegate = self
sceneView.session.run(configuration)
}
public func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
if !anchors.isEmpty {
print("ANCHORS NOT EMPTY")
planesDetectedState()
} else {
print("Anchors is empty")
}
}
}
答案 0 :(得分:3)
请先添加首先加入更新。您需要使用以下功能:
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
guard let frame = session.currentFrame else { return }
print("Just added an anchor and felt so good")
}
另外,除非您向锚点添加节点,否则您将无法看到任何平面,您可以通过以下方式执行此操作:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
// Add a planeNode and make it a child to the node automatically added.
}
我还建议,下载初学者代码,这些代码很容易理解为您正在考虑的事情。
https://developer.apple.com/documentation/arkit/building_your_first_ar_experience
答案 1 :(得分:2)
如果您想检测ARAnchors
,有几种方法可以解决这个问题。
1st:您可以使用ARSessionDelegate
回拨:
public func session(_ session: ARSession, didAdd anchors: [ARAnchor]) { }
其中:
告诉代理人已经添加了一个或多个锚点 会话。
使用此回调的示例如此:
extension UIViewController: ARSessionDelegate{
public func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
//1. If We Have At Least One ARAnchor Detected The Log The Information
if !anchors.isEmpty {
anchors.forEach { (anchor) in
print("""
The Type Of Anchor = \(anchor.classForCoder)
The Anchor Identifier = \(anchor.identifier)
The Anchor Translation = X: \(anchor.transform.columns.3.x), Y: \(anchor.transform.columns.3.y), Z: \(anchor.transform.columns.3.z)
""")
}
}
}
}
第二名:您可以使用ARSCNViewDelegate
回调:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor)
其中:
告诉委托一个对应于新AR的SceneKit节点 锚已被添加到场景中。
使用此回调的示例如此:
extension ViewController: ARSCNViewDelegate{
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
//1. Check An ARPlane Anchor Has Been Detected
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
//2. Get The Width & Height Of The Plane
let width = CGFloat(planeAnchor.extent.x)
let height = CGFloat(planeAnchor.extent.z)
//3. Create An SCNPlane So We Can Visualize The Plane Detected
let plane = SCNPlane(width: width, height: height)
//4. Set It's Colour
plane.materials.first?.diffuse.contents = UIColor.cyan
//5. Create An SCNNode To Hold Our Plane
let planeNode = SCNNode(geometry: plane)
//6. Position & Rotate It
let x = CGFloat(planeAnchor.center.x)
let y = CGFloat(planeAnchor.center.y)
let z = CGFloat(planeAnchor.center.z)
planeNode.position = SCNVector3(x,y,z)
planeNode.eulerAngles.x = -.pi / 2
//7. Add It To The Node For Anchor
node.addChildNode(planeNode)
}
}