从起始值到终止值绘制直线ARKit

Pie*_*neo 2 ios swift arkit

一如既往,我需要您的帮助。我需要从起始值(称为SCNVector3)绘制一条直线,并将其连接到现实世界中的位置,直到终点。

有人可以用几行代码向我解释我该怎么做?谢谢!

Vas*_*vev 6

你需要实现: touchesBegantouchesMovedtouchesEndedtouchesCancelled 为您的相机视图控制器。在其中,touchesBegan您需要hitTest在该UITouch位置创建当前的ARFrame 。然后,您将拥有您startPosition和您的lastTouch,这就是您的开始UITouch

然后,您将需要添加0.016_667间隔为(60Hz)的计时器,该计时器将hitTest在您移动相机时更新您的最后触摸位置。与您在touchesMoved功能上所做的相同。并且touchesMoved您还将更新您的lastTouch。因此,此时您将拥有startPositionand currentPosition,即SCNVector3。如果需要直线,则可以为这些位置重新绘制SCNCylinder(例如,半径为0.001m)。

在最后一步,touchesEnded您将修复线路,或者如果touchesCancelled将其删除并清除lastTouch

UPD

如果您需要屏幕上的2D线,则需要将其projectPoint用于ARSceneView。

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])
    }
}
Run Code Online (Sandbox Code Playgroud)

使用方法:

let line = SCNGeometry.line(from: startPosition, to: endPosition)
let lineNode = SCNNode(geometry: line)
lineNode.position = SCNVector3Zero
sceneView.scene.rootNode.addChildNode(lineNode)
Run Code Online (Sandbox Code Playgroud)