本文介绍了SceneKit 绘制曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 SceneKit 画一条贝塞尔曲线,并认为这可行:

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:

有谁知道为什么curveTo 控制点被忽略或如何打开它们?或者有什么其他方法可以在 SceneKit 中绘制曲线?

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

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

我可以得到这个:

但是我想我不希望填充形状.理想情况下,我试图在这里实现弯曲的管状线.(想象一下电源线或类似的东西......).我想我很想拥有相当于 SCNLine 或 SCNTube 的在贝塞尔路径上渲染"

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"

推荐答案

SCNShape 仅从封闭的 Bézier 路径构建,并且将始终填充(孔洞除外).这些路径可以完全自定义,但将始终沿 z 轴沿线段挤出.您想要的是沿任意路径挤出一个圆,但不支持.

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 公开了允许您构建任意几何图形的 API.

SCNGeometry exposes APIs that allow you to build arbitrary geometries.

这篇关于SceneKit 绘制曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:49