简体   繁体   中英

SCNShape with bezier path

I am tying to draw a 3d line in Scenekit iOS, i am trying below code to draw line,

func path() -> UIBezierPath
    {
        var bPath = UIBezierPath()
        bPath.moveToPoint(CGPointMake(0, 0))
        bPath.addLineToPoint(CGPointMake(10, 10))
        bPath.addLineToPoint(CGPointMake(5, 5))
        bPath.closePath()
        return bPath
    }

and the below code is to load the line in SCNNode,

let sap = SCNShape()
    sap.path = path()
    let carbonNode = SCNNode(geometry: sap)
    carbonNode.position = SCNVector3Make(0, 0, 15)
    scene.rootNode.addChildNode(carbonNode)

But, i am getting blank screen.

try changing your geometry's materials. You might have a white object on a white background.

edit: it also turns out that the vertices of your triangle are colinear. You thus end up with a degenerate triangle.

Your path has no surface area since you start at (0,0), draw a line to (10, 10) and then a second line to (5,5) which is already on the line between (0,0) and (10, 10). Then you close the path by drawing another line to (0,0).

Changing either of the three points creates a path with a surface area.

Also, the z-axis points out of the screen. This means that depending on where your camera is positioned, you may be placing the carbonNode behind the camera. If instead you meant to position it further into the scene you should likely use a negative z value.

Here's some code I wrote to address the problem.

    // Material colors
    let cyanMaterial = SCNMaterial()
    cyanMaterial.diffuse.contents = UIColor.cyanColor()

    let anOrangeMaterial = SCNMaterial()
    anOrangeMaterial.diffuse.contents = UIColor.orangeColor()

    let aPurpleMaterial = SCNMaterial()
    aPurpleMaterial.diffuse.contents = UIColor.purpleColor()

    // A bezier path
    let bezierPath = UIBezierPath()
    bezierPath.moveToPoint(CGPointMake(-0.25, 0))
    bezierPath.addLineToPoint(CGPointMake(0, -0.25))
    bezierPath.addLineToPoint(CGPointMake(0.25, 0))
    bezierPath.addLineToPoint(CGPointMake(0, 0.25))
    bezierPath.closePath()

    // Add shape
    let shape = SCNShape(path: bezierPath, extrusionDepth: 0.75)
    shape.materials = [cyanMaterial, anOrangeMaterial, aPurpleMaterial]
    let shapeNode = SCNNode(geometry: shape)
    shapeNode.position = SCNVector3(x: 0.2, y: 0.75, z: 0.1);
    self.rootNode.addChildNode(shapeNode)
    shapeNode.rotation = SCNVector4(x: -1.0, y: -1.0, z: 0.0, w: 0.0)

Keep in mind that the numbers, the Scene Kit scale, are in meters. So set the numbers similarly to those that work for the other shapes you create.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM