React Native possesses a unique capability that enables the blending of Native code with JavaScript as needed. In today's blog post, we will explore how to enhance your spatial application with immersive features using Swift and the RealityKit framework.
Our goal is to create a Mixed Reality Immersive Space that generates snow particles:
Disclaimer: The main focus of this blog post is to describe how to integrate custom RealityKit code into React Native visionOS, not how to use RealityKit. If you want to dive deeper into this topic, checkout this WWDC talk.
Getting started
First let's initialize a new project:
Next, go to YourApp/visionos folder and run following commands to install Pods:
Open the project in Xcode and click Run
JavaScript part
Let's start with implementing JavaScript part. Navigate to App.tsx and add basic markup with two buttons and text.
Next, let's implement methods to open and close immersive spaces:
This code calls XR.requestSession() passing a unique identifier of ImmersiveSpace that we will implement in the next section.
Warning: XR API doesn't store information whether session is open, you need to track it yourself.
Swift Part
Inside of App.swift add new ImmersiveSpace. Use the same identifier as in JavaScript part.
And that's it! Now you have App UI written in React Native and spatial extension in RealityKit. I hope you found this article useful. If you have any questions or feedback feel free to reach out to me on Twitter.
For those interested in the sample code, you can find it in the GitHub Repository.