Tuesday, July 26, 2022
HomeGame Developmentunity - Tracked picture positions at all times get ARSessionOrgin

unity – Tracked picture positions at all times get ARSessionOrgin


I’m constructing an AR app wherein I wish to monitor 4 pictures. With the 4 places from the photographs I wish to scale a aircraft with a fabric like a map.

I wrote my class to create empty sport objects on the positions from these pictures and hand them to the aircraft drawing class, however I’ve the issue that each detected picture will get the place coordinates from my ARSessionOrigin, as an alternative of a unique place for every picture primarily based on its detected location. I’m caught for days and might’t discover the error.

(For visible debugging, I added the road to instantiate a dice as an alternative of an empty sport object)

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing UnityEngine.UI;
utilizing UnityEngine.XR.ARFoundation;

public class AddAnchor : MonoBehaviour
{
   non-public ARTrackedImageManager trackedImageManager;
   public Record<EmptyList> emptyList = new Record<EmptyList>();
   public Materials materials;
   
   //new record simply to maintain monitor of the GameObjects
   public Record<GameObject> Empty = new Record<GameObject>();

   non-public void Awake()
   {
      trackedImageManager = FindObjectOfType<ARTrackedImageManager>();
   }

   void OnEnable() => trackedImageManager.trackedImagesChanged += OnChanged;

   void OnDisable() => trackedImageManager.trackedImagesChanged -= OnChanged;

   void OnChanged(ARTrackedImagesChangedEventArgs eventArgs)
   {
      // for each new detected Picture a brand new Gameobject and Entry in Record are set when the title of the detected Picture is not in record
      foreach (ARTrackedImage newImage in eventArgs.added)
      {
         
         bool isInside = false;
         for (int i = 0; i < emptyList.Rely; i++)
         {

            if (newImage.title == emptyList[i].trackedImageName)
            {
               //do nothing the Picture is within the Record and set bool on true
               isInside = true;
            }
         }
         // if within the Record no similar title is discovered a GameObject and Record Entry are set
         if(isInside == false)
         {
            GameObject tmp = setEmpty(newImage.referenceImage.title, newImage.remodel.place);
            Debug.Log(newImage.remodel.place + " newImage place");
            emptyList.Add(new EmptyList(newImage.title, newImage.remodel.place, tmp));
            //Add the created GameObjects to record
            Empty.Add(tmp);
            Debug.Log("Empty is add to record" );
            Debug.Log(tmp.remodel.place + " tmp place");
            Airplane(Empty, materials);
            
         }
      }
   

      foreach (ARTrackedImage updatedImage in eventArgs.up to date)
      {
         for (int i = 0; i < emptyList.Rely; i++)
         {
            if (updatedImage.title != emptyList[i].trackedImageName)
            {
               //do nothing
            }
            else
            {
               // when the picture is up to date take away the Entry in Record and get the GameObject to vary location. Then Add the brand new Entry to Record
               // The Replace Half mustn't have an effect on the Record for GameObjects
               emptyList.RemoveAt(i);
               GameObject tmp = getEmpty(updatedImage.title);
               tmp.remodel.place = updatedImage.remodel.place;
               emptyList.Add(new EmptyList(updatedImage.title, updatedImage.remodel.place, tmp));
               
            }
         }
      }

      foreach (ARTrackedImage removedImage in eventArgs.eliminated)
      {
         for (int i = 0; i < emptyList.Rely; i++)
         {
            
            if (removedImage.title == emptyList[i].trackedImageName)
            {
               
               //when the Picture is eliminated the associated GameObject ist detected and destroyed and the Entry in Record deleted
               GameObject tmp = getEmpty(removedImage.title);
               Destroy(tmp);
               //Delete GameObjects out of Record
               Empty.Take away(tmp);
               emptyList.RemoveAt(i);
            }
         }
      }
   }

   //units Title and Positon from the detected Picture onto the instantiated GameObject
   public GameObject setEmpty(string title, Vector3 place)
   {
      GameObject tmp = new GameObject();
      //GameObject tmp = GameObject.CreatePrimitive(PrimitiveType.Dice);
      tmp.title = title;
      tmp.remodel.place = place;
      //tmp.remodel.localScale = new Vector3(0.1f, 0.1f, 0.1f);
      return tmp;
      
   }
   
   //Technique finds GameObject primarily based on title
   public GameObject getEmpty(string title)
   {
      GameObject tmp = GameObject.Discover(title);
      return tmp;
   }

   public void Airplane(Record<GameObject> Empty, Materials mat)
   {
      if (Empty.Rely == 4)
      {
         Debug.Log("4 Emptys in Record Airplane ought to be drawn");
         CreatePlane.CreateMap(Empty, mat);
      }
   }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments