MegaBlock_Ema

Attention

The content of this page will be updated soon.

Note

This sample supports AR Foundation (it can also work without AR Foundation). If you want to use AR Foundation, please refer to AR Foundation Configuration for configuration.

Demonstrates how to use Mega’s annotation feature, export ema files using the tool, create and display 3D objects at the annotation data positions recorded in ema, and finally run to view tracking effects.

Attention

If this is your first time using Mega, it is not recommended to use this sample. The use of ema is not required in most Unity development scenarios.

Configuration and Running

Please refer to Mega Unity Sample Usage Guide for configuration and running.

Note that to use this sample, you need to export the ema file using Mega Studio Annotation Tool in another Unity project.

../../_images/image_s2_31.png

Then place the ema file at Assets/StreamingAssets/EasyAR.Mega.Annotation.ema. This path is written in the sample code and can be modified as needed.

../../_images/image_s2_41.png

This sample demonstrates how to use the ema file and create 3D objects at runtime by parsing the annotation data in the ema file. This usage is very useful in some special scenarios. After obtaining the ema, you do not need to use the tool to import Blocks and place 3D objects in the sample scene.

Running Effect

../../_images/image_s2_2.gif

How to Use

../../_images/image_s2_1.png
Label 1: Displays diagnostic information (latest version may show slight differences).
Label 2: 3D objects created in the sample according to ema data records, displayed at the positions annotated by ema.
Label 3: Stop/Start tracking. By default, content disappears when tracking is stopped.

How It Works

How is the scene created?

The section Mega Unity Create a Runnable Project from Scratch - Create EasyAR Mega Scene introduces the creation of EasyAR-related features in the scene.

Where are the 3D models?

No 3D models are provided in the scene. This sample does not require you to place 3D models yourself (of course, you can modify as needed). Models are created at runtime via script, please pay attention to this part in the script:

// here is where the "cube" or "sphere" in the sample is created
var placeholder = info.Geometry == AnnotationNode.GeometryType.Cube ? Instantiate(CubePlaceholder) : Instantiate(PointPlaceholder);

The objects created here are placed at the annotation points read from ema. Although the annotation data is also displayed as cubes, the cubes shown in the annotation tool are not the actual objects in the scene, and the objects created here are just “placeholders” and have no relation to the annotation data (ema file). You can use other models here as needed.

What is diagnostic information?

At runtime, you will find that positioning information is constantly updated, which means the application is continuously communicating with the server. This is the basis for Mega to run. Generally, you should not close this connection, and disconnecting from the server during use will greatly affect Mega’s performance.

Displaying and paying attention to diagnostic information during development is extremely helpful. It helps you understand the basic operation of the system and also establishes effective communication with the EasyAR team when reporting issues.

Important

Please read Message Output carefully and consider what configuration and control switches to use during development, testing, and after application launch. Note that communication with EasyAR usually requires providing this information, so it is recommended to make full use of it rather than turning it off immediately.

Normal service status that needs to be handled properly

Not shown in this sample, please refer to the code in MegaBlock_Basic.

Read ema file

StartCoroutine(LoadEmaFile(emaAssetPath));

ema decoding

var emaDecoded = EasyAR.Mega.Ema.EmaDecoder.Decode(emaData);
if (!(emaDecoded is EasyAR.Mega.Ema.v0_5.Ema ema))
{
    var message = $"ema {emaDecoded.Version} usage is not written in the sample";
    ShowMessage(10, message, string.Empty);
    throw new InvalidOperationException(message);
}

For simplicity, only ema format version 0.5 is supported in the sample. You need to use tools version 2.0 or above to export.

Parse Blocks in ema and add to BlockHolder

foreach (var item in ema.blocks)
{
    var info = new BlockController.BlockInfo { ID = item.id.ToString(), Timestamp = item.timestamp };
    if (!item.keepTransform && item.location.OnSome)
    {
        blockHolder.Hold(info, item.location.Value);
    }
    else
    {
        blockHolder.Hold(info, item.transform.ToUnity());
    }
}

This step adds Blocks to MapHolder, so that MegaTrackerFrameFilter`_ can track the Blocks. The effect achieved here is similar to setting Block Root to the tool-generated MegaBlocks node in the MegaBlock_Basic sample.

Get all annotation data of type Node and create 3D objects at corresponding positions

var nodes = ema.annotations.Where(a => (a is EasyAR.Mega.Ema.v0_5.Node node) && (node.geometry == "cube" || node.geometry == "point")).Select(a => a as EasyAR.Mega.Ema.v0_5.Node);
foreach (var node in nodes)
{
    var name = node.Name.OnSome ? node.Name.Value : string.Empty;
    var info = new AnnotationNode.AnnotationNodeInfo
    {
        ID = node.id.ToString(),
        Timestamp = node.timestamp,
        Geometry = node.geometry == "cube" ? AnnotationNode.GeometryType.Cube : AnnotationNode.GeometryType.Point,
        FeatureType = node.featureType.OnSome ? node.featureType.Value : null
    };

    // here is where the "cube" or "sphere" in the sample is created
    var placeholder = info.Geometry == AnnotationNode.GeometryType.Cube ? Instantiate(CubePlaceholder) : Instantiate(PointPlaceholder);

    if (node.parent is EasyAR.Mega.Ema.v0_5.Node.WorldParent wp)
    {
        AnnotationNode.Setup(placeholder, blockHolder.BlockRoot, info, node.transform.ToUnity(), wp.location);
    }
    else if (node.parent is EasyAR.Mega.Ema.v0_5.Node.BlockParent bp)
    {
        AnnotationNode.Setup(placeholder, blockHolder.GetBlock(bp.id.ToString()), info, node.transform.ToUnity());
    }
}

This part parses according to the definition of ema format 0.5 and creates 3D objects at the corresponding Node positions. Note that the position of the 3D object is related to parent. For Nodes with Block as parent, the parent node is the corresponding Scene.BlockController, and for Nodes with World as parent, the parent node is Scene.BlockRootController