Lab 104 – Portal Orb

Setting up a lab to play with some ideas for using RealityKit Portals.

This lab is just a quick scene setup for some ideas I want to explore soon. As we saw in Lab 040, the content of a portal and a parent scene can share the same coordinate space. As I move this orb around, we see two versions of the dome, but our perspective in both dome is the same.

Video Demo

Video demo showing a blue portal moving in a red dome sceme

Lab Code

struct Lab104: View {

    var body: some View {
        RealityView { content in

            // 1. The root for our scene *outside* of the portal
            guard let rootEntity = try? await Entity(named: "PortalSwapRed", in: realityKitContentBundle) else { return }
            content.add(rootEntity)

            // 2. The root for the content that will appear *inside* the portal
            // We need a WorldComponent here
            let portalContentRoot = Entity()
            portalContentRoot.components.set(WorldComponent())
            rootEntity.addChild(portalContentRoot)

            guard let portalGlass = try? await Entity(named: "PortalGlass", in: realityKitContentBundle) else { return }
            portalGlass.components.set(ManipulationComponent())
            portalGlass.position = [-0.7, 1.4, -1.5]
            rootEntity.addChild(portalGlass)

            // 3. We need something to render the portal on
            if let lens = portalGlass.findEntity(named: "Lens") {

                // Replace the material with PortalMaterial
                lens.components[ModelComponent.self]?.materials[0] = PortalMaterial()

                // We also need to add a PortalComponent that targets the portalContentRoot
                lens.components.set(PortalComponent(target: portalContentRoot))

            }

            // 4. We'll load some content to add to the portalContentRoot
            guard let scene = try? await Entity(named: "PortalSwapBlue", in: realityKitContentBundle) else { return }
            portalContentRoot.addChild(scene)

        }
    }
}

Support our work so we can continue to bring you new examples and articles.

Download the Xcode project with this and many more labs from Step Into Vision.

Questions or feedback?