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.

../../../_images/meshing_savemesh.jpg

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. On iOS you can enable the UIFileSharingEnabled value in your app’s Info.plist. You can enable this as part of a PostProcessBuild step similar to the following:

#if UNITY_IOS && UNITY_EDITOR

using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.IO;

public class EnableFileSharingPostProcessor
{
    [PostProcessBuild]
    public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
    {
        if (buildTarget == BuildTarget.iOS)
            BuildForiOS(path);
    }

    private static void BuildForiOS(string path)
    {
        // Get plist
        string plistPath = path + "/Info.plist";
        PlistDocument plist = new PlistDocument();
        plist.ReadFromString(File.ReadAllText(plistPath));

        // Set key and value for UIFileSharingEnabled.
        PlistElementDict rootDict = plist.root;
        rootDict.SetBoolean("UIFileSharingEnabled", true);

        // Write to file
        File.WriteAllText(plistPath, plist.WriteToString());
    }
}

#endif

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.

../../../_images/meshing_extract_ios.jpg

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.

../../../_images/meshing_mockmeshes.jpg

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 MockMesh component’s Mesh Path field in the inspector.

Note that 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 Meshes

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.