採用 ARKit 的 ARCore SDK 介面搭配 ARKit 來提供 Cloud Anchor 功能可讓您在 iOS 和 Android 裝置之間共用錨點, 同一個環境
瞭解如何在自己的應用程式中使用 ARCore Cloud Anchor API 或 ARCore Cloud Anchor 服務。
必要條件
- Xcode 13.0 以上版本
- Cocoapods 1.4.0 以上版本 (如果使用 Cocoapods)
- 與 ARKit 相容的 Apple 裝置 (搭載 iOS 12.0 以上版本) (需要部署目標為 iOS 12.0 以上版本)
如果您是第一次使用 Cloud Anchors:
在應用程式中啟用 Cloud Anchors
如要使用 Cloud Anchors API,您必須建立
GARSessionConfiguration
敬上
然後為該資源設定 cloudAnchorMode
屬性,方法如
在 iOS 中設定 ARCore 工作階段。使用
setConfiguration:error: (GARSession)
敬上
進行設定
您也必須啟用 ARCore API 應用程式。
主機和解析錨點
您可以使用 ARCore Cloud Anchor API 代管及解析雲端錨點。API 包含已完成作業的回呼方法,以及 Future 物件 分別用於輪詢
代管錨點
代管 ARAnchor
會將錨點放在通用座標系統中,
實體空間
主機要求會將圖像資料傳送至 Google 伺服器,該伺服器對應 ARAnchor
的
位置,以代表目前實際空間的座標系統位置。A 罩杯
成功的主機要求會傳回新的 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 Anchors 對應位置的圖像。時間 就會成功解析,系統會將新的錨定標記加入工作階段並傳回。
- (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 的要求頻寬配額如下:
配額類型 | 上限 | 持續時間 | 套用對象 |
---|---|---|---|
錨點數量 | 無限制 | 不適用 | 專案 |
錨定主機要求 | 30 | 分鐘 | IP 位址與專案 |
錨定解決要求 | 300 | 分鐘 | IP 位址與專案 |
已知問題和解決方法
使用 iOS 適用的 ARCore SDK 時,有幾個已知問題。
預設配置設定會導致應用程式間歇性當機
以下程式碼已啟用 GPU Frame Capture 和 Metal API 驗證配置設定: 因為這個預設值有時可能會導致應用程式在 SDK 中異常終止。
診斷應用程式當機問題
如果您認為發生當機情形,請查看堆疊追蹤。
如果在堆疊追蹤中看到 MTLDebugComputeCommandEncoder
,可能的原因是
預設配置設定
解決方法
前往Product > Scheme > Edit Scheme…。
開啟「Run」分頁。
按一下 Options 即可查看目前的設定。
確認 GPU Frame Capture 和 Metal API Validation 都已停用。
建構並執行應用程式。
請參閱 Cocoapods CHANGELOG
瞭解其他已知問題。
限制
iOS 適用的 ARCore SDK 不支援 ARKit setWorldOrigin(relativeTransform:)
方法呼叫。
效能注意事項
啟用 ARCore API 後,記憶體用量會增加。迎接 網路用量和 CPU 使用率偏高,導致裝置的電池用量升高。