像测量应用程序一样在场景套件中绘制虚线圆柱体? [英] Draw dashline cylinder in scenekit like measure app?

查看:46
本文介绍了像测量应用程序一样在场景套件中绘制虚线圆柱体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经关注了

我不确定我在这里遗漏了什么,请帮忙.

更新T:

事实证明,清晰的颜色(在图像中)在 SCNView 中呈现为黑色,而不是透明的.仍然不知道为什么绿色会变成这样.

解决方案

Line & 的另一种方法虚线

final class LineNode: SCNNode {变量颜色:UIColor?{设置 { 几何?.firstMaterial?.diffuse.contents = newValue }得到 { 几何?.firstMaterial?.diffuse.contents 作为?界面颜色 }}便利初始化(位置A:SCNVector3,位置B:SCNVector3,破折号:CGFloat = 0,在场景中:SCNScene?= nil){self.init()让索引:[Int32] = [0, 1]让 source = SCNGeometrySource(vertices: [positionA, positionB])让元素 = SCNGeometryElement(索引:索引,primitiveType:.line)几何 = SCNGeometry(来源:[来源],元素:[元素])几何?.firstMaterial?.diffuse.contents = UIColor.green几何?.firstMaterial?.lightingModel = .constant返回}}最终类 DashLineNode:SCNNode {方便初始化(位置A:SCNVector3,位置B:SCNVector3){self.init()让向量 = (positionB - positionA)让长度 = 地板(vector.length/1)让段=向量/长度让索引:[Int32] = Array(0..<Int32(length))var 顶点 = [positionA]对于 _ 索引 {vertices.append(vertices.last! + segment)}让源 = SCNGeometrySource(顶点:顶点)让元素 = SCNGeometryElement(索引:索引,primitiveType:.line)几何 = SCNGeometry(来源:[来源],元素:[元素])几何?.firstMaterial?.lightingModel = .constant}}

I've followed this question to try to make the dash cylinder

final class LineNode: SCNNode {

    convenience init(positionA: SCNVector3, positionB: SCNVector3) {
        self.init()

        let vector = SCNVector3(positionA.x - positionB.x, positionA.y - positionB.y, positionA.z - positionB.z)
        let distance = vector.length
        let midPosition = (positionA + positionB) / 2

        let lineGeometry = SCNCylinder()
        lineGeometry.radius = PileDrawer3D.lineWidth
        lineGeometry.height = CGFloat(distance)
        lineGeometry.radialSegmentCount = 5

        lineGeometry.firstMaterial?.diffuse.contents = dashedImage
        lineGeometry.firstMaterial?.diffuse.contentsTransform = SCNMatrix4MakeScale(distance * 10, Float(lineGeometry.radius * 10), 1)
        lineGeometry.firstMaterial?.diffuse.wrapS = .repeat
        lineGeometry.firstMaterial?.diffuse.wrapT = .repeat
        lineGeometry.firstMaterial?.isDoubleSided = true
        lineGeometry.firstMaterial?.multiply.contents = UIColor.green
        lineGeometry.firstMaterial?.lightingModel = .constant

        let rotation = SCNMatrix4MakeRotation(.pi / 2, 0, 0, 1)
        lineGeometry.firstMaterial?.diffuse.contentsTransform = SCNMatrix4Mult(rotation, lineGeometry.firstMaterial!.diffuse.contentsTransform)

        geometry = lineGeometry
        position = midPosition
        eulerAngles = SCNVector3.lineEulerAngles(vector: vector)

        name = className
    }

    lazy var dashedImage: UIImage = {

        let size = CGSize(width: 10, height: 3)
        UIGraphicsBeginImageContextWithOptions(size, true, 0)
        UIColor.white.setFill()
        UIRectFill(CGRect(x: 0, y: 0, width: 7, height: size.height))
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return img!
    }()

}

However, the pipes is not dashed.

I'm not sure what I'm missing here please help.

UpdateT:

It turns out that the clear color (in the image) is rendered as black, not transparent in the SCNView. Still, no idea why the green color got darken like this.

解决方案

Another approach for Line & DashLine

final class LineNode: SCNNode {

    var color: UIColor? {
        set { geometry?.firstMaterial?.diffuse.contents = newValue }
        get { geometry?.firstMaterial?.diffuse.contents as? UIColor }
    }

    convenience init(positionA: SCNVector3, positionB: SCNVector3, dash: CGFloat = 0, in scene: SCNScene? = nil) {
        self.init()
        let indices: [Int32] = [0, 1]
        let source = SCNGeometrySource(vertices: [positionA, positionB])
        let element = SCNGeometryElement(indices: indices, primitiveType: .line)
        geometry = SCNGeometry(sources: [source], elements: [element])
        geometry?.firstMaterial?.diffuse.contents = UIColor.green
        geometry?.firstMaterial?.lightingModel = .constant

        return
    }
}

final class DashLineNode: SCNNode {
    convenience init(positionA: SCNVector3, positionB: SCNVector3) {
        self.init()
        let vector = (positionB - positionA)
        let length = floor(vector.length / 1)
        let segment = vector / length

        let indices:[Int32] = Array(0..<Int32(length))
        var vertices = [positionA]
        for _ in indices {
            vertices.append(vertices.last! + segment)
        }
        let source = SCNGeometrySource(vertices: vertices)
        let element = SCNGeometryElement(indices: indices, primitiveType: .line)

        geometry = SCNGeometry(sources: [source], elements: [element])
        geometry?.firstMaterial?.lightingModel = .constant
    }
}

这篇关于像测量应用程序一样在场景套件中绘制虚线圆柱体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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