gpt4 book ai didi

ios - Swift:使用scenekit 中的projectPoint 随时间获取并保存更新的SCNNode

转载 作者:行者123 更新时间:2023-12-01 16:05:13 58 4
gpt4 key购买 nike

我正在尝试使用 projectPoint在scenekit中获取更新后的SCNNode的二维信息并保存。
根据 ignotusverum 的建议,我可以将 SCNNode 保存到按钮中的路径中。

     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 }
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)
Position = CGPoint(x: xVertex, y: yVertex)
}
selectedContentController.session = sceneView?.session
selectedContentController.sceneView = sceneView
selectedContentController.renderer(renderer, didUpdate: contentNode, for: anchor)
}

通过开始按钮开始保存:
    private var fpsTimer = Timer()
private var currentCaptureFrame = 0
@IBAction private func startPressed() {
currentCaptureFrame = 0 //inital capture frame
fpsTimer = Timer.scheduledTimer(withTimeInterval: 1/fps, repeats: true, block: {(timer) -> Void in self.recordData()})
}

通过单击停止按钮保存它们:
@IBAction private func stopPressed() {
do {
fpsTimer.invalidate() //turn off the timer
let capturedData = captureData.map{$0.stringRepresentation}.joined(separator:"\(lastPosition)>")
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")
}
}
到目前为止,保存点的效果很好。问题是在保存的数据中,我意识到数据只保存了 x 和 y 顶点的一帧。例如:
[(411.0618591308594, 534.4215087890625), (410.7286071777344, 544.9381713867188), (411.5425720214844, 522.1063232421875), (412.0340881347656, 512.1854248046875),... 

[(411.0618591308594, 534.4215087890625), (410.7286071777344, 544.9381713867188), (411.5425720214844, 522.1063232421875), (412.0340881347656, 512.1854248046875)
数据以一帧重复,而不是从我单击开始按钮到停止按钮的那一刻起保存的时间段。
我的问题是如何从我单击开始按钮到停止按钮的那一刻起随时间保存更新的 SCNNode?
提前致谢!

最佳答案

如果我正确理解您的问题,您希望在每次更新顶点位置时保存它们,跟踪所有以前的更新以及最近的更新。为此,您只需将新的顶点位置数组附加到具有保存数据的全局数组中。

 var savedPositions = [CGPoint]()
var beginSaving = false

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
guard anchor == currentFaceAnchor,
let contentNode = selectedContentController.contentNode,
contentNode.parent == node
else { return }
for vertex in vertices {
let projectedPoint = sceneView.projectPoint(node.convertPosition(SCNVector3(vertex), to: nil))
if beginSaving {
savedPositions.append(CGPoint(x: projectedPoint.x, y: projectedPoint.y))
}
}
selectedContentController.session = sceneView?.session
selectedContentController.sceneView = sceneView
selectedContentController.renderer(renderer, didUpdate: contentNode, for: anchor)
}

@IBAction private func startPressed() {
beginSaving = true
}

@IBAction private func stopPressed() {
beginSaving = false
....//do whatever with your array of saved positions
}

关于ios - Swift:使用scenekit 中的projectPoint 随时间获取并保存更新的SCNNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63251149/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com