gpt4 book ai didi

ios - 渲染回调时场景被修改

转载 作者:可可西里 更新时间:2023-11-01 03:34:02 28 4
gpt4 key购买 nike

我有以下产生错误的代码:

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

if anchor is ARImageAnchor {

let phoneScene = SCNScene(named: "Phone_01.scn")!
let phoneNode = phoneScene.rootNode.childNode(withName: "parentNode", recursively: true)!

// rotate the phone node
let rotationAction = SCNAction.rotateBy(x: 0, y: 0.5, z: 0, duration: 1)
let inifiniteAction = SCNAction.repeatForever(rotationAction)
phoneNode.runAction(inifiniteAction)
phoneNode.position = SCNVector3(anchor.transform.columns.3.x,anchor.transform.columns.3.y + 0.1,anchor.transform.columns.3.z)
node.addChildNode(phoneNode)
}
}

场景在另一个场景的渲染回调中被修改。

所以我将其替换为以下内容:

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

if anchor is ARImageAnchor {

DispatchQueue.global().async {

let phoneScene = SCNScene(named: "Phone_01.scn")!
let phoneNode = phoneScene.rootNode.childNode(withName: "parentNode", recursively: true)!

DispatchQueue.main.async {

// rotate the phone node
let rotationAction = SCNAction.rotateBy(x: 0, y: 0.5, z: 0, duration: 1)
let inifiniteAction = SCNAction.repeatForever(rotationAction)
phoneNode.runAction(inifiniteAction)
phoneNode.position = SCNVector3(anchor.transform.columns.3.x,anchor.transform.columns.3.y + 0.1,anchor.transform.columns.3.z)
node.addChildNode(phoneNode)
}
}
}
}

现在错误消失了,一切正常。我的问题是:这是正确的解决方案吗?我应该切换到后台线程来加载场景,然后切换到主线程来添加节点吗?节点是否甚至添加到主线程上?

最佳答案

在方法委托(delegate)中尝试这样的事情。这是一个旧项目的示例。

DispatchQueue.main.async {
if let imageAnchor = anchor as? ARImageAnchor {

let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
plane.firstMaterial?.diffuse.contents = UIColor(white: 1.0, alpha: 0.5)

let planeNode = SCNNode(geometry: plane)
planeNode.eulerAngles.x = -.pi
node.addChildNode(planeNode)

...

}
}

关于ios - 渲染回调时场景被修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51885884/

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