我试图将ARImageAnchor放置在与ARPlaneAnchor相同的位置,但它不起作用。下面是我的renderer函数的开始,我认为问题就在这里:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let imageAnchor = anchor as? ARImageAnchor else { return }
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
...
}当我删除与planeAnchor相关的所有内容时,它都能正常工作,但一旦我尝试将它们放在相同的位置,它仍然会编译,但它不会检测到参考图像。有人以前见过这样的问题吗?谢谢!
UPDATE:以sceneView作为我的ARSCNView,我使用以下代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("got here")
guard let touchLocation = touches.first?.location(in: sceneView),
let hitTest = sceneView?.hitTest(touchLocation, options: nil),
//let detectedPlane = planes[planeAnchor],
let nodeTapped = hitTest.first?.node,
let nodeName = nodeTapped.name else { return }
print("got here 2")
print(nodeName)
}但是“到达这里2”从来没有打印出来,当我点击飞机时什么也不会发生。我该试试什么?
发布于 2018-02-26 02:07:02
仔细看了看,我不知道这是可能的。
如果您将ARWorldTrackingConfiguration设置为检测平面,例如:
configuration.planeDetection = .horizontal然后在委托方法中添加此调试日志:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
print(anchor.classForCoder)
}您将看到,当您检测到目标图像时,它将记录ARImageAnchor,而如果它检测到一个水平平面,它将记录:ARPlaneAnchor。
这就是为什么你的警卫声明没有被执行的原因:
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }只是因为不能同时有两个锚..。
有一个ARPlaneAnchor或一个ARImageAnchor。
基于您的最后一个响应的更新,它将允许您获得以下属性:
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 }
//2. Get The Targets Name
let name = currentImageAnchor.referenceImage.name!
//3. Get The Targets Width & Height
let width = currentImageAnchor.referenceImage.physicalSize.width
let height = currentImageAnchor.referenceImage.physicalSize.height
//4. Log The Reference Images Information
print("""
Image Name = \(name)
Image Width = \(width)
Image Height = \(height)
""")
}然而,我仍然不明白为什么还需要一个PlaneAnchor,您可以使用它来显示模型等等:)
如果您想在那个锚上添加一些内容,请尝试如下:
/// Generates An SCNPlane On The Detected Image At The Size Of The Target
///
/// - Parameters:
/// - node: SCNNode
/// - width: CGFloat
/// - height: CGFloat
func generateModel(_ node: SCNNode, width: CGFloat, height: CGFloat){
let nodeToAdd = SCNNode()
let planeGeometry = SCNPlane(width: width, height: height)
planeGeometry.firstMaterial?.diffuse.contents = UIColor.red
nodeToAdd.geometry = planeGeometry
nodeToAdd.transform = SCNMatrix4MakeRotation(-Float.pi / 2, 1, 0, 0)
node.addChildNode(nodeToAdd)
}并在委托方法中调用它:
generateModel(node, width: width, height: height)我认为这就是你想要达到的目标。
更新:要在添加的节点上获取hitTest,只需使用touchesBegan。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first?.location(in: self.augmentedRealityView),
let hitTest = self.augmentedRealityView?.hitTest(touch, options: nil),
let nodeTapped = hitTest.first?.node,
let nodeName = nodeTapped.name else { return }
print(nodeName)
}Update:如果放置在ImageAnchor上的对象看起来不稳定,可能是您在ARAssets包中设置了referenceImage大小错误:

https://stackoverflow.com/questions/48979812
复制相似问题