简体   繁体   English

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

[英]Draw straight line from start value to end value ARKit

As always I need your help. 一如既往,我需要您的帮助。 I need to draw a straight line from a start value (declared as SCNVector3 ) and connected to the position in the real world till the end point. 我需要从起始值(声明为SCNVector3 )绘制一条直线,并将其连接到现实世界中的位置,直到终点。

Can someone explain to me with some lines of code how can I do it? 有人可以用几行代码向我解释我该怎么做? Thank you! 谢谢!

You'll need to implement: touchesBegan , touchesMoved , touchesEnded , touchesCancelled for your camera view controller. 您需要为相机视图控制器实现: touchesBegantouchesMovedtouchesEndedtouchesCancelled In touchesBegan you will need to make hitTest for current ARFrame in the UITouch location. touchesBegan您需要在UITouch位置为当前的ARFrame进行hitTest Then you'll have your startPosition and your lastTouch , which is your start UITouch . 然后,您将拥有startPositionlastTouch (即您的启动UITouch

Then you will need to add timer with 0.016_667 interval (60Hz) which will update your last touch position with hitTest when you camera moves. 然后,您将需要添加具有0.016_667间隔(60Hz)的计时器,当相机移动时,它将使用hitTest更新您的最后触摸位置。 Same you'll do in touchesMoved function. 与在touchesMoved功能中所做的相同。 And in touchesMoved you'll also update your lastTouch . touchesMoved您还将更新lastTouch So at this point you'll have your startPosition and currentPosition , which is SCNVector3 . 因此,此时您将拥有startPositioncurrentPosition ,即SCNVector3 And you can just redraw SCNCylinder (with 0.001m radius for example) for those positions if you need straight line. 如果需要直线,则可以为那些位置重新绘制SCNCylinder(例如,半径为0.001m)。

At last step in touchesEnded you'll fix your line, or if touchesCancelled you will remove it and clear lastTouch . touchesEnded最后一步,您将修复线路,或者如果touchesCancelled您将其删除并清除lastTouch

UPD UPD

If you need 2D line on the screen, then you'll need to use projectPoint for your ARSceneView. 如果您需要屏幕上的2D线,则需要对projectPoint使用projectPoint。

3D line drawing 3D线图

For drawing 3D line you could SCNGeometry use extension: 对于绘制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])
    }
}

Using: 使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM