Chapter 11. Scene Kit

Scene Kit is a simple framework for creating, configuring, and rendering a 3D scene in real time. Scene Kit’s designed for all kinds of 3D rendering situations, ranging from visualization of data to games. If you want to make a 3D game but don’t want to spend ages learning how to deal with low-level OpenGL or Metal code, Scene Kit’s a great way to get started.

Setting Up for Scene Kit

Problem

You want to set up your game to use Scene Kit for rendering graphics.

Solution

Open your application’s storyboard, and locate the view controller that you want to use to present the 3D content in your game.

Select the view, and go to the Identity inspector. Change the class to SCNView.

Next, go to your view controller’s source code. At the top of the file, you need to import the SceneKit module:

import SceneKit

The simplest way to ensure that Scene Kit is working properly is to make your SCNView show a background color. In the implementation of viewDidLoad, add the following code:

let sceneView = self.view as! SCNView
sceneView.backgroundColor = UIColor(white: 0.6, alpha: 1.0)

Discussion

An SCNView is responsible for drawing 3D content in your game. While you don’t often work directly with the view, it’s a necessary first step to make sure that you’ve got a view and that it’s working. Once you have it, you can move on to bigger and brighter things.

Creating a Scene Kit Scene

Problem

You want to manage your 3D objects by grouping them into scenes.

Solution

Once you have an SCNView to render ...

Get iOS Swift Game Development Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.