- iOS 13.0+
- Xcode 11.0+
As an AR app runs, ARKit gathers information about a userʼs physical environment by processing the camera feed from the user’s device. To effect a multiuser AR experience in which users learn more about the environment by sharing the information from their device’s camera feed with other users, you enable collaboration.
Throughout a collaborative session, ARKit periodically provides data for you to share with peer users, and you choose a network protocol to send that data. In addition to information about the layout of the physical environment––the world data––collaboration data includes an anchor for each participant. These anchors indicate each peer’s approximate location, which you can use, for example, to place virtual content that represents the peer user. ARKit also provides you with any anchors that the peer users create themselves.
This sample app allows multiple users to view a common horizontal surface and place blocks on top of the surface, with a unique color for each user.
Although this sample draws its graphics using RealityKit, it doesn’t use RealityKit’s mechanism for over-the-network entity synchronization. Instead, it uses RealityKit as a renderer only as necessary to demonstrate ARKit’s collaborative session.
To create a shared AR experience using a host-guest approach, see Creating a Multiuser AR Experience.
Gather Collaboration Data
When collaboration is enabled, ARKit periodically invokes
session(_:, which provides collaboration data that you can share with nearby users. You are responsible for sending collaboration data over the network, including choosing the network framework and implementing the code. The data you send is a serialized version of the
ARSession object provided by your session. Before you send collaboration data over the network, first serialize it using
It’s safe to ignore the collaboration data if no peers have joined the session. In that case, ARKit outputs the collaboration data later to try again. The alternative approach of enabling collaboration only after peers have joined is not supported, because doing so restarts the session.
Send Collaboration Data to Others
You choose the network protocol with which to share collaboration data. This sample app sends collaboration data using MultipeerConnectivity.
Update Your Session with Collaboration Data
Facilitate World Map Merging
For ARKit to know where two users are with respect to each other, it has to recognize overlap across their respective world maps. When ARKit succeeds in fitting the two world maps together, it can begin sharing those users’ respective locations and any anchors they created with each other.
To aid ARKit with world map merging, a user must point their device near an area that another user has viewed. The sample app accomplishes this by asking the users to hold their devices side by side.
Identify when ARKit Merges World Data
The first time ARKit successfully merges world data from another user, it calls your app’s
session(_:, passing in an
ARParticipant that identifies the other user. This action notifies you of the merging event.
Visualize Users by Displaying Virtual Content
When ARKit successfully merges two users’ world data, you can then initiate actions to begin the multiuser experience. The sample adds virtual content in the real-world location of newly joined peer users to visualize them in AR.
The multicolored coordinate system shown in the following illustration represents the real-world pose of a peer user. ARKit periodically refreshes participant anchors to reflect any updates in the real-world location and orientation of the user it tracks. This process is a part of the collaboration data your app shares and uses to update its session.
Check an Anchor’s Owner
When ARKit merges two users’ world data, it collects all the anchors created by both users and calls
session(_: to notify each user of the collection. To check which user created an anchor, you compare the anchor’s
session with the active session’s
identifier. If the anchor’s session ID is different from the active session’s ID, the other user created the anchor.
Color Virtual Content Based on the User
To distinguish virtual content by user, you choose a different color for each user. The sample app uses the
to function to assign user colors.
The random color function works by applying a modulo operation to the anchor’s session ID, and interpreting the result as an index into a color array.
Place Virtual Content
When ARKit notifies you of a new nonparticipant anchor in
session(_:, place a block geometry tinted with the color calculated in the previous section.
The sample app uses common ARKit techniques to place virtual objects. For more information about mapping screen touches to real-world locations, see Placing Objects and Handling 3D Interaction.