SceneKit draw curved line

可紊 提交于 2021-02-19 04:32:35

问题


I'd like to draw a bezier curved line with SceneKit and thought this would work:

func drawCurvedLine() {

    let scene = SCNScene()
    let scnView = self.view as! SCNView
    scnView.scene = scene
    scnView.allowsCameraControl = true

    let path = UIBezierPath()
    path.moveToPoint(CGPoint(x: -20, y: -20))
    path.addCurveToPoint(CGPoint(x: 20, y: 20),
        controlPoint1: CGPoint(x: 5, y: 5),
        controlPoint2: CGPoint(x: 15, y: 15))

    path.closePath()

    path.flatness = 0.3

    var scnShape:SCNShape = SCNShape(path: path, extrusionDepth: 2)

    scnShape.firstMaterial?.diffuse.contents = UIColor.purpleColor()
    let shapeNode = SCNNode(geometry: scnShape)
    scene.rootNode.addChildNode(shapeNode)
}

However my results are like this screenshot:SceneKit render

Does anyone know why the curveTo control points are ignored or how to switch them on? Or any other method to draw a curved line in SceneKit?

  • Edit Updating the path coordinates to:

    path.moveToPoint(CGPoint(x: -20, y: -20)) path.addCurveToPoint(CGPoint(x: 20, y: 20), controlPoint1: CGPoint(x: 10, y: 0), controlPoint2: CGPoint(x: 10, y: 0))

I can get this:curved

However I guess I'm looking to NOT fill the shape. Ideally a curved tubular line is what I'm trying to achieve here. ( Imagine a power cable or some such ... ). I guess I would love to have the equivalent of SCNLine or SCNTube "render on bezier path"


回答1:


SCNShape just be built from closed Bézier paths and will always be filled (with the exception of holes). These paths can be completely custom but will always be extruded along a segment along the z axis. What you want is to extrude a circle along an arbitrary path, and that's not supported.

SCNGeometry exposes APIs that allow you to build arbitrary geometries.




回答2:


It is working! Very well, I might add.

The problem is, all your points lie on the same line, y=x

Try changing your control points to have values where y!=x

ex,

    path.addCurveToPoint(CGPoint(x: 20, y: 20),
    controlPoint1: CGPoint(x: 15, y: 0),
    controlPoint2: CGPoint(x: 20, y: 10))


来源:https://stackoverflow.com/questions/31049103/scenekit-draw-curved-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!