我创建了示例应用程序以在3d对象上进行绘制,输出图形不平滑(如图所示)。我花了将近一天的时间弄清楚仍然无法解决的问题。可能是什么问题? ios - SceneKit –在3D对象上绘图-LMLPHP

我正在创建如下的自定义工程图几何

//I am creating self.drawingNode touches begin and adding to the rootnode
//points are nothing but 2d points from touches

func createGeometryForPoints(points:[CGPoint]) -> SCNGeometry {

    var all_3d_points:[SCNVector3] = []

    for point in points {

        let result = self.get3dPoint(point)

        if result.1 == true {
            all_3d_points.append(result.0)
        } else {
            print("INVALID POINT")
        }
    }

    var indices: [Int32] = []
    var index:Int32 = 0

    var previousIndex:Int32 = -1

    for _ in all_3d_points {

        if(previousIndex != -1) {

            indices.append(previousIndex)
        }
        indices.append(index)
        index = index + 1
        previousIndex = index
    }

    if indices.count > 0 {
        indices.removeLast();
    }

    let indexData = NSData(bytes: indices, length: sizeof(Int32) * indices.count)

    let source = SCNGeometrySource.init(vertices: all_3d_points, count: all_3d_points.count)
    let element = SCNGeometryElement.init(data: indexData, primitiveType: .Line, primitiveCount: indices.count/2, bytesPerIndex: sizeof(Int32))

    let line = SCNGeometry(sources: [source], elements: [element])
    line.materials.first?.diffuse.contents = UIColor.redColor()
    line.materials.first?.doubleSided = true

    return line
}

func get3dPoint(point:CGPoint) -> (SCNVector3,Bool) {

    var canAdd = false
    let scnView = self.view as! SCNView
    var unprojectedStartPoint = SCNVector3.init(x:Float(point.x), y:Float(point.y), z:0)

    let hitResults = scnView.hitTest(point, options: nil)
    if hitResults.count > 0 {

        let result: AnyObject! = hitResults[0]
        if hitResults.count > 1 {
            print("SOME PROBLEM")
        }
        unprojectedStartPoint = result.localCoordinates
        unprojectedStartPoint = result.node.convertPosition(unprojectedStartPoint, toNode: self.drawingNode!)

        canAdd = true
    }

    return (unprojectedStartPoint,canAdd)
}

编辑

我正在考虑找到与摄像机方向或法线方向相同的偏移量,因为我是Scenekit的新手,所以我没有获得如何向绘图节点添加此偏移量的信息,在此方向上寻求帮助



编辑:2

通过沿命中测试的法线添加较小的偏移量来解决此问题
let offsetToAvoidFlickering: Float = 0.05
unprojectedStartPoint = unprojectedStartPoint + result.localNormal.normalized() * offsetToAvoidFlickering

最佳答案

我怀疑您有深度精度问题;看起来您的直线部分已被裁剪,因为它们相交于球体的各个部分。尝试在禁用深度测试的情况下画线。

07-28 09:23