How to smoothly change the FoV of an SCNCamera that won't animate?

614 Views Asked by At

I have an SCNCamera working in a 3D SceneKit view on iOS.

When I change orientation of the screen, in order to keep the world objects the same size and relative positions I need to change the FoV of the SCNCamera.

However, I can't animate this change, so instead the world 'flashes' or 'pulses' as the FoV is changed from one setting to another.

Is there a way of doing this smoothly so that the change in FoV isn't so abrupt that it causes the objects in the world to 'pulse'?

When the device changes orientation, this code is executed:

if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight
{
    self.cameraNode?.camera?.fieldOfView  = 38
}
else
{
    self.cameraNode?.camera?.fieldOfView  = 50
}

I've tried wrapping the FoV change in a UIView animation block but that didn't work.

I've tried to run it on it's on thread with a delay but that still caused the visual 'pulse' as the FoV changed.

Is there some way to make this transition smooth?

1

There are 1 best solutions below

8
On BEST ANSWER

You need to add your code with SCNTransaction like as follow to do animation.

The SCNTransaction class defines SceneKit's architecture for scene content changes, including implicit animations: A transaction is a single atomic operation that combines multiple changes to nodes, geometries, materials, or other scene content.

    if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
    SCNTransaction.begin()
    SCNTransaction.animationDuration = 1.5
    self.cameraNode?.camera?.fieldOfView = 38
    SCNTransaction.commit()
} else {
    SCNTransaction.begin()
    SCNTransaction.animationDuration = 1.5
    self.cameraNode?.camera?.fieldOfView = 50
    SCNTransaction.commit()
}