Advanced Tutorial: Meshes in the Unity Editor
How to save a mesh as a file and reuse it on a device.
When working on a Meshing -based feature, it can be time consuming to deploy to the device every time you try or change something. For this reason, the ARDK offers a rudimentary system to save meshes as files on device and re-use them in the editor as Mock Meshes.
Save Mesh Files
The MeshSaver example scene in ARDK Examples contains a UI button Save Mesh File that calls the MeshSaver helper to dump the current mesh data into a binary file in the device’s file system.
Every push of the button creates a new mesh_*.bin file, named after the mesh version. Each file is a binary dump of the API’s Block, Vertex and Face buffers, as described in the Low Level Addendum. It is a good idea to save multiple meshes as you scan around if you want to reproduce mesh updates in the Unity Editor.
Importantly, you need to set up your application’s permissions to write files & allow file access over USB.
Extract Mesh Files on iOS
Connect your iPhone to your laptop via USB and use Finder (macOS Catalina & above) to browse to the phone’s files. Find ARDK Examples, and copy its
meshes folder to your local disk.
You’ll find inside multiple directories each representing a different AR session, named by timestamp. Inside each session folder, you’ll find the mesh_*.bin files.
Extract Mesh Files on Android
Connect you Android phone to your laptop via USB and use Android File Transfer to browse the phone’s files.
You will find the files under Android/data/com.nianticlabs.ar.ardkexamples/files/meshes, in multiple directories each representing a different AR session, named by timestamp.
Import Meshes into Unity
In your Unity scene hierarchy, create an empty
GameObject called “MockMeshes”. The goal of that object will be to load saved meshes in the editor, and test mesh-related logic without deploying to a device.
Add a MockMesh object by clicking Add Component and typing “mock mesh” in the search box.
Then copy/paste the mesh files to a folder in the scene’s file hierarchy. Right click on one of .bin files in the Project window, select Copy Path, and then paste that path into the
Mesh Path field in the inspector.
Time To Discovery is a parameter that sets, in seconds, how long it takes for the mesh to appear after you initialize a mock AR session in the Unity Editor’s Play mode.
To try it out, click the Unity Editor’s Play button. If you have a weird UI in the Game tab, just change the dropdown to simulate the aspect ratio of a portrait mode iPhone, then hit the Run AR button.
You’ll see the mesh appear after the set time to discovery. You can chain multiple mesh updates by adding multiple
MockMesh components to the objects, each with a different time to discovery and pointing to a different mesh file path. Alternatively, you can add a MockMeshSequence, for which you set the
Mesh Sequence Path field to a folder containing individual mesh files. Meshes will be loaded one by one, ordered by version number, with the interval in seconds specified in the
Update Interval field.
Sample mock meshes are available in the ardk-mock-meshes.unitypackage, available on lightship.dev. This package contains example meshes from scans of various real-world locations. Import this package into your Unity project to use these meshes as mock meshes.