0

I have the following code:

    func createScene(){
    count += 1

    let sphereGeom = SCNSphere(radius: 1.5)
    sphereGeom.firstMaterial?.diffuse.contents = UIColor.redColor()
    let path = UIBezierPath()
    path.moveToPoint(CGPoint(x: 0, y: 0))

    let radius = 3.0
    var radians = Double(0)
    var yPosition = Float(5.4)

    while count <= 20 {

        if radians >= 2{
            radians -= 2
        }

        let sphereNode = SCNNode(geometry: sphereGeom)
        let angle = Double(radians * M_PI)

        let xPosition = Float(radius * cos(angle))
        let zPosition = Float(radius * sin(angle))

        sphereNode.position = SCNVector3(xPosition, yPosition, zPosition)

        let cgX = CGFloat(xPosition)
        let cgY = CGFloat(yPosition)

        path.addQuadCurveToPoint(CGPoint(x: cgX, y: cgY), controlPoint: CGPoint(x: (cgX / 2), y: (cgY / 2)))
        path.addLineToPoint(CGPoint(x: (cgX - (cgX * 0.01)), y: cgY))
        path.addQuadCurveToPoint(CGPoint(x: 1, y: 0), controlPoint: CGPoint(x: (cgX / 2), y: ((cgY / 2) - (cgY * 0.01))))

        let shape = SCNShape(path: path, extrusionDepth: 3.0)
        shape.firstMaterial?.diffuse.contents = UIColor.blueColor()
        let shapeNode = SCNNode(geometry: shape)
        shapeNode.eulerAngles.y = Float(-M_PI_4)
        self.rootNode.addChildNode(shapeNode)

        count += 1
        radians += 0.5556
        yPosition -= 1.35

        self.rootNode.addChildNode(sphereNode)

    }

I want to add a Bezier path connecting each sphere to the next one, creating a spiral going down the helix. For some reason, when I add this code, the shape doesn't even appear. But when I use larger x and y values, I see the path fine, but it is no way oriented to the size of the spheres. I don't understand why it disappears when I try to make it smaller.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
ap524
  • 61
  • 8

1 Answers1

0

Your SCNShape doesn't ever get extruded. Per Apple doc,

An extrusion depth of zero creates a flat, one-sided shape.

With larger X/Y values your flat shape happens to become visible. You can't build a 3D helix with SCNShape, though: the start and end planes of the extrusion are parallel.

You'll have to use custom geometry, or approximate your helix with a series of elongated SCNBox nodes. And I bet someone out there knows how to do this with a shader.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42