SceneKit On iOS 8: Know The Basics

By | April 23, 2015
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

SceneKit has emerged as a strong and more user-friendly alternative to OpenGL, for 3D graphics rendering and visually customizing mobile games. This Apple tool was initially available only on the OS X platform, and was made available on iOS 8 in 2014.

 

Apple had first brought SceneKit – a powerful, multi-featured 3D graphics API tool – way back in 2012, for Mac OS X 10.8. Last year, SceneKit was introduced on the iOS 8 platform, much to the delight of mobile game developers. Thanks to the tool, developers no longer have to rely on the complex OpenGL system, to create immersive layouts and graphic content. The broad similarities between SceneKit and the Cocoa framework have also contributed to the former’s rising popularity as an efficient 3D rendering tool. We will here be discussing upon some pertinent factors related to SceneKit on iOS 8:

 

  1. Compatibility with other frameworks – SceneKit can be used by app and game developers in collaboration with other popular Apple frameworks, right from SpriteKit, to Core Image and Core Animation. This feature, in fact, makes it very easy for those experienced in working with Cocoa and/or Cocoa Touch to learn up SceneKit and start using it to create high-quality visualizations and layouts.
  2. Method of creating and managing Scenes – Xcode 6 comes with an in-built game project template, in place of the OpenGL Game template present in its previous version. To create a custom game scene, iOS game development experts have to create ‘Nodes’ first (after, of course, launching SceneKit and the template), and then, arrange them in a three-dimensional environment. Child nodes can be defined in relation to their respective parent nodes. Surface rendering is done by adding the available geometrics of the tool, and while configuring, the color and the texture can also be tweaked. Need more illumination in a Scene? All that you have to do is include a light to any of the nodes.
  3. Starting out with SceneKit – Okay, now that you have an idea of how SceneKit works in theory, let’s actually start off with the tool. After creating a new project in Xcode 6 (get the latest version), click on iOS → Application → Game and hit ‘Next’. A box would appear, prompting you to enter the Product Name, Language and Game Technology respectively. Choose ‘Swift’ and ‘SceneKit’ for the second and third fields respectively. That’s it, you are now ready to add attributes and proceed with your game.
  4. Animation support – SceneKit has the potential to become an absolute delight for professional/indie game animators. Once again, the familiarity with the Cocoa Touch framework is a factor (although the quality of SceneKit animations is way better). The API for action animation in SpriteKit is available in SceneKit too, enabling coders to create animation sequences and loops. For animating an object property, a CAAnimation method has to be declared for the target key path (for instance, pos1.x). Inside the SCNTransaction, the value between ‘begin’ and ‘commit’ can be altered as well. It won’t be far off to say, what SpriteKit is for 2D animation, SceneKit is for 3D animation.
  5. Available attributes in SceneKit – Game developers who have worked with SpriteKit would know how subclasses have to be used to render game elements on the device screens. In SceneKit, attributes have to be worked with for the same purpose. There are three key attributes of the 3D graphics tool: SCNCamera (for overlaying a camera on a node for rendering a scene, just like using the pointOfView property), SCN Geometry (for creation of geometrical shapes and figures, for personalizing the game surface), and SCNLight (with four types of light options, for ambient, directional, omnidirectional and spot lights). These attributes have to be attached to the Nodes, for rendering. Note that nothing can be rendered only by the Nodes.
  6. Managing the node hierarchy – In order to avoid confusions, SceneKit on iOS 8 and OS X 10.10 has separate methods that can be used. Scale, position and rotation are the three main features of any node in this tool – and their hierarchy is managed by using insertChildNode(_:atIndex: ), addChildNode(_:) and removefromParentNode(). All the child node hierarchies are managed relative to their respective parent nodes.
  7. Creating a dummy Scene – How about actually trying our hands at creating a Scene with SceneKit now? Create a blank SCNScene (which has root node features), and define any geometry instance inside it (SCNSphere for a sphere, SCNBox for a box, and so on). Make sure to attach these instances to SCNNode, and the latter is properly attached to the root node of the project. Next up, attach the Scene you have just made to sceneView, type sceneSetup(), and build and run. Your dummy Scene is ready!
  8. Geometry instances in SceneKit – By now, you must be wondering about the types of geometry instances you can actually add to Scenes? SceneKit comes up trumps on this count, with its extended support for a vast range of instances in the SCNGeometry class. Apart from the boxes and spheres mentioned above, iPhone app and game developers can define cones, cylinders, pyramids, tubes, planes and capsule shapes on a scene. Even without using external 3D models, ‘Primitives’ can be applied to draw up certain shapes/figures. The SCNText object is used for adding text strings in SceneKit, which are later rendered in 2D.
  9. Adding Materials to surfaces – iOS game developers and app makers do not have much to code manually, for working with Materials (inside the SCNMaterial) class in SceneKit. The line ‘material = SCNMaterial.Create ()’initiates a material, following which diffuse contents (e.g., animal figures) can be defined in it. Since the Materials respond differently to the various types of Lights, they are ideal for making mobile game animations more personalized. Reflective contents are supported in SceneKit Materials too.
  10. Asset Catalogs in SceneKit – On iOS 8, SceneKit brings in an extremely handy functionality to manage game assets and maintain the hierarchy of objects – the Asset Catalogs. Developers only have to store all the assets inside the specified folder (it has a .scnassets extension), and it will automatically be copied at run-time by Xcode 6. Consistency is a big advantage of this method, and graphic designers/animators can categorize different assets (textures, models, particles, etc.) in separate folders. Levelwise organization of game assets is also possible.
  11. Supported programming languages – App developers have the freedom to work with either Swift or Objective-C in the different attributes of SceneKit on iOS 8. The tool resides hierarchically on top of OpenGL, and ensures superior, quicker, better graphics creation, customization and rendering properties. For adding custom special effects like field depth or dynamic shadows, the advanced configurations of SceneKit are perfect. There is even the option to use GLSL and perform the rendering there (it will be a move downwards).
  12. SceneKit View in Storyboard – Experts in the domain of mobile game development are probably already aware of using Swift code files in the latest iteration of Xcode. In addition to the regular view controller (attached to ViewController.swift) in Main.Storyboard, there is another high-utility component called SceneKit View present at the same location. From the Interface Builder’s Object Library, SceneKit View renders 3D graphics quickly and accurately. What’s more – it also helps in defining key attributes with precision. The rendering function of SceneKit View is roughly similar to that of GLKitView.
  13. Input mechanism handling in SceneKit – Once again, iOS game developers who have worked with Cocoa Touch will find the input handling methods in SceneKit somewhat identical. All types of keyboard and touch events, and gesture recognition are handled via the same mechanism. The only significant difference in SceneKit in this regard is that a single view is present for managing all inputs – the Scene View. In order to perform a hit test on the Scene View, -hitTest(_:options: ) has to be used. Every time a camera ray hits a geometric object, a series of hit test results are generated. These results are instrumental for handling inputs like screen taps and pans.
  14. Support for Alembic and COLLADA – In SceneKit, including videos in mobile games is easy – thanks to the built-in support for Alembic files ( .abc) of the tool. 3D animations and geometries can be loaded quickly and immersively with Alembic. Prior to proceeding further, you can check the 3D files with QuickLook (by using Finder), or directly via the Preview option. Yet another point of convenience of coders involved in the creation of iPhone games is the built-in support of SceneKit for the COLLADA editor. The latter refers to XML-styled files that have the extension .dae, and are helpful for inspecting the 3D elements in a game inside Xcode.
  15. Using SCNTechnique – SceneKit goes one up on OpenGL, thanks to the additional functionality that the SCNTechnique class offers. With it, game and app developers can create deferred shading effects and such other complex animations/special effects (not possible with OpenGL). The first pass of SceneKit defines the depth, while the following focuses on the normals. In the built-in dictionary of SCNTechnique, all the shader files, drawing passes, symbols and figures are defined.

The overall hierarchy of SceneKit has Scene right at the top, with the Root Node below it, and all the attributes are placed as Child Nodes. If you use ambient lighting, it is advisable to use omnidirectional Lights with it (since the former, on its own, does not have powerful illuminating properties). Creating a Scene in SceneKit (SCNScene) is, in essence, the same as using SKScene in SpriteKit. SceneKit has received the thumbs-up from game developers worldwide, and the tool is expected to get even better over time.

 

Have you used SceneKit?

 

Hussain Fakhruddin
Follow me

Hussain Fakhruddin

Hussain Fakhruddin is the founder/CEO of Teknowledge mobile apps company. He heads a large team of app developers, and has overseen the creation of nearly 600 applications. Apart from app development, his interests include reading, traveling and online blogging.
Hussain Fakhruddin
Follow me
 

Leave a Reply

Your email address will not be published. Required fields are marked *