我试图在参考图像上正确添加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)
}
}
问题在于,当我移动设备方向时,杯子不会在图像的中间保持良好状态。即使我删除图像,我也希望将杯子放在同一位置。我没有遇到问题,因为当您使用平面检测和命中测试添加对象时,还会有一个用于飞机的ARAnchor。
更新05/14/2018
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)
}
}