Chapters

Hide chapters

Metal by Tutorials

Second Edition · iOS 13 · Swift 5.1 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Section I: The Player

Section 1: 8 chapters
Show chapters Hide chapters

Section III: The Effects

Section 3: 10 chapters
Show chapters Hide chapters

22. Integrating with SpriteKit & SceneKit
Written by Caroline Begbie & Marius Horga

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Now that you have mastery over rendering, you can put your knowledge to use in other APIs.

SceneKit, SpriteKit and Core Image all have some integration with Metal shaders and Metal rendering.

There may be times when you don’t want to write a full-blown 3D Metal app, but you want to take advantage of SceneKit. Or perhaps all you want is a 2D layer in your Metal app — for that, you can use SpriteKit. And even though you have less control over your final output with SceneKit and SpriteKit, you can still incorporate shaders to give your games a unique look.

In this chapter, you’ll have a look at a collection of APIs that integrate with Metal. You’ll first create a toon outline and cel shader for the jet in Apple’s SceneKit template.

After that, you’ll open a scene similar to the game scene from Chapter 9, “Scene Graph.” and add to it a 2D overlay that feeds information to the player.

By creating this overlay, you’ll learn how to render a SpriteKit scene during each Metal frame rendering.

Finally, you’ll create a Core Image Metal kernel to do the blending of the SpriteKit overlay and the 3D rendered scene.

SceneKit starter project

Before creating the toon shader, you’ll first learn how to create your own custom shaders in SceneKit, and then find out how to pass data to the shaders.

Create a new project using the macOS Game template (or you can choose the iOS Game template if you prefer). Use the Product Name of Toon-SceneKit and make sure that SceneKit is specified in the Game Technology dropdown.

Note: Alternatively, you can choose to open the starter project for this chapter instead.

Build and run, and you’ll see Apple’s default game template jet animating on the screen. You can turn the jet by dragging.

This chapter assumes you have some familiarity with SceneKit, but if not, you can get the general idea of how SceneKit works by reading through GameViewController.swift, which has extensive comments.

Just like the game engine you’ve worked on throughout this book, each element is a node. You have geometry nodes such as the jet, light nodes and a camera node. These nodes are placed into a scene, and you attach the scene to a view. To animate the nodes, you run actions on them.

Open GameViewController.swift, and in viewDidLoad(), replace the rotation animation:

// animate the 3d object
ship.runAction(
  SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 2, z: 0, 
                          duration: 1)))

With:

ship.eulerAngles = SCNVector3(1, 0.7, 0.9)

This gives you a better angle to see your toon shading.

Next, configure a different background color. Change:

scnView.backgroundColor = NSColor.black

To:

scene.background.contents = nil
scnView.backgroundColor = NSColor(calibratedWhite: 0.9, 
                                  alpha: 1.0)

This removes the procedural sky that ship.scn created, and replaces it with a light gray background.

SceneKit shaders

To run a Metal shader on a node, you create an SCNProgram object and attach it to the node.

Match SceneKit names

When you’re using Metal with SceneKit, you’ll find that parameter names must match what SceneKit is expecting. Here, in the vertex function, SceneKit expects the Uniforms buffer to be named scn_node. When you give a parameter the name scn_node, you can name the struct Uniforms any name, and give the buffer index any index, and SceneKit will recognize the buffer as being the uniform values.

Send a texture to the fragment shader

In Shaders.metal, add these attributes to VertexIn:

Sending constant data to shaders

You’ve rendered a textured, unlit model, but if you want to do lighting, then you have to set it up yourself, with the usual light positions, model normals and lighting calculations.

Toon shading

A full toon shader consists of an edge detection algorithm and a cel shader where you reduce the color palette. Generally, shading is a gradient from light to dark, but the shading in this rocket demonstrates cel shading where abrupt steps in the gradient occur:

The fwidth function

In fragment shaders, you have access to the current fragment, but you also have access to the change in slope of the fragment from neighboring fragments. Fragments pass through the rasterizer in a 2 x 2 arrangement, and the partial derivatives of each fragment can be derived from the other fragment in the group of four.

Cel shading

When you render non-photorealistic toons, you generally use a minimal color range. Instead of having a smooth gradient for shading, you use a stepped flat color gradient.

SpriteKit rendering in Metal

Note: As of the time of writing, CIContext.render hangs the app when using Xcode 11 or macOS Catalina 10.15. However, you can still do the rest of this chapter using Xcode 10 on macOS Mojave 10.14.

Create the HUD in SpriteKit

Create a new SpriteKit scene using the SpriteKit Scene template, and name it Hud.sks.

SKRenderer

Generally, when you create a SpriteKit app, you hook up the SKScene with an SKView. The SKView takes care of all the rendering and places the scene onto the view. SKRenderer takes the place of SKView, allowing you to control updating and rendering of your SpriteKit scene.

Post-processing

Create a new Swift file named PostProcess.swift, and replace the code with:

Core Image

So far in this chapter, you’ve used Metal with SpriteKit and SceneKit. There’s one other framework whose shaders you can replace with your own custom Metal shaders: Core Image.

Core Image Metal kernels

There are a few pre-defined kernel types you can use with Core Image:

Create a Metal library for Core Image

Create a new file using the Metal template named CIBlend.metal. Include the Core Image headers:

Pre-multiplied alpha blending

In this example, the HUD texture has anti-aliasing, where the texture is partially transparent around the edge of the letters:

Compiling and linking a Core Image kernel

You can build the kernel to check that it has no syntactical errors, however, because it’s wrapped up in the Core Image namespace, it won’t be available at runtime from your default Metal library.

Challenge

Currently, you’re printing out “updating HUD” in the debug console. This print comes from update(_:) in the SpriteKit scene class Hud. Your challenge is to update the HUD, keeping track of the oil cans that the player has collected. To achieve this, in GameScene, on colliding with an oil can, you’ll tell HudNode to update Hud using oilcanCount. As always, you’ll find a solution in the challenge directory for this project.

Where to go from here?

In this chapter, you created two simple examples that you can experiment with further. Fragment shaders, and to a lesser extent vertex shaders, are endlessly fascinating.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now