Unity ARFoundation 图像跟踪预制位置

Posted

技术标签:

【中文标题】Unity ARFoundation 图像跟踪预制位置【英文标题】:Unity ARFoundation image tracking prefab location 【发布时间】:2019-11-23 23:55:12 【问题描述】:

我正在尝试扫描标记并将 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】:
 if (trackedImage.trackingState == TrackingState.Tracking)
            
// do something

这行代码可以跟踪更新问题。

【讨论】:

【参考方案2】:

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

void UpdateInfo(ARTrackedImage trackedImage)


  if (trackedImage.trackingState != TrackingState.Tracking)
    

     if(trackedImage.referenceImage.name == "ABC")
        Instantiate(prefab1, transform.position, transform.rotation);
      
     else if(trackedImage.referenceImage.name == "XYZ")
        Instantiate(prefab2, transform.position, transform.rotation);
      

    else
    // Destroy object if you dont want to keep
   

希望这段代码对你有所帮助。

【讨论】:

以上是关于Unity ARFoundation 图像跟踪预制位置的主要内容,如果未能解决你的问题,请参考以下文章

ARFoundation系列讲解 - 85 手部跟踪

ARFoundation系列讲解-54人脸跟踪三

ARFoundation系列讲解-52人脸跟踪

ARFoundation之路_很全面的

[ARFoundation]unity ARfoundation+ARcore

[ARFoundation]unity ARfoundation+ARcore