如何在Measure应用程序中在ARKit(SceneKit)中绘制虚线? [英] How to draw dashed line in ARKit (SceneKit) like in the Measure app?
问题描述
只是想知道是否有可能(如我所知,但是如何)像在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 for
height` I just put the diameter (2*r)).
这篇关于如何在Measure应用程序中在ARKit(SceneKit)中绘制虚线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!