SKScene displayed as a SCNMaterial

Hi, I just wanted to display a SpriteKit Scene in a SCNPlane. So I set the the SCNMaterial contents to my SKScene, but instead of getting the scene I'm getting a grey plane. This is my code by the way:

var mainScene: SKScene {
    let scene = Game()
    scene.size = CGSize(width: 1024, height: 1024)
    scene.scaleMode = .resizeFill
    scene.backgroundColor = .purple
    scene.view?.backgroundColor = .purple
    scene.view?.allowsTransparency = false
    return scene
}



func initMainScene() -> SceneView {

    mainScene.view?.isPaused = false

    let scene = SCNScene()

    let mainSceneMaterial = SCNMaterial()
    mainSceneMaterial.normal.contents = mainScene
    mainSceneMaterial.isDoubleSided = true

    let planeGeometry = SCNPlane(width: 1, height: 1)
    planeGeometry.materials = [mainSceneMaterial]

    let plane: SCNNode = SCNNode(geometry: planeGeometry)

    let camera: SCNNode = SCNNode()
    camera.name = "Camera"
    camera.camera = SCNCamera()
    camera.position = SCNVector3(x: 0.0, y: 0.0, z: 4.0)

    let light: SCNNode = SCNNode()
    light.light =  SCNLight()
    light.light!.type = .omni
    light.position = SCNVector3(x: 1.5, y: 1.5, z: 1.5)

    scene.rootNode.addChildNode(camera)
    scene.rootNode.addChildNode(light)
    scene.rootNode.addChildNode(plane)

    return SceneView(
        scene: scene,
        pointOfView: scene.rootNode.childNode(withName: "Camera", recursively: false),
        options: []

    )
}

Here is the screenshot:

Also, my SpriteKit scene has touchesBegan and touchesMoved functions implemented, will those events still work if I embed the scene in the SCNMaterial?

Thanks very much 🙏

  • I have achieved to render the SKScene as the material of the SCNPlane, but the didMove function isn't being called, and if I set the material to a SKView, give it a frame size, and call presentScene passing my scene as the argument, the app crashes 😓

Add a Comment

Replies

Hello,

It sounds like maybe you have found a solution to your original issue, but if not, I believe the problem is here:

    mainSceneMaterial.normal.contents = mainScene

Most likely you meant to set the diffuse.contents, not the normal.contents.

  • Hi! I really appreciate your help! I've just tested this, but it seems that that's not the only problem 😓 The didMove function within SKScene isn't being called when applying the scene to a SCNPlane material, so I moved the self.addChild(myNodesAndStuff) to the sceneDidLoad function, and my nodes appeared on the material! But as soon as my code called the frame size of the scene the app crashed, and that's the limitation I'm facing now (before that I tried to set the material to a SKView, give it a frame size, and call presentScene passing my scene as the argument and that also crashed) I don't really know if that's a bug or something documented (I'm fairly new to SpriteKit and SceneKit to be honest). Thank you very much for your help 💪

Add a Comment