11

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?

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
ducktalez
  • 115
  • 1
  • 6

1 Answers1

12

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.

Community
  • 1
  • 1
Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • Thanks for the great example!! But how to add more than one overlay into scene? In the provided sample only one overlay is added. How to reuse the same InformationOverlayScene provided in the provided example(https://github.com/halmueller/ImmersiveInterfaces/tree/master/Tracking%20Overlay) for multiple texts and for multiple nodes? – Vidhya Sri Sep 19 '17 at 10:24
  • 1
    The overlay scene can be as complex as you like. If you think you need multiple scenes, that deserves its own question. – Hal Mueller Sep 20 '17 at 01:07