specifying scene's background color in SceneKit

7.4k views Asked by At

I'm trying to specify a background color (which is currently black) of a screen just before the scene appears. I searched documentation and this is what I found:

var background: SCNMaterialProperty { get }

A background to be rendered before the rest of the scene. (read-only) If the material property’s contents object is nil, SceneKit does not draw any background before drawing the rest of the scene. (If the scene is presented in an SCNView instance, the view’s background color is visible behind the contents of the scene.)

So I went and sent the contents to some color.

scene.background.contents = UIColor.redColor()

However, the moment before the scene renders, the screen(SCNView portion of it) is still black. What am I doing wrong? Perhaps the background property is not what I need? Also I'm not quite sure if a can drill further down some property which is only get(bacground is a getter), and than set some stuff like I did in code example. Thanks for answering

1

There are 1 answers

4
Sylvan On

Try setting the backgroundColor property of your SCNView instance. In my code, I have the following: (some of the code may not apply to your situation, but it should give you the general idea.)

if let scene = SCNScene(named: "myScene.scn") {
        ...
  // retrieve the SCNView
  let scnView = self.view as! SCNView

  // set the scene to the view
  scnView.scene = scene

  // configure the view
  scnView.backgroundColor = UIColor.redColor()
}

Some of that was generated from Apple's template for a SceneKit game. The last line is the important one. I should mention I'm using Xcode 7 beta with Swift 2, in case there are some subtle differences.

Edit: Seems I misinterpreted the question a bit. To change the background color of the view just before the scene is loaded, click on the Main.storyboard, and in Interface Builder, click the View property inside the View Controller. Then simply set the Background color property in the Inspector tab.