0

I was looking at this post Face texture to print the x and y vertices:

    func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
        guard anchor == currentFaceAnchor,
            let contentNode = selectedContentController.contentNode,
            contentNode.parent == node
            else { return }
        let vertices = currentFaceAnchor!.geometry.vertices
        for (index, vertex) in vertices.enumerated() {
            let vertex = sceneView.projectPoint(node.convertPosition(SCNVector3(vertex), to: nil))
            let xVertex = CGFloat(vertex.x)
            let yVertex = CGFloat(vertex.y)
            let newPosition = CGPoint(x: xVertex, y: yVertex)
        print(newPosition)
        }
  }

I am able to print the newPosition like so. For example:

(391.0479431152344, 577.9422607421875)
(379.6573791503906, 579.8109741210938)
(369.43145751953125, 583.1146240234375)
(362.12176513671875, 587.81787109375)...

The goal is I want to save newPosition in a UIbutton click.

@IBAction private func stopPressed() {
    do {
        fpsTimer.invalidate() //turn off the timer
        let capturedData = captureData.map{$0.stringRepresentation}.joined(separator:"\(newPosition)>")
        let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
        let url = dir.appendingPathComponent("testing.txt")
        try capturedData.appendLineToURL(fileURL: url as URL)
    }
    catch {
        print("Could not write to file")
    }
}

But I can't access newPosition directly like so in the captureData: Use of unresolved identifier 'newPosition'

Can anyone direct me on how to access the renderer and be called in the UIbutton so I can save the vertices?

Thanks!

swiftlearneer
  • 324
  • 4
  • 17

1 Answers1

0

It's slightly hard to suggest a good solution with only those two pieces of code. But from what I gathered - you want to store captured data with the new position of your button.

My suggestion would be to capture last position in some kind of intermediate property such as:

    var lastPosition: CGPoint? 

    func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
        guard anchor == currentFaceAnchor,
            let contentNode = selectedContentController.contentNode,
            contentNode.parent == node
            else { return }
        let vertices = currentFaceAnchor!.geometry.vertices
        for (index, vertex) in vertices.enumerated() {
            let vertex = sceneView.projectPoint(node.convertPosition(SCNVector3(vertex), to: nil))
            let xVertex = CGFloat(vertex.x)
            let yVertex = CGFloat(vertex.y)
            self.newPosition = CGPoint(x: xVertex, y: yVertex)
        }
  }

then you would be able to use it when the button is no longer pressed.

Vlad Z.
  • 3,401
  • 3
  • 32
  • 60