Endlessly spawn objects

Hi everyone,

I want to make an AR experience where it just continuously drops a cube into the scene every 10 seconds. I was able to create something close enough in Reality Composer where it spawns a cube every time every time you tap the screen but a) it's not automatic and b) it's not continuous. Basically I am moving a cube from very far away so it cannot be seen into the view and then dropping it. Instead I would like this action to be automated and continuous on a loop.

Any ideas on how to archive this?

Many thanks in advance! – Berke

Answered by Vision Pro Engineer in 735385022

You would need to create new cubes programmatically, which you can do either by cloning an existing entity, or by creating an entirely new object. If you want spawn multiple copies of the same object, cloning is more efficient, since RealityKit can share resources between instances.

The steps involved are:

  1. Retrieve the cube entity from the loaded scene.
if let boxScene = try? Experience.loadBox() {
    if let box = boxScene.findEntity(named: "Steel Box") {}
       ...
    }
}
  1. Clone the entity.
let boxCopy = box.clone(recursive: true)
  1. Add the entity to the scene by creating an AnchorEntity and adding it as a child of the anchor.
let boxAnchor = AnchorEntity()
arView.scene.addAnchor(boxAnchor)
boxAnchor.addChild(boxCopy)

The clone will have the same transform as the original cube, so you'll need to modify each copy's transform to place them at a different location. See also https://developer.apple.com/documentation/realitykit/manipulating-reality-composer-scenes-from-code.

I assume you already know how to create a cube and add it to the scene programmatically, and your question is about automating this.

You can set your class as the delegate of the ARSession and leverage the session:(_:didUpdate:) callback (https://developer.apple.com/documentation/arkit/arsessiondelegate/2865611-session). In this callback, you receive regular ARFrame updates. By looking at the frame's timestamp you can compute how much time has passed to determine when to spawn the next object.

Alternatively, you can also use a Timer (https://developer.apple.com/documentation/foundation/timer) for performing work in specific intervals.

Is there some kind of curse on my profile? I have replied three times and the messages keep disappearing!

OK one last try, I will make this short because I refuse to type this out a third time.

Forget the timer for now let's make that cube spawn. RCproject here with gray cube and gold cube. When scene starts, golden cube disappears, gray cube drops. When tap ➔ gold cube is made visible and drops also.

How can I spawn more cubes? I can loop the action and keep spawning the same gold cube but I want MORE. Maybe I am thinking about this in the wrong way, I am usually not this clueless. The issue is I can't tell wether the looping/spawning/repeating process needs to happen in Reality Converter, Xcode, or both.

Inspiration: definitely not google's video platform me pipe .com/watch?v=SDeVyxtdSUk

Accepted Answer

You would need to create new cubes programmatically, which you can do either by cloning an existing entity, or by creating an entirely new object. If you want spawn multiple copies of the same object, cloning is more efficient, since RealityKit can share resources between instances.

The steps involved are:

  1. Retrieve the cube entity from the loaded scene.
if let boxScene = try? Experience.loadBox() {
    if let box = boxScene.findEntity(named: "Steel Box") {}
       ...
    }
}
  1. Clone the entity.
let boxCopy = box.clone(recursive: true)
  1. Add the entity to the scene by creating an AnchorEntity and adding it as a child of the anchor.
let boxAnchor = AnchorEntity()
arView.scene.addAnchor(boxAnchor)
boxAnchor.addChild(boxCopy)

The clone will have the same transform as the original cube, so you'll need to modify each copy's transform to place them at a different location. See also https://developer.apple.com/documentation/realitykit/manipulating-reality-composer-scenes-from-code.

It's working, but now I have a new problem:

import UIKit
import RealityKit

class ViewControllerBlender: UIViewController {

    @IBOutlet var arView: ARView!

    @IBAction func Clone(_ sender: Any) {
        if let boxScene = try? Blender.loadVoid() {
            if let box = boxScene.findEntity(named: "Steel Box") {
                let boxCopy = box.clone(recursive: true)
                let boxAnchor = AnchorEntity()
                arView.scene.addAnchor(boxAnchor)
                boxAnchor.addChild(boxCopy)
                }
            }
        }

    override func viewDidLoad() {
        super.viewDidLoad()        
    }
}

It functions as expected, but returns the error "Result of call to function returning 'Blender.Void' is unused" – why? The .rcproject file is called "Blender", the Scene is called "Void", and the Box is called "Steel Box".

Endlessly spawn objects
 
 
Q