在 ARKit 中检测平面
Detect planes in ARKit
当应用在 ARKit 中检测到至少一架飞机时,我如何收到通知?
我目前正在做类似的事情,但这不是firing/it不相关的:
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")
}
}
}
请先添加 锚以更新 那些。您需要使用以下功能:
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
如果你想检测ARAnchors
,有几种方法可以做到。
1st: 您可以使用 ARSessionDelegate
回调:
public func session(_ session: ARSession, didAdd anchors: [ARAnchor]) { }
其中:
Tells the delegate that one or more anchors have been added to the
session.
使用此回调的示例如下所示:
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)
其中:
Tells the delegate that a SceneKit node corresponding to a new AR
anchor has been added to the scene.
使用此回调的示例如下所示:
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)
}
}
当应用在 ARKit 中检测到至少一架飞机时,我如何收到通知?
我目前正在做类似的事情,但这不是firing/it不相关的:
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")
}
}
}
请先添加 锚以更新 那些。您需要使用以下功能:
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
如果你想检测ARAnchors
,有几种方法可以做到。
1st: 您可以使用 ARSessionDelegate
回调:
public func session(_ session: ARSession, didAdd anchors: [ARAnchor]) { }
其中:
Tells the delegate that one or more anchors have been added to the session.
使用此回调的示例如下所示:
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)
其中:
Tells the delegate that a SceneKit node corresponding to a new AR anchor has been added to the scene.
使用此回调的示例如下所示:
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)
}
}