Unity ARFoundation图像跟踪预制位置

时间:2019-07-15 09:39:27

标签: c# unity3d image-processing scripting

我正在尝试扫描标记并将3D对象放在该标记上。到目前为止,这是可行的,但是我似乎无法在脚本(arFoundation脚本)中找到位置,当找到referenceImage时,将预制件放在那里。获取referenceImage位置也很有用,所以我可以在该位置实例化对象。

任何帮助将不胜感激

我正在使用:

ARFoundation 2.2.0 preview
ARCore 2.1.0 preview
ARKit 2.2.0 preview
Unity 2019.1.8f1



//TrackedImageInfoManager.cs SCRIPT
public void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
    /*ARTrackedImage trackedImage = null;//SOME CODE FROM FORUMS, could this be better than below uncommented code?

    for (int i = 0; i < eventArgs.added.Count; i++)
    {
        trackedImage = eventArgs.added[i];
        // instantiate AR object, set trackedImage.transform
        // use a Dictionary, the key could be the trackedImage, or the name of the reference image -> trackedImage.referenceImage.name
        // the value of the Dictionary is the AR object you instantiate.
    }

    for (int i = 0; i < eventArgs.updated.Count; i++)
    {
        trackedImage = eventArgs.updated[i];
        if (trackedImage.trackingState == TrackingState.Tracking)
        //if (trackedImage.trackingState != TrackingState.None)
        {
            // set AR object to active, use Dictionary to get AR object based on trackedImage
            // you can also include TrackingState.Limited by checking for None
        }
        else
        {
            // set active to false
        }
    }

    for (int i = 0; i < eventArgs.removed.Count; i++)
    {
        // destroy AR object, or set active to false. Use Dictionary.
    }*/ 
    //END HERE

    //WORKING CODE STARTS HERE
    foreach ( var trackedImage in eventArgs.added)
    {
        trackedImage.transform.localScale = new Vector3(0.1f , 0.1f , 0.1f);//when object is instaniated, this handles it scale
        //trackedImage.transform.localPosition
        UpdateInfo(trackedImage);
    }

    foreach( var trackedImage in eventArgs.updated)
    {
        UpdateInfo(trackedImage);
    }


}

我需要找到一种方法来与实例化的预制件混为一谈,或者只是获取将其实例化(在referenceImage上)并自己实例化的位置。

1 个答案:

答案 0 :(得分:1)

在ARTrackedImageManager中将默认Prefeb设置为trackedImagePrefab, 如果您想在每个新的图像轨道上实例化自己的预制件,则可以像这样更改代码

void UpdateInfo(ARTrackedImage trackedImage)
{

  if (trackedImage.trackingState != TrackingState.Tracking)
    {

     if(trackedImage.referenceImage.name == "ABC"){
        Instantiate(Prefeb1, transform.position, transform.rotation)
      }
     else if(trackedImage.referenceImage.name == "XYZ"){
        Instantiate(prefeb2, transform.position, transform.rotation)
      }

    }else{
    // Destroy object if you dont want to keep
   }
}

希望此代码对您有所帮助。