SCNShape with bezier path

半城伤御伤魂 提交于 2019-12-11 02:14:39

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/28190604/scnshape-with-bezier-path

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