Hey All!
I wrote a script that detects markers and spawns a game object (In this case, a prefab of type "World Object" based on the id of the marker) on the position of the marker. the script is based on the Example scene. The problem is that all objects instantiate in a major offset from the marker itself, both in app simulator and in ML2 build. The problem occurs both for QR and ArUco.
In the simulator I added markers with the length of 100 and set the "QrCodeMarkerSize" and "ArucoMarkerSize" variables to 0.1f. From other topics in the forum it appears maybe there's something i am missing here with the scale of the marker or the object?
Code is Attached below:
using MagicLeap.Examples;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.XR.MagicLeap;
public class MLMarkerIdentifier : MonoBehaviour
{
#region Serialized fields
[SerializeField] private float QrCodeMarkerSize = 0.1f;
[SerializeField] private float ArucoMarkerSize = 0.1f;
[SerializeField] private MLMarkerTracker.MarkerType Type = MLMarkerTracker.MarkerType.Aruco_April;
[SerializeField] private MLMarkerTracker.ArucoDictionaryName ArucoDict = MLMarkerTracker.ArucoDictionaryName.DICT_5X5_50;
[SerializeField] private MLMarkerTracker.Profile Profile = MLMarkerTracker.Profile.Default;
[SerializeField] WorldObjectsLoader worldObjectsLoader;
#endregion
#region private fields
private ASCIIEncoding _asciiEncoder = new ASCIIEncoding();
private HashSet<string> _activeObjects = new HashSet<string>();
private Camera _mainCamera;
#endregion
#region Mono Behaviour
private void OnEnable()
{
MLMarkerTracker.OnMLMarkerTrackerResultsFound += OnTrackerResultsFound;
}
private void Start()
{
MLMarkerTracker.TrackerSettings trackerSettings = MLMarkerTracker.TrackerSettings.Create(
true, Type, QrCodeMarkerSize, ArucoDict, ArucoMarkerSize, Profile);
_ = MLMarkerTracker.SetSettingsAsync(trackerSettings);
_mainCamera = Camera.main;
}
private void OnDisable()
{
MLMarkerTracker.OnMLMarkerTrackerResultsFound -= OnTrackerResultsFound;
_ = MLMarkerTracker.StopScanningAsync();
}
#endregion
/// <summary>
/// Given a marker data, extract the code Id and determine marker size
/// </summary>
/// <param name="data"> marker data given by scanner </param>
/// <returns>
/// string - the code id.
/// float - the marker size.
/// </returns>
private string ExtractInfoFromMarkerData(MLMarkerTracker.MarkerData data)
{
string id = "";
switch (data.Type)
{
case MLMarkerTracker.MarkerType.Aruco_April:
id = data.ArucoData.Id.ToString();
break;
case MLMarkerTracker.MarkerType.QR:
id = _asciiEncoder.GetString(data.BinaryData.Data, 0, data.BinaryData.Data.Length);
break;
case MLMarkerTracker.MarkerType.EAN_13:
case MLMarkerTracker.MarkerType.UPC_A:
id = _asciiEncoder.GetString(data.BinaryData.Data, 0, data.BinaryData.Data.Length);
Debug.Log("No pose is given for marker type " + data.Type + " value is " + data.BinaryData.Data);
break;
}
return id;
}
private void SetObjectTransform(WorldObject worldObject ,MLMarkerTracker.MarkerData data)
{
// set the marker object transform
worldObject.transform.position = data.Pose.position; // marker.PositionOffset;
Debug.Log("marker pos: " + data.Pose.position);
Debug.Log("object pos: " + worldObject.transform.position);
// make object face the player.
Vector3 targetDirection = -(_mainCamera.transform.position - worldObject.transform.position);
Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
worldObject.transform.rotation = Quaternion.Euler(0f, targetRotation.eulerAngles.y, 0f);
// worldObject.transform.localScale = marker.scale;
//Debug.Log($"marker position: {marker.transform.position.ToString()}, data position: {data.Pose.position}");
}
private void OnTrackerResultsFound(MLMarkerTracker.MarkerData data)
{
var id = ExtractInfoFromMarkerData(data);
if (_activeObjects.Contains(id)) return;
Debug.Log($"Tracker detected, id: {id} ");
if (!string.IsNullOrEmpty(id))
{
var objectToLoad = worldObjectsLoader.GetWorldObjectPrefab(id);
if (objectToLoad == null)
{
Debug.LogError($"Error: Object with the given id {id} not found, prefab wasn't loaded.");
return;
}
WorldObject newWorldObject = Instantiate(objectToLoad);
SetObjectTransform(newWorldObject, data);
_activeObjects.Add(id);
}
}
}
Unity Editor version: 2022.3.16f1
ML2 OS version: 1.4.0-dev2
MLSDK version: 1.4.0
ML MRTK 3: 1.0.0-pre5
Host OS: Windows
Thank you in advance!
Gal.