SpatialMap_Sparse_Localizing¶
Demonstrate how to use the sparse SpatialMap uploaded into the database (e.g. created from SpatialMap_SparseSpatialMap sample).
Demonstrate how to localize sparse SpatialMap and put virtual objects onto SpatialMap
Reference: Motion Tracking and EasyAR Features.
Configurations¶
Server access information is required when using sparse spatial map, which can be get from SpatialMap page in EasyAR develop center. There are two ways to set this information in Unity.
One is global configuration, all scenes using global spatial map configuration will use this value. Select <EasyAR -> Sense -> Configuration> in Unity menu,
and enter the information in Project Settings get from develop center,
Another is local configuration in the scene, it is only valid to the current scene.
Map information is also required, which can be get from SpatialMap page in EasyAR develop center after create and upload from sample SpatialMap_SparseSpatialMap.
How to Use¶
There will be point cloud displayed in the scene. The cube will be placed and moved on the cloud when drag. Two finger pinch will scale the cube.
How It Works¶
Localize sparse spatial map in the scene¶
Sparse spatial map in the sample runs in localize mode.
To run sparse spatial map in localize mode, a SparseSpatialMapController should exist with Source Type set to Map Manager, and Map Worker set to the SparseSpatialMapWorkerFrameFilter in the session.
Use SparseSpatialMapWorkerFrameFilter.Localizer.startLocalization to start localization.
sparse.Localizer.startLocalization();
Objects under world root¶
WorldRoot is designed to do these things,
Control show/hide of objects when tracking status change.
Move together against camera according to ARSession.CenterMode.
You can ignore WorldRoot If you can make sure all above is handled by yourself.
In this sample, WorldRootController.ActiveControl is set to ActiveControlStrategy.HideWhenNotTracking, so the cube will hide when tracking fails.
Move object on sparse spatial map¶
You can perform hit test on the sparse spatial map and move cube to the returned point.
var viewPoint = new Vector2(touch.position.x / Screen.width, touch.position.y / Screen.height);
if (sparse && sparse.LocalizedMap)
{
var points = sparse.LocalizedMap.HitTest(viewPoint);
foreach (var point in points)
{
onSparse = true;
TouchControl.transform.position = sparse.LocalizedMap.transform.TransformPoint(point);
break;
}
}
Sparse spatial map events¶
Map events are used for custom operations, the sample use the events to output some information. You can delete them if they are not used, and you can use the events to handle your game logic.
Notice: load map (only the first time each map) will trigger a download in this sample. Statistical request count will be increased (more details on EasyAR website). Map cache is used after a successful download. Map cache will be cleared if SparseSpatialMapManager.clear is called or app uninstalled.
MapController.MapLoad += (map, status, error) =>
{
GUIPopup.EnqueueMessage("Load map {name = " + map.Name + ", id = " + map.ID + "} into " + sparse.name + Environment.NewLine +
" => " + status + (string.IsNullOrEmpty(error) ? "" : " <" + error + ">"), status ? 3 : 5);
...
};
MapController.MapLocalized += () =>
{
GUIPopup.EnqueueMessage("Localized map {name = " + MapController.MapInfo.Name + "}", 3);
};
MapController.MapStopLocalize += () =>
{
GUIPopup.EnqueueMessage("Stopped localize map {name = " + MapController.MapInfo.Name + "}", 3);
};