ARKit 将 3d 对象位置转换为 UIView 坐标

ARKit Convert 3d object position to UIView coordinates

我创建了测量演示,它允许放置多个点并显示它们之间的距离。效果很好

我想使用 UIBezierPath 显示到目前为止在现实世界中绘制到 UIView 的预览。就像 http://armeasure.com/

我尝试了很多方法来实现这一点,但我找不到任何正确的方法。

if self.linkList.count == 1 {
    bezierPath.removeAllPoints()
    bezierPath.move(to: CGPoint(x: 10,y: 10))
} else {
    guard self.linkList.count > 1 ,let object2 = self.linkList.lastNode, let object1 = self.linkList.lastNode?.previous else {return}


    let value = self.getMeasurementXandYBetween(vector1: object1.node.mainNode.position, and: object2.node.mainNode.position)
    print(value)
    let x = Double((object1.node.mainNode.position.x + value ) * 377.9527559055 )
    let y = Double((object1.node.mainNode.position.y + value) * 377.9527559055)
    let pointCoordinates = CGPoint(x: x , y: y)
    print("x : Y ",x,y)
    bezierPath.addLine(to: pointCoordinates)

}
shapeLayer.removeFromSuperlayer()
shapeLayer.path = bezierPath.cgPath
shapeLayer.lineWidth = 0.5
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.position = CGPoint(x: 0, y: 0)


self.viewToDraw.layer.addSublayer(shapeLayer)
func getMeasurementXandYBetween(vector1:SCNVector3, and vector2:SCNVector3) -> Float {
    return sqrtf((vector1.x - vector2.x) * (vector1.x - vector2.x) +  (vector1.y - vector2.y) * (vector1.y - vector2.y))
}

我使用的逻辑(这是行不通的)前一个节点的位置+我从getMeasurementXandYBetween得到的距离乘以377。

请提出提示或任何其他解决方案

您可以使用SCNSceneRenderer 上的projectPoint() 获取屏幕-space 中任意点的坐标。 这将为您提供一个包含 3 个元素的矢量,使用前两个元素构建您的 CGPoint 并从这些点构建您的形状。