Table of Contents

添加一組幀數據源

一個 AR Session 可以包含多個幀數據源組件,稱爲幀數據源組(frame source group)。在運行時,session 會根據當前設備和啓用的 AR 功能,從幀數據源組中選取一個最合適的幀數據源進行使用。本文介紹瞭如何使用和管理幀數據源組。

開始之前

  • 瞭解 幀數據源 的基本概念、類型以及運行時的選取方法。

使用預設 AR Session 的幀數據源組

使用默認配置創建的 session 會自帶一組幀數據源,在使用單一 AR 功能時,一般就足夠了。

不同預設 session 中所包含的幀數據源不同。

使用 ARSessionFactory.ARSessionPreset.ImageTracking 預設或 AR Session (Image Tracking Preset) 菜單創建的 session 中只有單個幀數據源:

alt text

使用 ARSessionFactory.ARSessionPreset.MegaBlock_MotionTracking_Inertial 預設或 AR Session (Mega Block Default Preset) 菜單創建的 session 中包含多個幀數據源組件的場景層級結構:

alt text

如果場景中一開始使用了某個預設創建了 session,在迭代過程中增加其它功能時,不僅需要添加相應的 frame filter 組件,還需要根據需要添加合適的幀數據源組件。

重要事項

從 4.7 或更低版本升級後,需要整個刪除 session 後重新通過菜單或 ARSessionFactory 創建 session,才能使用預設的幀數據源組。

以下列出了所有預設的 AR 功能默認配置的幀數據源組件,注意列表中的排序與場景中幀數據源的組件排序相同:

預設 幀數據源組
  1. CameraDeviceFrameSource
  1. XREALFrameSource
  2. AREngineFrameSource
  3. ARCoreFrameSource
  4. ARCoreARFoundationFrameSource
  5. ARKitFrameSource
  6. ARKitARFoundationFrameSource
  7. VisionOSARKitFrameSource
  8. MotionTrackerFrameSource
  1. XREALFrameSource
  2. AREngineFrameSource
  3. ARCoreFrameSource
  4. ARCoreARFoundationFrameSource
  5. ARKitFrameSource
  6. ARKitARFoundationFrameSource
  7. VisionOSARKitFrameSource
  8. MotionTrackerFrameSource
  9. EditorCameraDeviceFrameSource
  1. XREALFrameSource
  2. AREngineFrameSource
  3. ARCoreFrameSource
  4. ARCoreARFoundationFrameSource
  5. ARKitFrameSource
  6. ARKitARFoundationFrameSource
  7. VisionOSARKitFrameSource
  8. MotionTrackerFrameSource
  9. InertialCameraDeviceFrameSource
  10. EditorCameraDeviceFrameSource
  1. XREALFrameSource
  2. AREngineFrameSource
  3. ARCoreFrameSource
  4. ARCoreARFoundationFrameSource
  5. ARKitFrameSource
  6. ARKitARFoundationFrameSource
  7. VisionOSARKitFrameSource
  8. MotionTrackerFrameSource
  9. InertialCameraDeviceFrameSource
  10. ThreeDofCameraDeviceFrameSource
  11. EditorCameraDeviceFrameSource
  1. XREALFrameSource
  2. AREngineFrameSource
  3. ARCoreFrameSource
  4. ARCoreARFoundationFrameSource
  5. ARKitFrameSource
  6. ARKitARFoundationFrameSource
  7. VisionOSARKitFrameSource
  8. MotionTrackerFrameSource
  9. InertialCameraDeviceFrameSource
  10. ThreeDofCameraDeviceFrameSource
  11. CameraDeviceFrameSource
  1. XREALFrameSource
  2. AREngineFrameSource
  3. ARCoreFrameSource
  4. ARCoreARFoundationFrameSource
  5. ARKitFrameSource
  6. ARKitARFoundationFrameSource
  7. VisionOSARKitFrameSource
  8. MotionTrackerFrameSource
  9. CameraDeviceFrameSource
附註

使用預設創建的組件排序可以保證在所有受內置幀數據源支持的設備上使用最優的幀數據源。

使用默認幀數據源配置

在使用默認參數時,幀數據源的配置會根據設備和運行時啓用的 AR 功能自動調整。

如果手動修改過幀數據源的參數,在 session 中的 AR 功能發生變化時(比如在原本只包含圖像跟蹤的 session 中新增了運動跟蹤功能),可能需要手動調整幀數據源的參數以適應新的功能需求,這樣所有 AR 功能才能以最佳效果運行。

重要事項

從 4.7 或更低版本升級後,需要整個刪除 session 後重新通過菜單或 ARSessionFactory 創建 session,才能使用正確的默認參數。

添加幀數據源組

Hierarchy 視圖中,選中 AR Session (EasyAR) 並點擊右鍵,通過菜單 EasyAR Sense > [ AR 功能 ] > Frame Source : * 可以添加適合該功能的 frame source 組件。也可以通過菜單 EasyAR Sense > Frame Source by Transform Type > * Dof > Frame Source : * 添加需要的 frame source 組件。

在腳本中,可以使用 ARSessionFactory.AddFrameSource<Source>(GameObject, bool) 來添加 frame source 組件。

比如,通過菜單 EasyAR Sense > Frame Source by Transform Type > 3 Dof Rot-Only > Frame Source : Three Dof Camera Device 可以給當前選中的 session 添加一個 ThreeDofCameraDeviceFrameSource

alt text

對應的腳本代碼如下:

ARSessionFactory.AddFrameSource<ThreeDofCameraDeviceFrameSource>(session);

幀數據源排序

session 組裝過程中,幀數據源組中最終只有一個幀數據源會被選中後組裝到 session 中,選取的規則取決於 AssembleOptions.FrameSourceSelection 屬性的值。在默認配置下,可以通過調整幀數據源組中各個組件的排序來影響最終被選中的幀數據源。

一般可以使用在 Hierarchy 視圖中 對場景中的物體進行排序 的方法直接移動 frame source 物體進行排序。

在腳本中,可以使用 Transform.SetSiblingIndex(int) 來調整物體的排序。

比如,要將 MotionTrackerFrameSource 排在其它幀數據源前面,可以在 Hierarchy 視圖中選中 Motion Tracker 物體並拖動到最上面的位置。

相同的效果也可以通過下面的腳本代碼實現:

motionTrackerFrameSource.transform.SetSiblingIndex(0);

另外還有一些預定義的排序方法可以使用。在 Hierarchy 視圖中,選中 AR Session (EasyAR) 並點擊右鍵,通過菜單 EasyAR Sense > Utility > Sort Frame Source : * > * 對特定的若干幀數據源組件進行排序。

在腳本中,可以使用 ARSessionFactory.SortFrameSource(GameObject, ARSessionFactory.FrameSourceSortMethod) 實現相同的效果。

比如,通過菜單 EasyAR Sense > Utility > Sort Frame Source : Motion Tracker > System SLAM 可以將 MotionTrackerFrameSource 排在 ARCoreFrameSourceARCoreARFoundationFrameSourceARKitFrameSourceARKitARFoundationFrameSourceAREngineFrameSource 前面。

alt text

對應的腳本代碼如下:

ARSessionFactory.SortFrameSource(session, new ARSessionFactory.FrameSourceSortMethod { MotionTracker = ARSessionFactory.FrameSourceSortMethod.MotionTrackerSortMethod.PreferEasyAR });

經過上面的排序之後,場景層級結構變爲:

alt text

相關主題