ios - ARKit:将 3D 对象放置在图像之上

标签 ios swift augmented-reality arkit

我正在尝试在引用图像上正确添加 3D 对象。为了在现实世界中的图像上添加 3D 对象,我使用了 imageAnchor.transform 属性,如下所示。

    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

    guard let imageAnchor = anchor as? ARImageAnchor else { return }

    let referenceImage = imageAnchor.referenceImage

    updateQueue.async {  
        // Add a virtual cup at the position of the found image
        self.virtualObject.addVirtualObjectWith(sceneName: "cup.dae",
                                                childNodeName: nil,
                                                position: SCNVector3(x: imageAnchor.transform.columns.3.x,
                                                                     y: imageAnchor.transform.columns.3.y,
                                                                     z: imageAnchor.transform.columns.3.z),
                                                recursively: true,
                                                imageAnchor: imageAnchor)

    }

}

The cup when just added to the scene

问题是当我移动设备方向时,杯子不会很好地停留在图像的中间。即使我删除图像,我也希望将杯子放在同一个位置。我不明白这个问题,因为当您使用平面检测和 HitTest 添加对象时,还有一个用于平面的 ARAnchor。

The cup when device is moved

2018 年 5 月 14 日更新

 func addVirtualObjectWith(sceneName: String, childNodeName: String, objectName: String, position: SCNVector3, recursively: Bool, node: SCNNode?){
    print("VirtualObject: Added virtual object with scene name: \(sceneName)")

    let scene = SCNScene(named: "art.scnassets/\(sceneName)")!
    var sceneNode = scene.rootNode.childNode(withName: childNodeName, recursively: recursively)!
    sceneNode.name = objectName
    sceneNode.position = position

    add(object: sceneNode, toNode: node)

}

func add(object: SCNNode, toNode: SCNNode?){
    if toNode != nil {
        toNode?.addChildNode(object)
    }
    else {
        sceneView.scene.rootNode.addChildNode(object)
    }
}

最佳答案

我终于找到了解决方案,结果是属性检查器中的 AR 引用图像的大小设置不正确。当尺寸不正确时,图像的 anchor 会摇晃。

attributes inspector

关于ios - ARKit:将 3D 对象放置在图像之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49425383/

相关文章:

ios - Swift - 获取国家列表

ios11 - 转换后的 SCN 文件(来自 DAE 文件)未显示在 sceneView 中

ios - 如何快速设置警报文本字段中的数字输入 20 至 20000 数字?

iphone - iOS:录制语音编码的音频格式,用于网络传输

ios - 使用 UIImagePickerController 减少内存使用

ios - 如何在 Swift 中的不同 UIViewController 中显示计时器?

json - 将 JSON 中的对象数组解析为 Swift 中的对象

ios - 在 iOS 13 中,UITabBarItem 的标准外观适用于所有其他项目

iphone - 增强现实寻宝

swift - AR 对象在 RealityKit 中无法正确锚定或调整大小