2

I create an Augmented Reality Project using Xcode's template.

Xcode creates a file called Experience.rcproject.

This project contains a scene called Box and a cube called Steel Cube.

I add 3 more scenes to Experience.rcproject, called alpha, bravo and delta.

I run the project.

Xcode runs these two lines

// Load the "Box" scene from the "Experience" Reality File
let boxAnchor = try! Experience.loadBoxX(namedFile: "Ground")

// Add the box anchor to the scene
arView.scene.anchors.append(boxAnchor)

These lines load the Box scene from the Experience file.

Once this scene is loaded how do I switch to another scene alpha, bravo or delta without having to load the whole thing?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
John Doe
  • 93
  • 7

1 Answers1

4

The simplest approach in RealityKit to switch two or more scenes coming from Reality Composer is to use removeAll() instance method, allowing you to delete all the anchors from array.

You can switch two scenes using asyncAfter(deadline:execute:) method:

let boxAnchor = try! Experience.loadBox()
arView.scene.anchors.append(boxAnchor)

    
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
    
    self.arView.scene.anchors.removeAll()

    let sphereAnchor = try! Experience.loadSphere()
    self.arView.scene.anchors.append(sphereAnchor)
}

Or you can switch two different RC scenes using a regular UIButton:

@IBAction func loadNewSceneAndDeletePrevious(_ sender: UIButton) {

    self.arView.scene.anchors.removeAll()

    let sphereAnchor = try! Experience.loadSphere()
    self.arView.scene.anchors.append(sphereAnchor)
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    Thanks. This answer is amazing. I wish I could upvote you to the stratosphere! – John Doe Jun 23 '20 at 12:37
  • What about if I am using image recognition and I want the anchors to swap out when it recognises a new image? I have my scenes all setup in Reality Composer and have loaded the anchors so now I need to append each anchor as the image is recognised while using removeAll to remove the existing one. – JeremyRaven Jan 12 '21 at 01:54
  • Hi @JeremyRaven, yes it's possible. All you need to do is to remove an old model tethered with its image anchor from your scene (look at this `if-else condition` principle – https://stackoverflow.com/questions/61486389/file-from-reality-composer-not-switching-scenes-on-iphone-7/62264048#62264048) The idea is the same: `arView.scene.anchors.removeAll()`. In addition to the above, you may use anchor's id or reference image's name in `if condition`. – Andy Jazz Jan 12 '21 at 07:31
  • 1
    @AndyFedoroff Ill check the anchor id and reference image name. Many thanks :) – JeremyRaven Jan 12 '21 at 08:09
  • Is there a specific runtime function I can use to put the if statement into so it will always check when the image is changed? – JeremyRaven Jan 12 '21 at 08:22
  • I want to reload the same scene in it's default state to start Entities back where they started and restart the animations. When I try to removeAll() and append the scene again it crashes. Any ideas? – Collin Oct 08 '22 at 01:13