如何像在 Measure 应用程序中一样在 ARKit (SceneKit) 中绘制虚线?

ate*_*kov 5 scenekit swift scnnode arkit

只是想知道是否有可能(我知道是,但如何)像在 Measure 应用程序中一样在 ARSCNView 中绘制虚线?也许有一种方法可以开箱即用地使用场景节点,idk。

我一直在使用 SCNCylinder 画一条直线,IDK 是否可以重复使用它并进行调整,或者我们必须使用一种非常不同的方式来制作虚线。

import SceneKit

class CylinderLineNode: SCNNode {

    private(set) var cylinder: SCNCylinder
    private(set) var positionA: SCNVector3
    private(set) var positionB: SCNVector3

    init(with positionA: SCNVector3, positionB: SCNVector3, radius: CGFloat = 0.02, color: UIColor = .red) {
        self.positionA = positionA
        self.positionB = positionB
        let vector = positionB - positionA
        let height = vector.length()
        cylinder = SCNCylinder(radius: radius, height: CGFloat(height))
        cylinder.radialSegmentCount = 8
        cylinder.firstMaterial?.diffuse.contents = color
        super.init()
        geometry = cylinder
        position = (positionB + positionA) / 2
        eulerAngles = SCNVector3.lineEulerAngles(vector: vector)
    }

    ...

}
Run Code Online (Sandbox Code Playgroud)

d4R*_*4Rk 0

可能不是最专业的解决方案,但我从一个非常相似的方法开始。然后添加虚线样式,如下所示。

\n\n

首先,我创建了一个半白、半透明的图像,以创建虚线样式

\n\n

然后将其用于以下材料SCNCylinder

\n\n
material.diffuse.contents = UIImage(named: "line")!\nmaterial.diffuse.wrapS = .repeat\nmaterial.diffuse.wrapT = .repeat\nmaterial.isDoubleSided = true // Not sure if this is really needed here^\n
Run Code Online (Sandbox Code Playgroud)\n\n

接下来,我相应地缩放它,以按照我的需要重复它(使其尽可能精细):

\n\n
material.diffuse.contentsTransform = SCNMatrix4MakeScale(width * repeatCountPerMeter, height * repeatCountPerMeter, 1)\n
Run Code Online (Sandbox Code Playgroud)\n\n

由于我使用了白色图像,因此我可以将其“着色”为我想要的任何颜色:

\n\n
material.multiply.contents = UIColor.green\n
Run Code Online (Sandbox Code Playgroud)\n\n

为了使其看起来更像“2D”,请忽略照明,使用:

\n\n
material.lighting = .constant\n
Run Code Online (Sandbox Code Playgroud)\n\n

另外(因为我的圆柱体旋转了 90\xc2\xb0),我还必须旋转材料:

\n\n
let rotation = SCNMatrix4MakeRotation(.pi / 2, 0, 0, 1)\nmaterial.diffuse.contentsTransform = SCNMatrix4Mult(rotation, material.diffuse.contentsTransform)\n
Run Code Online (Sandbox Code Playgroud)\n\n

每当调整线的大小时,都会SCNMatrix4MakeScale相应地更新它(参见width高度above, where forheight`,我只是输入直径(2 * r))。

\n