5

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

potato
  • 4,479
  • 7
  • 42
  • 99

1 Answers1

5

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.

Sylvan
  • 526
  • 3
  • 6
  • Thanks for answering. My problem is not about setting the scnView background color. Run your simulation, and riiiiight before the scene(with scnView background color properly set) appears, there is a moment with visible UIView(part of my screen is UIView, the other part is SCNView) but scnView totally black. – potato Jun 28 '15 at 07:51
  • Oh, I see. For that, click on your Main.storyboard, click in the View (inside Game View Controller) and set the Background property to your color in the Inspector tab. – Sylvan Jun 28 '15 at 19:14
  • Nono, my View's color is yellow as I wanted it to be(Im not using Game View Controller). This is the sequence of events: yellow View appears(lower part of my screen) and the SCNView is black, after that the SCNView renders it contents. – potato Jun 28 '15 at 19:29
  • I don't see what you're seeing, because my scene covers the whole screen. I never see that intermediate "black" screen. Good luck! – Sylvan Jun 28 '15 at 19:40