Meshing Getting Started

How to setup meshing or add meshing to an existing scene.

The Meshing example scene in ARDK Examples is a good starting point for exploring a simple Meshing setup. This tutorial explores how to recreate it from scratch or add meshing to an existing AR scene.

Setting up the AR Session

To use meshing, the AR Session must be configured to enable the feature.

If you use an ARSessionManager in your scene, the easiest way is to add an ARMeshManager, preferably by adding the ARMesh prefab to the scene (see below). This will automatically run the AR Session with meshing enabled at the desired framerate.

Alternatively, if you prefer to manage the session in code, you need to explicitly enable meshing in the IARWorldTrackingConfiguration.

// Example of setting up Meshing with an ARSession
IARSession _session;
private void Init()
{
  // Configure Session to enable Meshing
  var configuration = ARWorldTrackingConfigurationFactory.Create();
  configuration.IsMeshingEnabled = true;
  configuration.MeshingTargetFrameRate = 10;

  // Create and Run session
  _session = ARSessionFactory.Create();
  _session.Run(configuration);

  // Optionally: Set mesh update callback
  _session.Mesh.MeshBlocksUpdated += OnMeshUpdated;
}

private void OnMeshUpdated(MeshBlocksUpdatedArgs args)
{
  var blocks = args.Mesh.Blocks;
  // Your code here...
}

Adding the ARMeshManager

The simplest way to add a mesh object to your scene is to drag the ARMesh prefab in Assets/ARDK/Extensions/Meshing into your scene.

../../../_images/meshing_armeshprefab.jpg

Note

The transform’s scale is (1, -1, 1) to reflect the coordinate system conversion between ARDK and Unity conventions.

The ARMeshManager helper subscribes to the IARSession ‘s Mesh.MeshBlocksUpdated event to parse mesh block data (IARMesh) into Unity GameObject s instantiated under the specified MeshRoot parent object.

The ARMeshManager helper also exposes a number of events and methods to toggle the visibility of the mesh, pause and resume updates, etc.

Mesh Block Prefabs

../../../_images/meshing_chunk.jpg

Mesh Block Prefabs, also called Chunks, are the template objects for each block of the mesh. Every new block will instantiate this prefab and update its MeshFilter over time. The MeshFilter is a Unity component containing the Unity Mesh object, which can be rendered by another component, the MeshRenderer. Physics are done with a MeshCollider, which owns its own reference to a Unity Mesh object.

Next Steps

At this stage, your scene is functional but doesn’t do much more than display the mesh.

This video takes these basic steps and adds collisions with virtual objects:

To go further, you could copy the prefab and customize it to add components as desired. See an idea of custom behavior in the Garden Chunk Tutorial.

You could also add navigation capabilities for virtual characters by using the mesh for the GameBoard Tutorial.

Either way, make sure to set up your Unity Editor to use Mock Meshes so you can test without deploying to device.