您需要实现:
touchesBegan, touchesMoved, touchesEnded, touchesCancelled
为您的相机视图控制器。
在touchesBegan 中,您需要为UITouch 位置中的当前ARFrame 创建hitTest。然后您将拥有您的startPosition 和您的lastTouch,这是您的开始UITouch。
然后您需要添加具有0.016_667 间隔(60Hz)的计时器,当您的相机移动时,它将使用hitTest 更新您的最后一次触摸位置。你会在touchesMoved 函数中做同样的事情。在touchesMoved 中,您还将更新您的lastTouch。所以此时您将拥有startPosition 和currentPosition,即SCNVector3。如果您需要直线,您可以为这些位置重绘 SCNCylinder(例如半径为 0.001m)。
在touchesEnded 的最后一步,您将修复您的线路,或者如果touchesCancelled 您将删除它并清除lastTouch。
UPD
如果您需要在屏幕上显示 2D 线,那么您需要为您的 ARSceneView 使用 projectPoint。
3D 线描
要绘制 3D 线,您可以 SCNGeometry 使用扩展名:
extension SCNGeometry {
class func line(from vector1: SCNVector3, to vector2: SCNVector3) -> SCNGeometry {
let indices: [Int32] = [0, 1]
let source = SCNGeometrySource(vertices: [vector1, vector2])
let element = SCNGeometryElement(indices: indices, primitiveType: .line)
return SCNGeometry(sources: [source], elements: [element])
}
}
使用:
let line = SCNGeometry.line(from: startPosition, to: endPosition)
let lineNode = SCNNode(geometry: line)
lineNode.position = SCNVector3Zero
sceneView.scene.rootNode.addChildNode(lineNode)