Saturday, November 2, 2013

First thoughts on Sprite Kit

Today,  I have decided to give Sprite Kit a try on iOS.  I have been disenchanted with the amount of setup needed for my little projects/prototypes and wanted something extremely simple to use.  Sprite Kit delivers.

First, Sprite Kit is a 2D scene graph.  You add nodes and transform them within a hierarchy.  There is a nice way to specify actions such as blinking lights or other little details atop a sprite.

My question was - how viable is this performance-wise.  After a few tests in displaying a large 512x512 tile map, here are my results:
  1. Sprite Kit may be able to manage draw calls, but it works best when you manage the nodes.  For large enough levels, nodes will have to be swapped in and out.  Too many nodes and Sprite Kit will spend too much time processing them all.
  2. Create a cache of nodes and recycle them -- the bottleneck was the allocation and deallocation. Even with a solid color SKSpriteNode, CPU usage would hit 90% when scrolling the map!
A few other notes after playing with Sprite Kit a bit more:
  1. Render to texture effects are achieved through the core image filter system as far as I can tell.  This layout makes a feedback loop a bit challenging.  They are nodes in a tree, and previous results are not in the tree.
  2. The particle system is easy to grow out of.  I tried to use it, but found that changing the velocity of all particles wasn't easy.  Rendering each particle as a sprite was surprisingly efficient.
  3. Blend modes are particularly easy to set up.  They have similar names and behaviours to those in Photoshop - making for a nice way to quickly preview the result.  Note - much fewer blend modes in Sprite kit (normal, add, replace, screen)
  4. Something that deserves more attention; the ability to effortlessly replace a solid colour box with an image should not be under-rated.  Why?  Have an idea for a sprite?  Give it the behaviour you want using solid colour boxes and see if it is fun before investing time making it look pretty.  This should push people to try out many behaviours before committing to anything.
There are a few caveats.  Oft when I think of some concept with a visual representation, Sprite Kit is incapable of doing what I wish (or would not do it efficiently), namely:
  1. Complex / warping geometry would be beneficial.  If some instances, rather than encode a dozen images certain effects can be done through manipulation of geometry.  Suppose you had a circle and wanted it to grow spikes.  Suppose even a rope - you would be limited to build the rope out of rectangular segments.
  2. Custom shaders can't be overlooked.  So many visual effects can be more readily achieved with them.  That, and multi pass techniques could add so much visually.  Worse, I do not see a clear step outside of the confines of Sprite Kit if ever these effects become desirable.  This may change in future iOS releases.  Hopefully it does.
I believe it is a great step forward in ease-of-creation for games and other multimedia apps.  Unfortunately, I wish I could use custom effects (shaders) to provide more interesting visuals.

After writing this, custom GLSL shaders are supported.  Still, I keep on being over-ambitious with the API.