5

I have 2 SCNViews next to each other and both should show the same scene but through different cameras. It seems to me that Scene Kit uses that node with a camera that is highest in the node hierarchy so I tried something like that

    leftSceneView.scene?.rootNode.addChildNode(scene.rootNode)
    rightSceneView.scene?.rootNode.addChildNode(scene.rootNode)

    leftSceneView.scene?.rootNode.addChildNode(cameraNodeLeft)
    rightSceneView.scene?.rootNode.addChildNode(cameraNodeRight)

but I got the error message [SCNKit ERROR] removing the root node of a scene from its scene is not allowed and it did not work at all.

Has anybody a suggestion how I can achieve that?

Toby

tobynextdoor
  • 175
  • 2
  • 8

3 Answers3

12

This answer regards the issue (mentioned by @WolfLink) that having multiple SCNView objects with different cameras showing the same SCNScene causes the entire update sequence to occur multiple times.

To fix this, all you have to do is set the SCNSceneRendererDelegate to only one of the SCNView objects used. Assuming that the delegate is taking care of all of the nodes in the SCNScene and is updating them accordingly, the other SCNView objects that don't have an assigned delegate would still be able to see all of the changes occurring. This is because the changes are updated in the actual SCNScene that all of the SCNView objects are hooked up to.

So, using the original answer by @Toyos, the way to go about using 2 cameras without causing the entire update sequence to fire twice is:

// Set up sceneView 1
sceneView1.scene = scnScene
sceneView1.pointOfView = scnScene.camera1
sceneView1.delegate = scnScene

// Set up sceneView 2
sceneView2.scene = scnScene
sceneView2.pointOfView = scnScene.camera2

(Disclaimer: I was going to comment on the answer by @Toyos, but I currently don't have enough reputation for that since I'm still new to the StackOverflow community).

lizg
  • 121
  • 1
  • 3
  • If I have > 2 Scene Views all updating from the same scene, would I set the delegate on all but one of them or only one of them? – Brandon Slaght Apr 26 '17 at 02:02
  • Since you only have one scene in this case, I believe you would only set the delegate on one of the sceneViews. – lizg Apr 27 '17 at 22:20
11

Set the point of view to render the scene with the "pointOfView" property of the SCNView.

scnView.pointOfView = cameraNodeLeft;

Toyos
  • 4,024
  • 15
  • 16
  • This causes both SCNView objects to follow the entire update sequence twice, causing your project to run at double speed and using more resources than you need and want. – WolfLink Aug 25 '15 at 04:47
  • @WolfLink : Do you have a suggestion for a more efficient means of accomplishing the same task (displaying views from two cameras in the same scene)? – Mitch Cohen Dec 02 '15 at 16:10
  • @Mitch Cohen I'm looking for a solution to this too. I'm thinking using a custom SCNRenderer but I don't have the OpenGL experience to make one. – WolfLink Dec 11 '15 at 02:30
1
        SCNTransaction.animationDuration = 1.0
        scnView.defaultCameraController.frameNodes([node])
        let newPositoin = SCNVector3(node.boundingSphere.center.x, 0, nodeShereRadius*2)
        cameraNode.position = newPositoin
        scnView.pointOfView = cameraNode
        SCNTransaction.commit()
Memtimen
  • 41
  • 1
  • 4