-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
I am using Unity version 2022.3.11f1 and ARFoundation version 5.1.5, and I have a problem. When an image is tracked, its position is always (0, 0, 0). This is the code I am using:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using TMPro;
public class CalibrationApp : MonoBehaviour
{
[SerializeField] private ARTrackedImageManager aRTrackedImageManager;
[SerializeField] private List<Vector3> globalPositionList = new List<Vector3>();
[SerializeField] private List<Vector3> localPositionList = new List<Vector3>();
[SerializeField] private List<Quaternion> globalRotationList = new List<Quaternion>();
[SerializeField] private List<Quaternion> localRotationList = new List<Quaternion>();
[SerializeField] private Vector3 firstMarkerPosition;
[SerializeField] private Quaternion firstMarkerRotation;
[SerializeField] private bool isFirstMarkerTracked = false;
[SerializeField] private TextMeshProUGUI debugLog;
[SerializeField] private TextMeshProUGUI pathName;
[SerializeField] private TextMeshProUGUI jobState;
MarkerCollection myCollection;
// Start is called before the first frame update
void Start()
{
myCollection = new MarkerCollection();
}
// Update is called once per frame
void Update()
{
}
private void Awake()
{
aRTrackedImageManager = GetComponent<ARTrackedImageManager>();
}
private void OnEnable()
{
aRTrackedImageManager.trackedImagesChanged += ARTrackedImageManager_trackedImagesChanged;
}
private void OnDisable()
{
aRTrackedImageManager.trackedImagesChanged-=ARTrackedImageManager_trackedImagesChanged;
}
private void ARTrackedImageManager_trackedImagesChanged(ARTrackedImagesChangedEventArgs obj)
{
foreach(ARTrackedImage trackedImage in obj.added)
{
if(trackedImage.trackingState == TrackingState.None)
{
StartCoroutine(WaitForTrackingState(trackedImage));
}
jobState.text = trackedImage.referenceImage.name;
if(!isFirstMarkerTracked)
{
firstMarkerPosition=trackedImage.transform.position;
firstMarkerRotation=trackedImage.transform.rotation;
isFirstMarkerTracked=true;
}
Debug.Log(trackedImage.transform.position.x + "\t" + trackedImage.transform.rotation.x + "\t" + trackedImage.trackingState);
/*
globalPositionList.Add(trackedImage.transform.position);
globalRotationList.Add(trackedImage.transform.rotation);
localPositionList.Add(trackedImage.transform.InverseTransformPoint(firstMarkerPosition));
localRotationList.Add(Quaternion.Inverse(firstMarkerRotation) * trackedImage.transform.rotation);
*/
Marker marker = new Marker();
marker.name = trackedImage.referenceImage.name;
marker.globalPosition = trackedImage.transform.position;
marker.globalRotation = trackedImage.transform.rotation;
marker.localPosition = trackedImage.transform.InverseTransformPoint(firstMarkerPosition);
marker.localRotation = Quaternion.Inverse(firstMarkerRotation) * trackedImage.transform.rotation;
myCollection.markers.Add(marker);
}
foreach (ARTrackedImage trackedImage in obj.updated)
{
}
foreach (ARTrackedImage trackedImage in obj.removed)
{
}
}
IEnumerator WaitForTrackingState(ARTrackedImage trackedImage)
{
// Aspetta fin quando il valore di trackedImage.trackingState non diventa TrackingState.Tracking
while (trackedImage.trackingState != TrackingState.Tracking)
{
Debug.Log("Waiting for tracking state...");
yield return null; // Aspetta un frame prima di controllare di nuovo
}
// Una volta che lo stato di tracciamento è TrackingState.Tracking, esegui il codice desiderato
Debug.Log("Tracked Image is now being tracked!");
}
public void ExportData()
{
//Path combine ha la stessa funzione del "+", ma con il vantaggio di non dover mettere gli "slash"
string fileName = Path.Combine(Application.persistentDataPath, "calibrationData.json");
//serializzo la mia lista in formato Json
string jsonString = JsonUtility.ToJson(myCollection);
debugLog.text = jsonString;
pathName.text = fileName;
//Creo un file di testo nel percorso fileName con jsonString come contenuto
File.WriteAllText(fileName, jsonString);
Debug.Log(fileName);
}
}
[Serializable]
public class Marker
{
public string name;
public Vector3 globalPosition;
public Vector3 localPosition;
public Quaternion globalRotation;
public Quaternion localRotation;
}
[Serializable]
public class MarkerCollection
{
public List markers;
public MarkerCollection()
{
this.markers = new List<Marker>();
}
}
Could someone help me? In my application, I need to read the position of the markers in space so that I can print them later.
We also tried with a sample project, and the prefabs are still always instantiated at (0, 0, 0) and not at the marker's position.