我目前正在SceneKit / Swift中开发一种建筑保护AR应用程序,该应用程序可检测目标图像(骆驼的图像)并将其覆盖在具有更新的骆驼图像纹理的平面上。我希望更新的图像平面在第一次检测到图像时淡入,而在不再检测到目标图像时淡出。
我通过camelNode跟踪场景中渲染节点的数量。
我的渐入渐出机制在起作用。最初,我在func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor)
中触发了渐变操作,但将其更改为func renderer(_ renderer: SCNSceneRenderer, willUpdate node: SCNNode, for anchor: ARAnchor)
。但是,淡出功能不起作用。我还尝试了didUpdate函数,并认为它没有达到imageAnchor.isTracked。当不再检测到图像时,渲染的平面将消失而不会褪色。
这是willUpdate函数的代码:
func renderer(_ renderer: SCNSceneRenderer, willUpdate node: SCNNode, for anchor: ARAnchor) {
guard anchor is ARImageAnchor else {return}
if let imageAnchor = anchor as? ARImageAnchor {
if (imageAnchor.isTracked == false) { // No longer tracking the object so trigger fade out
let imageSize = imageAnchor.referenceImage.physicalSize
let plane = SCNPlane(width: CGFloat(imageSize.width), height: CGFloat(camelHeight))
plane.firstMaterial?.diffuse.contents = toRenderImgs[0]
let camelToRenderNode = SCNNode(geometry: plane)
camelToRenderNode.name = "CamelNodeFront"
camelToRenderNode.position.z = -Float((imageSize.height-CGFloat(camelHeight))/2)
camelToRenderNode.position.x = -((Float(imageSize.width)-Float(camelWidth))/2)
camelToRenderNode.eulerAngles.x = -.pi / 2
camelToRenderNode.opacity = 1
node.addChildNode(camelToRenderNode)
let fadeOutAnimation = SCNAction.fadeOut(duration: 0.5)
camelToRenderNode.runAction(fadeOutAnimation, completionHandler: {
camelNodes[0].removeFromParentNode()
camelNodes.removeAll()
camelToRenderNode.removeFromParentNode()
})
} else {
if (camelNodes.count == 0) {
let imageSize = imageAnchor.referenceImage.physicalSize
let plane = SCNPlane(width: CGFloat(imageSize.width), height: CGFloat(camelHeight))
plane.firstMaterial?.diffuse.contents = camelToRenderImgs[0]
let camelToRenderNode = SCNNode(geometry: plane)
camelToRenderNode.name = "CamelNodeFront"
camelToRenderNode.position.z = -Float((imageSize.height-CGFloat(camelHeight))/2)
camelToRenderNode.position.x = -((Float(imageSize.width)-Float(camelWidth))/2)
camelToRenderNode.eulerAngles.x = -.pi / 2
camelToRenderNode.opacity = 0
let fadeInAnimation = SCNAction.fadeIn(duration: 0.5)
node.addChildNode(camelToRenderNode)
camelNodes.append(camelToRenderNode)
camelToRenderNode.runAction(fadeInAnimation)
}
}
}
}