如何像在 Measure 应用程序中一样在 ARKit (SceneKit) 中绘制虚线? [英] How to draw dashed line in ARKit (SceneKit) like in the Measure app?

查看:32
本文介绍了如何像在 Measure 应用程序中一样在 ARKit (SceneKit) 中绘制虚线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Just wanted to know is it possible (I know it is, but how) to draw dashed line in ARSCNView like in the Measure app? Maybe there is a way to use scene nodes out of the box, idk.

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

I've been using the SCNCylinder to draw a straight line and IDK is it possible to reuse it and adjust or we have to use a really different way to make the dashed line.

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)
    }

    ...

}

推荐答案

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

Probably not the most professional solution, but I started with a very similar approach. And then added the dashed-style like follows.

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

First I created an image that is half white, half transparent, to create the dashed-style

然后用在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^

接下来我相应地缩放它,按照我想要的方式重复它(使其尽可能好):

Next I scaled it accordingly, to repeat it (make it as fine) as I want it:

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

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

As I used a white image, I can "tint" it in any color I want:

material.multiply.contents = UIColor.green

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

To make it look more "2D like", ignore the lighting, using:

material.lighting = .constant

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

Additionally (as my Cylinder is rotated by 90°) I had to rotate the material as well:

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

每当线被调整大小时,相应地更新它的 SCNMatrix4MakeScale(参见上面的 width 和 height,对于height` 我只是把直径 (2*r)).

And whenever the line gets resized, update its SCNMatrix4MakeScale accordingly (see width and heightabove, where forheight` I just put the diameter (2*r)).

这篇关于如何像在 Measure 应用程序中一样在 ARKit (SceneKit) 中绘制虚线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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