Contents

ImagePresentationComponent.Spatial3DImage

A 3D spatial scene created from a 2D image.

Declaration

class Spatial3DImage

Overview

Create and generate a Spatial3DImage to present a spatial scene in RealityKit. Spatial scenes are a 3D representation of a 2D image or photo which RealityKit renders with depth and motion parallax.

Spatial scene generation

To present a Spatial3DImage as a spatial scene, you must first generate it. Call the generate() method to generate an in-memory representation of a spatial scene from the original image. Generation can take a few seconds to complete.

You can choose to pre-generate a spatial scene in advance of presentation, or post-generate it in response to an interaction such as someone pressing a button.

In either case, start by creating a new Spatial3DImage from a local file URL for an existing image, or from an existing CGImageSource.

To pre-generate and present the image as a spatial scene, call the generate() method on the Spatial3DImage to generate its in-memory spatial scene representation. Next, use the init(spatial3DImage:) initializer to create an ImagePresentationComponent from the generated Spatial3DImage. Set the component’s desiredViewingMode to spatial3D or spatial3DImmersive, then add the component to an entity to present the spatial scene immediately.

To post-generate a spatial scene, create a new ImagePresentationComponent from the Spatial3DImage before generating it, and add the component to an entity. By default, the component displays the image with a monoscopic (mono) viewing mode. If you created the image from a spatial photo, you can choose to present the Spatial3DImage as a spatial photo instead by setting the component’s desiredViewingMode to spatialStereo or spatialStereoImmersive.

In your app’s UI, add a button or other trigger to convert the image to 3D. When someone presses the button, set the component’s desiredViewingMode to spatial3D or spatial3DImmersive, to indicate that you want the component to present the spatial scene as soon as the app finishes generating it. Then, call the generate() method to begin the generation process. The component displays a generation animation, similar to the Photos app on visionOS, and transitions to presenting the spatial scene as soon as generation completes.

Topics

Creating a spatial scene from a 2D image or spatial photo

Generating a spatial scene

See Also

Images and spatial scenes