【问题标题】:Displaying live information in 3d using arkit, possibly using scntext使用 arkit 以 3d 形式显示实时信息,可能使用 scntext
【发布时间】:2018-01-04 07:02:08
【问题描述】:

我已经尝试让它工作 3 天了。对于我的项目,我需要在用 Swift for iOS 编写的 AR 应用程序中将变化的值显示为 3d 文本。

我已经找到的: 我可以使用以下代码在某个位置生成 3d 静态文本。我可以将它放在我的 ViewController 的 ViewDidLoad 方法中,以便它在启动时加载一次。

let text = SCNText(string: "Let's begin!", extrusionDepth: 1)

//Create material
let material = SCNMaterial()
material.diffuse.contents = UIColor.green
text.materials = [material]

//Create Node object
let textNode = SCNNode()
textNode.scale = SCNVector3(x:0.004,y:0.004,z:0.004)
textNode.geometry = text
textNode.position = SCNVector3(x: 0, y:0.02, z: -0.5)

sceneView.scene.rootNode.addChildNode(textNode)

现在我的问题是我无法让它定期更改并说计数到 10000。

我尝试了很多想法,但没有一个显示数字在增加。

更新:我在创建节点后无法删除它。我也不知道什么时候必须删除它。

我收到了错误的访问代码=1 错误。问题似乎在于查找和删除节点,因为如果我评论应用程序启动的行。这可能与访问权限有关。

这是我的功能:

func updateSCNText2 (incomingInt: Int) {

    // create new text
    let text = SCNText(string: String(incomingInt), extrusionDepth: 1)
    //  create material
    let material = SCNMaterial()
    material.diffuse.contents = UIColor.green
    text.materials = [material]

    //Create Node object
    let textNode = SCNNode()
    textNode.name = "textNodeName"
    textNode.scale = SCNVector3(x:0.004,y:0.004,z:0.004)
    textNode.geometry = text
    textNode.position = SCNVector3(x: 0, y:0.02, z: -0.5)

    //  add new node to root node
    sceneView.scene.rootNode.addChildNode(textNode)

    //  find & remove previous node (childNodeWithName)
    sceneView.scene.rootNode.childNode(withName: "textNodeName", recursively: false)?.removeFromParentNode()

}

我在哪里调用函数:

var k = 0

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

    print(k)
    updateSCNText2(incomingInt:  k)
    k = k+1
}

非常感谢您抽出宝贵时间!

【问题讨论】:

  • 我会每次发生变化(比如计数从 20 -> 21 变化)找到节点(通过给它一个名称:textNode.name),删除该节点并放置另一个具有相同的节点名称和场景中的相同位置。

标签: ios swift arkit scntext


【解决方案1】:

最好的方法是使用 NSTimer

首先,在 ViewController 类的顶部设置 textNode 和一个计数器变量

var textNode: SCNNode!

var counter = 0

在 viewDidLoad 内部添加 NSTimer 调用:

var timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)

在 ViewDidLoad 中保留原来的 textNode 创建方法,除了 textNode 现在是 var

这是更新函数

@objc func update() {

    counter += 1

    // remove the old textNode

    textNode.removeFromParentNode()

        // create new text
        let text = SCNText(string: String(counter), extrusionDepth: 1)
        //  create material
        let material = SCNMaterial()
        material.diffuse.contents = UIColor.green
        text.materials = [material]

        //Create Node object
    textNode = SCNNode()
    textNode.scale = SCNVector3(x:0.004,y:0.004,z:0.004)
    textNode.geometry = text
    textNode.position = SCNVector3(x: 0, y:0.02, z: -0.5)

    //  add new node to root node
    self.sceneView.scene.rootNode.addChildNode(textNode)

}

注意:此代码有效,只是在操场上进行了测试。需要的可以提供。

【讨论】:

  • 天哪,你太棒了!非常感谢!
猜你喜欢
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 2018-01-30
  • 2018-12-10
相关资源
最近更新 更多