【问题标题】:How to draw dashed line in ARKit (SceneKit) like in the Measure app?如何像在 Measure 应用程序中一样在 ARKit (SceneKit) 中绘制虚线?
【发布时间】:2019-05-18 14:48:51
【问题描述】:

只是想知道是否可以像在 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)
    }

    ...

}

【问题讨论】:

    标签: swift scenekit arkit scnnode


    【解决方案1】:

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

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

    然后用在SCNCylinder的素材中:

    material.diffuse.contents = UIImage(named: "line")!
    material.diffuse.wrapS = .repeat
    material.diffuse.wrapT = .repeat
    material.isDoubleSided = true // Not sure if this is really needed here^
    

    接下来我对它进行了相应的缩放,按照我的意愿重复它(让它尽可能好):

    material.diffuse.contentsTransform = SCNMatrix4MakeScale(width * repeatCountPerMeter, height * repeatCountPerMeter, 1)
    

    当我使用白色图像时,我可以将其“着色”为我想要的任何颜色:

    material.multiply.contents = UIColor.green
    

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

    material.lighting = .constant
    

    此外(因为我的圆柱体旋转了 90°)我还必须旋转材质:

    let rotation = SCNMatrix4MakeRotation(.pi / 2, 0, 0, 1)
    material.diffuse.contentsTransform = SCNMatrix4Mult(rotation, material.diffuse.contentsTransform)
    

    每当调整线的大小时,相应地更新其SCNMatrix4MakeScale(请参阅width 和heightabove, where forheight,我只是输入了直径(2*r))。

    【讨论】:

    • 好的,谢谢。我已经想出了相同的方法,所以将在这里发布我的解决方案(它与您的非常相似)。
    • @atereshkov 嘿兄弟,你的解决方案在哪里?我无法使这个解决方案起作用:(
    • @Eddie 为什么它对你不起作用?也许我可以帮忙。
    • @d4Rk 我不确定我错过了哪里,因为我不太明白,只是复制代码。也许转换丢失了,因为我不清楚widthheightrepeatCountPerMeter 是什么。我正在使用由 UIGraphicContext 创建的图像,所以图像可能没有问题。也许内容转换不正确? :|
    • @d4Rk 我在这里发布了一个新问题:stackoverflow.com/questions/63502194/…
    猜你喜欢
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    相关资源
    最近更新 更多