【问题标题】:Draw straight line from start value to end value ARKit从起始值到结束值绘制直线 ARKit
【发布时间】:2018-02-05 13:39:57
【问题描述】:

一如既往,我需要你的帮助。我需要从起始值(声明为SCNVector3)画一条直线,并连接到现实世界中的位置直到终点。

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

【问题讨论】:

  • This 是吗。
  • 我需要一条直线,而不仅仅是画一条线。谢谢!
  • 你能再澄清一点吗?连接到现实世界中的位置直到终点是什么意思?
  • 嗨艾伦,感谢您的提问。我有一个 touchesBegan 函数,它开始监视场景,因为这个函数我想开始画一条直线,直到我将手指从屏幕上移开,事件将返回这条线,直到我删除的最后一个位置手指。
  • 你想要场景中的 3D 线还是 UIView 上的 2D 线?

标签: ios swift arkit


【解决方案1】:

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

然后您需要添加具有0.016_667 间隔(60Hz)的计时器,当您的相机移动时,它将使用hitTest 更新您的最后一次触摸位置。你会在touchesMoved 函数中做同样的事情。在touchesMoved 中,您还将更新您的lastTouch。所以此时您将拥有startPositioncurrentPosition,即SCNVector3。如果您需要直线,您可以为这些位置重绘 SCNCylinder(例如半径为 0.001m)。

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

UPD

如果您需要在屏幕上显示 2D 线,那么您需要为您的 ARSceneView 使用 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)

【讨论】:

  • 非常感谢您的解释。我已经有一个 touchesBegan 和 touchesEnded 函数,但我还没有真正了解如何绘制 SCNCylinder 以及代码如何理解如何将起点连接到终点?我需要在 touchesBegan 上设置节点 1 与 startPosition 相同,但在 touchesEnded 内设置第二个节点?
  • @PietroMessineo 你可以试试let line = SCNGeometry.triangleFrom(vector1: startPoint, vector2: startPoint, vector3: endPoint) 然后let lineNode = SCNNode(geometry: line)
  • 有一个很好的例子:stackoverflow.com/questions/35002232/…
  • 谢谢!我会尽快尝试;)
  • 我不知道为什么但是:“类型'SCNGeometry'没有成员'triangleFrom'”
猜你喜欢
  • 1970-01-01
  • 2015-06-18
  • 2022-08-17
  • 1970-01-01
  • 2014-08-08
  • 1970-01-01
  • 2018-05-17
  • 2017-02-22
  • 2017-06-18
相关资源
最近更新 更多