簡體   English   中英

從起始值到終止值繪制直線ARKit

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

一如既往,我需要您的幫助。 我需要從起始值(聲明為SCNVector3 )繪制一條直線,並將其連接到現實世界中的位置,直到終點。

有人可以用幾行代碼向我解釋我該怎么做? 謝謝!

您需要為相機視圖控制器實現: touchesBegantouchesMovedtouchesEndedtouchesCancelled touchesBegan您需要在UITouch位置為當前的ARFrame進行hitTest 然后,您將擁有startPositionlastTouch (即您的啟動UITouch

然后,您將需要添加具有0.016_667間隔(60Hz)的計時器,當相機移動時,它將使用hitTest更新您的最后觸摸位置。 與在touchesMoved功能中所做的相同。 touchesMoved您還將更新lastTouch 因此,此時您將擁有startPositioncurrentPosition ,即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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM