Chapter 8. SceneKit
SceneKit is a simple framework for creating, configuring, and rendering a 3D scene in real time. SceneKit’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, SceneKit’s a great way to get started.
8.1 Setting Up for SceneKit
Problem
You want to set up your game to use SceneKit 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
SpriteKit
The simplest way to ensure that SceneKit 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.
8.2 Creating a SceneKit Scene
Problem
You want to manage your 3D objects by grouping them into scenes.
Solution ...
Get iOS Swift Game Development Cookbook, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.