Swift: как получить доступ к узлу рендеринга функций и сохранить через действие пользовательского интерфейса - PullRequest
0 голосов
/ 03 августа 2020

Я смотрел этот пост Текстура лица для печати вершин x и y:

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

Я могу напечатать новую позицию вот так. Например:

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

Цель состоит в том, чтобы сохранить newPosition при щелчке UIbutton.

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

Но я не могу получить доступ к newPosition напрямую, как в captureData: Use of unresolved identifier 'newPosition'

Может кто-нибудь указать мне, как получить доступ к рендереру и вызвать его в UIbutton, чтобы я мог сохранить вершины?

Спасибо!

1 Ответ

0 голосов
/ 03 августа 2020

Сложно предложить хорошее решение только с этими двумя частями кода. Но из того, что я собрал, вы хотите сохранить захваченные данные с новой позицией вашей кнопки.

Я предлагаю зафиксировать последнюю позицию в каком-то промежуточном свойстве, например:

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

, тогда вы сможете использовать его, когда кнопка больше не нажата.

...