swift - 方舟 : place object on a plane doesn't work properly

标签 swift scenekit ios11 arkit xcode9

我正在学习 ARKit 并尝试将对象放置在检测到的平面上。但它不能正常工作,并且平面和 3D 对象之间存在空间。

这是我的平面检测代码:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        position = SCNVector3Make(anchor.transform.columns.3.x, anchor.transform.columns.3.y, anchor.transform.columns.3.z)

        guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

        let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z))

        planeNode = SCNNode(geometry: plane)
        planeNode.position = position
        planeNode.transform = SCNMatrix4MakeRotation(-Float.pi / 2.0, 1.0, 0.0, 0.0)
        node.addChildNode(planeNode)
}

然后 3d 模型得到相同的位置:

object.position = position

但是当我运行应用程序时,物体和平面之间有很大的空间。我没弄明白为什么?

最佳答案

因为anchor transform与世界坐标有关。 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 }

        let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z))

        planeNode = SCNNode(geometry: plane)
        planeNode.position = SCNVector3Zero // Position of `planeNode`, related to `node`
        planeNode.transform = SCNMatrix4MakeRotation(-Float.pi / 2.0, 1.0, 0.0, 0.0)
        node.addChildNode(planeNode)
    }

关于swift - 方舟 : place object on a plane doesn't work properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45544204/

相关文章:

ios - Swift - 从 NSDate 中提取小时数,解析字符串问题

ios - SceneKit - 使用 physicsWorld.updateCollisionPairs() 进行即时接触检测

swift3 - 如何以编程方式在 iOS 11 中打开蓝牙设置

ios - 使用未解析的标识符 'segue'

cordova - Ionic 3 - ios - 在屏幕上显示选定的图像

ios - 错误: Missing argument for parameter #1 in call when I am calling a function from different class in Swift

ios - xcode xcworkspace 在没有工具或导航器的情况下打开?

swift - 如何使用按钮在 ScrollView Controller 中强制输入 "scroll"?

ios - 如何从 Objective-C 在 Swift 中复制 vector_float3?

ios - 使用重力使 SCNNode 下降?