[英]Draw straight line from start value to end value ARKit
一如既往,我需要您的幫助。 我需要從起始值(聲明為SCNVector3
)繪制一條直線,並將其連接到現實世界中的位置,直到終點。
有人可以用幾行代碼向我解釋我該怎么做? 謝謝!
您需要為相機視圖控制器實現: 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線,則需要對projectPoint
使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.