绘制从起始值到结束值的直线 ARKit [英] Draw straight line from start value to end value ARKit

查看:28
本文介绍了绘制从起始值到结束值的直线 ARKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和往常一样,我需要你的帮助.我需要从起始值(声明为 SCNVector3)绘制一条直线,并连接到现实世界中的位置直到终点.

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.

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

Can someone explain to me with some lines of code how can I do it? Thank you!

推荐答案

您需要实施:touchesBegantouchesMovedtouchesEndedtouchesCancelled用于您的相机视图控制器.在 touchesBegan 中,您需要在 UITouch 位置为当前 ARFrame 制作 hitTest.然后你会有你的 startPosition 和你的 lastTouch,这是你的开始 UITouch.

You'll need to implement: touchesBegan, touchesMoved, touchesEnded, touchesCancelled for your camera view controller. In touchesBegan you will need to make hitTest for current ARFrame in the UITouch location. Then you'll have your startPosition and your lastTouch, which is your start UITouch.

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

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. Same you'll do in touchesMoved function. And in touchesMoved you'll also update your lastTouch. So at this point you'll have your startPosition and currentPosition, which is SCNVector3. And you can just redraw SCNCylinder (with 0.001m radius for example) for those positions if you need straight line.

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

At last step in touchesEnded you'll fix your line, or if touchesCancelled you will remove it and clear lastTouch.

UPD

如果您需要屏幕上的 2D 线,那么您需要将 projectPoint 用于您的 ARSceneView.

If you need 2D line on the screen, then you'll need to use projectPoint for your ARSceneView.

3D 线描

要绘制 3D 线,您可以SCNGeometry 使用扩展名:

For drawing 3D line you could SCNGeometry use extension:

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)

这篇关于绘制从起始值到结束值的直线 ARKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆