3

Animating the backgroundColor property of a SCNView happens instantly, instead of gradually over the specified duration.

Here's the code:

UIView.animate(withDuration: 0.5, animations: {
     self.sceneView.backgroundColor = UIColor.redColor
})

This question is similar, but its solutions didn't work. Specifically, setting the backgroundColor beforehand and animating against the layer property did not help.

How can you animate the backgroundColor of a SCNView?

Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • For anyone reading here, note that the question is about a **scene view** but the answer below (2018 answer) is about a **scene**. – Fattie Nov 18 '22 at 15:47

1 Answers1

3

Change the contents of the background material instead. This will give you full SCN Material powers over what the background is, and the ability to use SCN Actions to animate any changes you make.

https://developer.apple.com/documentation/scenekit/scnscene/1523665-background


Better way, and what I'd suggest, is to use CAAnimation. From the docs:

You can attach animations to Scene Kit objects that adopt the SCNAnimatable protocol, including nodes, geometries, and materials.


Generally speaking, the diffuse and ambient colours of a material will be the ones to change to make an appearance adjustment to a background material, depending on how you setup lighting.

Confused
  • 6,048
  • 6
  • 34
  • 75
  • How do you use SCNActions to change the background material (or color)? There's no colorize action (similar to the SpriteKit actions). Thanks for your help! – Crashalot Mar 10 '18 at 11:24
  • Change the values of variables being using to define a material. This way they'll animate the look of the material. – Confused Mar 10 '18 at 11:43
  • You bring up a good point, SceneKit should have a modular, visual programming environment for materials, like Unreal and every other 3D engine. That would make this fun, and not feel like using 3ds Max's material editor in 1999. – Confused Mar 10 '18 at 11:44
  • Thanks for your help! So use which SCNAction function? `class func customAction(duration: TimeInterval, action: (SCNNode, CGFloat) -> Void)`? – Crashalot Mar 10 '18 at 19:57
  • Yes, that. Or make the property SCNActionable and use a CA Animation. – Confused Mar 11 '18 at 00:41
  • OK, thanks. Never used `customAction` before, will dig around for examples online. Thanks for your help! – Crashalot Mar 11 '18 at 21:01
  • Have a look at this, I'd say it's better than custom actions: https://developer.apple.com/documentation/quartzcore/caanimation – Confused Mar 12 '18 at 04:08
  • thanks! but how would you use this to animate the background color of the scene? – Crashalot Mar 12 '18 at 10:00
  • Get a reference to the property of the background material that you want to animate as a variable. Then change its value as you desire/need. – Confused Mar 12 '18 at 10:34
  • Added links and quote to answer. Hope it helps. @Crashalot – Confused Mar 12 '18 at 10:43
  • Very glad to help. Hope you're doing well!!! Will be pestering you about explaining initialisation to me... soon. I'm dense as two (or three) bricks. – Confused Mar 13 '18 at 07:51
  • Hah, will try! Thanks again for your help! – Crashalot Mar 13 '18 at 15:49
  • I've also found that if you need to update the background color frequently (e.g. a changing sky color), performance is much better using the scene's background material rather than changing the scene view's background color property. – stephent Sep 18 '19 at 21:43