zumzum
zumzum

Reputation: 20198

Zoom in and out of a scene?

What is the correct way to zoom in and out of a scene in SceneKit?

So when I enable the standard camera control in a scene and pinch in and out the scene gets bigger and smaller. What is that pinch really doing?

Is it changing the scale of the whole scene? Is it moving the camera closer?

I want to implement the same effect but programmatically.

What should I do to obtain the same effect?

Upvotes: 5

Views: 4515

Answers (3)

Maury Markowitz
Maury Markowitz

Reputation: 9283

For those on OSX, I used this in my SCNView subclass:

override func scrollWheel(theEvent: NSEvent) {
    let cam = pointOfView!.camera
    cam!.xFov = cam!.xFov - Double(theEvent.deltaY)
    cam!.yFov = cam!.yFov - Double(theEvent.deltaY)
}

There are two (minor) problems that could be addressed with a little extra code. One is that the values can go negative, at which point the image is flipped inside-out. The other is that mouse acceleration can cause the zoom level to go too fast if you really spin the wheel. Limits on both of these would be a good idea, but in my app the behaviour was fine as it is above.

Upvotes: 0

user3694446
user3694446

Reputation: 111

At any given point the camera has a position in space, it has a rotation for each of its own axis compared to each of the world axis, to have a zoom in and zoom out, you have to move the camera in the +z/-z axis direction.

Along the Cameras own Z/-Z axis.

Upvotes: 0

mnuages
mnuages

Reputation: 13462

When you pinch it's the field of view (xFox and yFov properties) of your camera that's changed. Changing the field of view is not the best way to zoom because it can dramatically change the perspective.

Moving the camera closer to your object is a good solution.

Also note that the "free camera" behavior is suitable for 3D viewers (such as Preview.app) but will rapidly become frustrating in any other app. At this point you might want to implement your own camera controller.

Upvotes: 4

Related Questions