0

私はこれを3日間機能させようとしています。私のプロジェクトでは、変化する値を iOS 用の Swift で記述された AR アプリケーションでレンダリングされた 3d テキストとして表示する必要があります。

私がすでに見つけたこと: 次のコードを使用して、特定の場所に 3D で静的テキストを生成できます。ViewController の ViewDidLoad メソッドに配置して、起動時に 1 回読み込まれるようにすることができます。

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
}

お時間を割いていただき、ありがとうございました!

4

1 に答える 1

0

Best way to do this is using NSTimer

Firstly, set textNode & a counter var at the top of your ViewController class

var textNode: SCNNode!

var counter = 0

inside of viewDidLoad add the NSTimer call:

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

leave you original textNode creation method in ViewDidLoad except textNode is now var

this is the update function

@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)

}

Note: this code works, just tested it out in a playground. Can provide if needed.

于 2018-01-03T22:21:15.717 に答える