I’ve been looking forward to the Apple WWDC 2014 for so long. And as expected a huge bunch of new features, API’s, tools and frameworks was presented to all of us developers. As a Sprite Kit lover I was very curious about what new methods they will provide to us. And what should I say, a dream come true… Sprite Kit with lighting and shadow effects! So this is a little tutorial how to set up a Sprite Kit Scene with the new beta of iOS 8 and the SKLightNode. How to make gorgeous shadows and BumpMapping effects that they call normalMapped. Keep in mind that this only works with the new version of Xcode 6 that is now available for apple developers as a beta in the apple developers center. Also remember that the new functionalities of Sprite Kit are also very beta and at this level are not claimed to work reliably or performantly. The source with this demo project can be downloaded here from GitHub.
Setup a new Sprite Kit project
First we setup a basic Sprite Kit project with Xcode 6. Let’s use Objective C instead of Swift. The announcement of Swift is only a few days old After this step get rid of the default stuff. I’ll throw the spaceship and the hello world text away. We can also delete the touchesBegan method and additionally I will setup correct scene dimensions as described in a former blog post. So everything is cleaned up to init some sprite nodes.
Add some sprites
First let’s make a background. A sprite that has the size of the entire screen. The image is optimized for 4:3 iPad.
So far there is no light source or shadows in this scene. But now let’s add some new light functionalities from iOS 8. A light is only another node called SKLightNode. We will init a SKLightNode and add it as a child to the fire emitter, so that it seems the fire is our light source:
Make a BumpMapping like effect
To add this dynamic bumpmap effect you can easily assign the categoryLightMask to the background like we’ve done before with the shadow:
The new lighting node in iOS8 is a great new feature to give a quality boost to every 2D game made with Sprite Kit. It’s super easy to use. This first beta version kind of buggy. The normalMap isn’t rendered on a real device in my case, only in the simulator, and the performance decrease is very strong. It also seems the falloff attribute has no effect and the shadow is only rendering the bounding square of a sprite, not processing the alpha values. So a circle image still drops a box shadow …but let’s see how upcoming versions will fix all this issues.
I’m looking forward to your comments or requests for new articles.