Harrison
Harrison

Reputation: 69

Unable to set initial radius of one SCNSphere

I am able to add twenty small spheres of, say, radius 0.5 to my scene.

However, when I attempt to only add one sphere, no matter what radius I specify, the sphere appears with the default radius of 1.0.

Here is the code that I'm using to add just one sphere. I merely put this code inside a for loop to add twenty spheres -- which works just fine.

func drawSpheres() {
    var x:Float = 0.0
    var radius:CGFloat = 0.5
    let sphereGeometry = SCNSphere(radius: radius)
    let sphereNode = SCNNode(geometry: sphereGeometry)
    sphereNode.position = SCNVector3(x: x, y: 0.0, z: 0.0)
    self.rootNode.addChildNode(sphereNode)
}

Am I missing something obvious?

Upvotes: 1

Views: 178

Answers (1)

David Rönnqvist
David Rönnqvist

Reputation: 56625

The problem that you are seeing is just Scene Kit trying to be smart. Everything is actually working as expected.

To be able to render your scene, Scene Kit needs a camera to be the point of view. If your scene contains a camera, that will automatically be made the point of view. But if your scene doesn't have a camera in it, Scene Kit has to create its own (or fail to render all together, which would be less preferable). When Scene Kit creates its own "default" point of view, it tries to adapt to the content of the scene so that it fills the view as best as possible (a slight simplification).

This can be a nice little convenience, but in your case it means that when you make your sphere larger, Scene Kit adapts its point of view so that the sphere still covers the entire view. If there is only one sphere in the scene, this means that you can't see any difference in the radius of the sphere because it will always cover the entire view.

If you add your own camera to the scene and configure it to your liking, you will be able to see the difference when changing the radius of the sphere.

Upvotes: 2

Related Questions