iOS 適用的 Cloud Anchors 開發人員指南

適用於 iOS 的 ARCore SDK 與 ARKit 介面提供 Cloud Anchor 功能,方便您在相同環境中在 iOS 和 Android 裝置之間共用錨點。

瞭解如何在自己的應用程式中使用 ARCore Cloud Anchor API (或 ARCore Cloud Anchor 服務)。

必備條件

  • Xcode 13.0 以上版本
  • Cocoapods 1.4.0 以上版本 (如果使用 Cocoapods)
  • 搭載 iOS 12.0 以上版本的 ARKit 相容 Apple 裝置 (需要 iOS 12.0 以上版本的部署目標)

如果您是第一次使用 Cloud Anchors:

在應用程式中啟用 Cloud Anchor

如要使用 Cloud Anchors API,您必須建立 GARSessionConfiguration 並設定其 cloudAnchorMode 屬性,如「在 iOS 中設定 ARCore 工作階段」一節所述。請使用 setConfiguration:error: (GARSession) 進行設定。

您也必須為應用程式啟用 ARCore API

主機及解析錨點

您可以使用 ARCore Cloud Anchor API 託管及解析雲端錨點。API 包含已完成作業的回呼方法,以及可輪詢的 Future 物件。

代管錨定廣告

代管 ARAnchor 會將錨點置於任何特定實體空間的共同座標系統中。

主機要求會將視覺化資料傳送至 Google 伺服器,將 ARAnchor 的位置對應至代表目前實體空間的座標系統位置。成功的主機要求會傳回新的 Cloud Anchor ID,這個 ID 可共用,並於日後用於解析錨點。

- (void)addAnchorWithTransform:(matrix_float4x4)transform {
  self.arAnchor = [[ARAnchor alloc] initWithTransform:transform];
  [self.sceneView.session addAnchor:self.arAnchor];

  __weak ExampleViewController *weakSelf = self;
  self.hostFuture = [self.cloudAnchorManager
      hostCloudAnchor:self.arAnchor
           completion:^(NSString *anchorId, GARCloudAnchorState cloudState) {
             [weakSelf handleHostAnchor:anchorId cloudState:cloudState];
           }
                error:nil];
  [self enterState:HelloARStateHosting];
}

解析錨點

解決 ARAnchor 可讓特定實體空間中的 Android 和 iOS 裝置將先前代管的錨點新增至新場景。

解析要求會向 Google 伺服器傳送雲端錨點 ID 和目前影格的視覺資料。伺服器會嘗試將這個視覺化資料與目前代管雲端錨點的對應位置進行比對。解決成功後,會在工作階段中加入新的錨點並傳回。

- (void)resolveAnchorWithIdentifier:(NSString *)identifier {
  GARResolveCloudAnchorFuture *garFuture =
      [self.gSession resolveCloudAnchorWithIdentifier:identifier
                                    completionHandler:completion
                                                error:&error];
}

// Pass the ARFRame to the ARCore session every time there is a frame update.
// This returns a GARFrame that contains a list of updated anchors. If your
// anchor's pose or tracking state changed, your anchor will be in the list.
- (void)cloudAnchorManager:(CloudAnchorManager *)manager didUpdateFrame:(GARFrame *)garFrame {
  for (GARAnchor *garAnchor in garFrame.updatedAnchors) {
    if ([garAnchor isEqual:self.garAnchor] && self.resolvedAnchorNode) {
      self.resolvedAnchorNode.simdTransform = garAnchor.transform;
      self.resolvedAnchorNode.hidden = !garAnchor.hasValidTransform;
    }
  }
}

選用的 GARSession 輪詢模式

如果您使用金屬或需要輪詢選項,「且」應用程式執行速度至少為 30 fps,請使用下列模式將 ARFrame 傳遞至 GARSession

-(void)myOwnPersonalUpdateMethod {
  ARFrame *arFrame = arSession.currentFrame;
  NSError *error = nil;
  GARFrame *garFrame = [garSession update:arFrame error:&error];
  // your update code here
}

API 配額

ARCore API 要求頻寬的配額如下:

配額類型 上限 時間長度 套用對象
錨點數量 無上限 不適用 專案
錨定 host 要求 30 分鐘 IP 位址和專案
錨定resolve要求 300 分鐘 IP 位址和專案

已知問題和解決方法

使用 iOS 適用的 ARCore SDK 時,有幾個已知問題。

預設配置設定會導致應用程式間歇性當機

系統預設會啟用 GPU 影格擷取和 Metal API 驗證配置設定,這有時可能會導致應用程式在 SDK 中當機。

診斷應用程式當機問題

懷疑發生當機情況時,請查看堆疊追蹤。如果您在堆疊追蹤中看到 MTLDebugComputeCommandEncoder,則可能是因預設配置設定所導致。

解決方法

  1. 前往 Product > Scheme > Edit Scheme…

  2. 開啟「Run」分頁。

  3. 按一下 Options 即可查看目前的設定。

  4. 確認 GPU Frame CaptureMetal API Validation 均已停用。

  5. 建構並執行應用程式。

如需其他已知問題,請參閱 Cocoapods CHANGELOG

限制

iOS 版 ARCore SDK 不支援 ARKit setWorldOrigin(relativeTransform:) 方法呼叫。

效能注意事項

啟用 ARCore API 後,記憶體用量就會增加。由於網路使用率和 CPU 使用率較高,裝置的電池用量預期會增加。

後續步驟