iOS - Combining SpriteKit and Metal - ios

Is it possible to combine SpriteKit with Metal? and if it is, how could one achieve to combine metal particles and SKNodes in a physics world so that the collide with each other, what's the usual approach for this kind of requirement.
Thanks

They are two totally different technologies. Sprite Kit is a framework that abstracts all of the rendering work for you as well as provides you with a built-in physics engine. Whereas Metal is purely a low-level GPU-accelerated graphics API which gives you complete control over the rendering process. It is similar to OpenGL ES but with much less overhead.
Sprite Kit will use Metal (on eligible devices) to render your scene. You don't need to do a single thing because Sprite Kit handles all rendering behind-the-scenes.
You don't combine them, they are two totally different frameworks. If you are looking to add physics to Metal then you will either need to write your own physics engine or use an an already existing engine like Box2D (which I believe Sprite Kit uses internally).

This appears to be possible now using SKRenderer which allows you to mix SpriteKit and Metal (by the looks of it adding SpriteKit to Metal and vice versa).
It's iOS 11+, macOS 10.13+ and tvOS 11+.

Related

Sprite Kit vs Scene Kit for Building information modelling

I am trying to develop a building information model viewer for iPad and I am faced with the current challenge. Should I use SpriteKit or SceneKit? I know SceneKit is meant for rendering 3D while SpriteKit is 2D. From my research so far, SceneKit seems more appropriate for Building Information Modelling as it will represent a 3D Model of a building. However, I would like to know if I can do it with SpriteKit (I read SpriteKit is easier to learn) or should I used SceneKit? Thanks for your input. I am new to iOS dev, so any assistance would be helpful.
SceneKit and SpriteKit are very similar to each other. SceneKit is a little harder to learn but it's pretty simple. SceneKit would be the only way to have a 3D model(With the options you provided). You can have a SpriteKit scene over top of the SceneKit scene to display labels that stay put.
You'll probably want to use both of them. SceneKit represents 3D very nicely, but can also accept SpriteKit scenes to use for backgrounds, foreground overlays, and object textures/materials.
You can assume SpriteKit is at Top of the SceneKit, As using SceneKit you can add 3D models into Augmented Reality while SpriteKit is used to add extra sprites onto the model.
In short SpriteKit is revolution in Gaming.

Character Animation with Metal

I have just delved into the world of Metal, and I thought that I'd got the hang of it! But then it occurred to me that if I wanted to make a game, then static objects moving around a screen wouldn't suffice. So my question is, 'Is it possible to create animations for models with Metal?'
I have looked at using other APIs, such as SpriteKit, and SceneKit, but I found that they do not support shaders, and are not as powerful as Metal.
The only way that I can think about how I would go about this, is by creating 60 different models, and then loading each one one after the other, to give a 'stop-motion' kind of effect, but I think that this would probably be incredibly inefficient, and was hoping that there was an easier answer?
Thanks a lot!
Yes, there are other, more efficient ways to do animation. But before getting into that, a warning: it really looks like you're barking up the wrong tree here.
Metal is a (conceptually) very low-level interface. You use Metal to talk (almost) directly to the GPU, so to work with it you need to think (sort of) like a GPU: in terms of data buffers, vertex transformations, etc. You seem to be working at a much higher conceptual level, so you're probably better served by one of the high-level game engines: SpriteKit for 2D or SceneKit for 3D. (Or a third party engine like Cocos or Unity.) Metal, on the other hand, is better suited for building those game engines.
SpriteKit and SceneKit do support shaders. Look at SKShader and SCNShadable in the docs (and be sure to click the "More" links to read the full overviews). SceneKit also supports character animations (aka skeletal animation aka skinning): typically one designs and rigs a model for animation in an external authoring tool (Maya, Blender, etc), then uses SceneKit to work with the animations at run time.
It is possible to do things like GPU-based skeletal animation in Metal. But I haven't seen any tutorials or similar written about it yet, probably because Metal is such a new technology. Fundamentally, though, it'd be based on the same sorts of techniques you'd use for skeletal animation in OpenGL or Direct3D — and much has been written about animation for those technologies. If you're willing to invest the time and energy to work at a low level, adapting the subject matter from GL/D3D tutorials is relatively easy.
You can do skeletal animation in Metal, SCNKit would be using the GPU to deform the mesh as well. But to do it in Metal you would need to pass skin weights, along with bone matrices for the bind pose and the transformations of the bones as they animate then calculate the new vertex positions based on these. In fact I think you need the inverse of the bind pose matrices. Each mesh vertex is then transformed by a weighted sum of transformations dictated by the skin weights.
I tried it but screwed it up somehow it didn’t deform properly, I don’t know if I’d obtained the wrong matrices from my custom script to grab animation data from blender or a bug in my shader maths or from the weights.
It was probably close, but with all the possible things that I may have got wrong in the process it was difficult to fix so I abandoned it in the end.
Probably easier to stick with SceneKit and let apple take care of the rest or use an existing game engine such as Unity.
Then again if you want a challenge, I’m sure it’s possible, just a little tricky. You could try CPU first to make sure the maths is ok then port it to the GPU to make it faster?
SceneKit do support shaders. And an object that manages the relationship between skeletal animations and the nodes and geometries they animate is SCNSkinner from SceneKit.
Typically, you need to create a skinned model using, for example, Autodesk Maya, save it along with animations that use the skeleton, in a scene file. You load the model from the scene file and pose or animate it in your app, either by using animation objects also loaded from the scene file or by directly manipulating the nodes in the skeleton. That's it.
Watch this 7-parts video about Blender's skeletal system and how to use it in SceneKit.
convenience init(baseGeometry: SCNGeometry?, //character
bones: [SCNNode], //array of bones
boneInverseBindTransforms: [NSValue]?, //ibt of matrix4
boneWeights: SCNGeometrySource, //influence on geometry
boneIndices: SCNGeometrySource //index mapping
)

Developing Shaders With SpriteKit

I've read that some of the downfalls of SpriteKit is that you're unable to develop shaders if you use it.
However, I read a post here on SO that suggest otherwise:
How to apply full-screen SKEffectNode for post-processing in SpriteKit
Can you develop your own shaders if you decide to use SpriteKit?
Thanks
It is not supported in iOS 7, but iOS 8 will support custom shaders. For more information, view the pre-release documentation of SKShader.
An SKShader object holds a custom OpenGL ES fragment shader. Shader objects are used to customize the drawing behavior of many different kinds of nodes in Sprite Kit.
Sprite Kit does not provide an interface for using custom OpenGL shaders. The SKEffectNode class lets you use Core Image filters to post-process parts of a Sprite Kit scene, though. Core Image provides a number of built-in filters that might do some of what you're after, and on OS X you can create custom filter kernels using a language similar to GLSL.

OpenGL vs Cocos2d: What to choose?

I understand that cocos2d it's really simple API, and that I can use it to do simple and huge 2D or even sometimes 3D games/applications. As well I understand that OpenGL it's more complicated, it's lower level API etc.
Question: What is better for implementing 2D/3D games? Why do we need to learn OpenGL if we have simple frameworks like cocos2d? What you can do with OpenGL that you can't do with cocos2d?
Thanks in advance!
What is better for implementing 2D/3D games?
Hard to tell, but a higher level API is always there to make things easier for you. For example you are writing a 2D shootem up. You will likely use a game loop, you will want to use sprites and make those move on the screen. You may want animations like explosions taking place. You'll end up writing your own higher level API to do those things. Cocos2D has solved those problems for you already. Any other frameworld should have solved it.
Why do we need to learn OpenGL if we have simple frameworks like cocos2d?
In case you like to cusomize the standard behaviour of a framework, especially the drawing part, you should get into openGL. If there is something you like to have which doesn't come out of the box you may find yourself reimplementing a base framework class. For example, look at the shaders used in Cocos2D 2.0. If you like some special blending mode, like a tinting effect, you won't get it for free. There is a colour attribute for a CCSprite but this may not be the result you're expecting. So you'll have to write your own shader and plug it into the sprite you like to be displayed in a different way.
What you can do with OpenGL that you can't do with cocos2d?
This comparison doesn't really work out, since cocos2d facilitates opengGL for the drawing part to build up that higher level api and make your life easier as a game developer.
Cocos2d is a wrapper around the 2D features of OpenGL (as of this: http://www.cocos2d-iphone.org/about) . Under the hood it itself uses OpenGL ES to implement its features. This is good because it means that there will be minimal performance overhead so you can start using its simpler API without having to get immersed to the definitely bigger learning path of OpenGL.
It has however only strong 2D support and if you plan to write later 3d games you loose all benefits of Cocos2d: why would you rewrite a 3d rendering engine with a 2d framework that under the hood uses a very strong 3d engine? You loose performance for a lot of unnecessary work.
So the simpler answer is: for 2d Cocos2d, for 3d OpenGL.
If you want to start OpenGL ES, this is a very good tutorial for beginners: http://iphonedevelopment.blogspot.it/2009/05/opengl-es-from-ground-up-table-of.html

iOS: image drawing performance

I have several fundamental questions to clearify to myself, hope you'll help...
I'm developing a game application. It has game loop and on each iteration i draw about 200 sprites. What is the fastest way of drawing bitmap? Currently I use [UIImage drawAtPoint] method to draw a sprite (sprite sizes can be 16x16 ... 64x64), but something tells me, this is not the best approach. As I understand, UIImage is based on Quartz technology? Can cocos2d provide the better performance?
I also thought about using OpenGL ES to reproduce drawing mechanism via textured objects.
If you're making a game you'll probably want to avoid Quartz/UIKit/CoreAnimation. They're designed to be simple and easy to use, at the cost of performance.
Opengl is the fastest way to draw sprites to the screen on the iPhone. There are two versions of Opengl in iOS: OpenglES 1.1 and OpenglES 2.0. If you're looking to support the iphone 3G and earlier, you'll need to either use 1.1 exclusively, or be able to determine the device you're running on and switch versions on the fly. Opengl is very fast but at the cost of a fairly steep learning curve and a lot of what you'd see as boilerplate code.
Cocos2d is an iOS game engine built on top of Opengl. It's designed to make using opengl easier (among many other things). If you're making a game, I'd suggest you use cocos2d as they've done a lot of the hard work already. If you don't you'll be spending 99% of your time getting opengl to work, rather than focusing on the game.
Good luck! :)

Resources