gpt4 book ai didi

ios - 如何使用 ARKit 添加相对于跟踪图像位置的节点?

转载 作者:行者123 更新时间:2023-11-30 11:00:27 24 4
gpt4 key购买 nike

当检测到图像时,我在其前面添加一个节点。

Image - what i need garantee

节点是这张图片,并且相对于这张图片,我添加了其他节点,一个位于图片上方 (1),另一个位于图片下方 (2)。节点 1 总是处于正确的位置,但节点 2 有时会出现在图像前面,这是错误的,有时会出现在正确的位置,就像照片一样。

Image - wrong

for anchor in sceneView.session.currentFrame!.anchors {
let imageAnchor = anchor as! ARImageAnchor
nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100) //node 1
nodeAuthorInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100) //node 2

这是我用来设置两个节点的初始位置的代码。

所以我想知道如何保证我的第二个节点始终保持在正确的位置,即它位于图片旁边

如何添加节点:

let tappedNode = self.sceneView.hitTest(gesture.location(in: gesture.view), options: [:])

if let result = tappedNode.first{
nodeInformation.transform = result.modelTransform
nodeAuthorInformation.transform = result.modelTransform
}
print("TAP PLACE = ", gesture.location(in: gesture.view))
if !tappedNode.isEmpty{
let node = tappedNode[0].node
print("NODE TAP = ", node.position)
let artPlane = SCNPlane(width: 0.3, height: 0.3)

artPlane.firstMaterial?.diffuse.contents = arArtDetailsScene
artPlane.firstMaterial?.isDoubleSided = true

let planeNode = SCNNode(geometry: artPlane)
planeNode.eulerAngles.x = .pi

authorPlane = SCNPlane(width: 0.2, height: 0.3)
authorPlane.firstMaterial?.diffuse.contents = arAuthorDetailsScene
authorPlane.firstMaterial?.isDoubleSided = true

let planeAuthorNode = SCNNode(geometry: authorPlane)
planeAuthorNode.eulerAngles.x = .pi

nodeInformation.addChildNode(planeNode)
nodeAuthorInformation.addChildNode(planeAuthorNode)
changeArtDetails(artId: artIndex + 1)

sceneView.scene.rootNode.addChildNode(nodeInformation)
sceneView.scene.rootNode.addChildNode(nodeAuthorInformation)
if sceneView.session.currentFrame != nil {
for anchor in sceneView.session.currentFrame!.anchors {
let imageAnchor = anchor as! ARImageAnchor
nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100) // initial position node 1
nodeAuthorInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100) // initial position node 2
}
}

我像这样更新节点的位置:

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

if sceneView.session.currentFrame != nil {
for anchor in sceneView.session.currentFrame!.anchors {
if let imageAnchor = anchor as? ARImageAnchor{
DispatchQueue.main.async {

self.nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100)
self.nodeAuthorInformation.position = SCNVector3Make(self.nodeInformation.position.x + 0.3, self.nodeInformation.position.y - 0.3, self.nodeInformation.position.z)//SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100)

}
}
}
}
}

最佳答案

ARKit 更新 anchor 的位置,而不是节点的位置。如果 anchor 是 ARImageAnchor,您需要在 ARSCNViewDelegate.renderer(_ renderer: SCNSceneRenderer, nodeForanchor: ARAnchor) -> SCNNode? 中提供一个具有三个自定义节点作为子节点的节点而不是手动将其添加到场景中。

关于ios - 如何使用 ARKit 添加相对于跟踪图像位置的节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53450693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com