iOS 向け Cloud Anchors デベロッパー ガイド

ARCore SDK for iOS は ARKit とインターフェースを持ち、Cloud Anchor 機能を提供します。これにより、同じ環境内の iOS デバイスと Android デバイス間でアンカーを共有できます。

独自のアプリで ARCore Cloud Anchor API または ARCore Cloud Anchor サービスを使用する方法を学びます。

前提条件

  • Xcode バージョン 13.0 以降
  • Cocoapods を使用している場合: Cocoapods 1.4.0 以降
  • iOS 12.0 以降を搭載した ARKit 対応の Apple デバイス(デプロイ ターゲットが iOS 12.0 以降である必要があります)

Cloud Anchors を初めて使用する場合は、次の手順で操作します。

アプリで Cloud Anchor を有効にする

Cloud Anchors API を使用するには、iOS で ARCore セッションを構成するで説明されているように、GARSessionConfiguration を作成し、cloudAnchorMode プロパティを設定する必要があります。setConfiguration:error: (GARSession) を使用して構成を設定します。

また、アプリで ARCore API を有効にする必要があります。

アンカーをホストして解決する

Cloud Anchors をホストして解決するには、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 デバイスは、以前にホストされたアンカーを新しいシーンに追加できます。

解決リクエストでは、現在のフレームの画像データとともにクラウド アンカー ID が Google サーバーに送信されます。サーバーは、この画像データを、現在ホストされている Cloud Anchor がマッピングされている場所の画像と照合しようとします。解決が成功すると、新しいアンカーがセッションに追加され、返されます。

- (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 ポーリング パターン

Metal を使用している場合、またはポーリング オプションが必要な場合、かつアプリが 30 fps 以上で実行される場合は、次のパターンを使用して ARFrameGARSession に渡します。

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

API 割り当て

ARCore API には、リクエスト帯域幅に次の割り当てがあります。

割り当てのタイプ 最大 期間 適用対象
アンカーの数 無制限 なし プロジェクト
アンカーのホスト リクエスト 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 使用率の増加により、デバイスのバッテリー使用量が増加する可能性があります。

次のステップ