Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I overlay a SKScene over a SCNScene in Swift?

There are some overlay tutorials on the Internet, all using overlaySKScene at some point.

That is somehow not possible in my project, as (I guess) my view does not use the constructor of SCNView at any point (which overlaySKScene is a part of).

In the ViewController's viewDidLoad, basically only the MainScene is created:

viewDidLoad() {
self.sceneView = MainScene(view: self.view)) }

...which goes here (note: SCNScene instead of SCNView):

class MainScene: SCNScene, SCNPhysicsContactDelegate {...
init(view:UIView) {
scnView = view as! SCNView
super.init()
scnView.scene = self; (...) }

The scene is perfectly created, I would now like to overlay a SKScene. Does anyone know how?

like image 868
ducktalez Avatar asked Jan 04 '16 03:01

ducktalez


1 Answers

The SpriteKit overlay goes on the SceneKit view, not on the SceneKit scene. This is a bit confusing because you're overlaying a scene on a view.

I see several possible error sources:

self.sceneView = MainScene(view: self.view)) 

as defined returns an SCNScene. You're assigning that to a property that expects an SCNView.

The line

scnView = view as! SCNView

will crash unless view returns a properly connected SCNView instance. But the init definition you've written expects a UIView.

Somewhere, you need to have your view be an SCNView. That view, because it conforms to protocol SCNSceneRenderer, will have an overlaySKScene property on it (overlaySKScene comes from that protocol, not from SCNView). That's where you can assign your SKScene instance.

If you have done that, then your code would look something like

scnView.scene = self 
scnView.overlaySKScene = theSKScene

I have a simple example of an SKScene overlaid on an SCNView at https://github.com/halmueller/ImmersiveInterfaces/tree/master/Tracking%20Overlay

See also How do I create a HUD on top of my Scenekit.scene.

like image 74
Hal Mueller Avatar answered Sep 19 '22 16:09

Hal Mueller