Displaying an AR Experience with Metal

Build a custom AR view by rendering camera images and using position-tracking information to display overlay content.

通過渲染相機圖像并使用位置跟蹤信息來顯示覆蓋內(nèi)容來構(gòu)建自定義AR視圖。


Overview

ARKit includes view classes for easily displaying AR experiences with SceneKit or SpriteKit. However, if you instead build your own rendering engine (or integrate with a third-party engine), ARKit also provides all the support necessary to display an AR experience with a custom view.


概述

ARKit包含視圖類,用于通過SceneKit或SpriteKit輕松顯示AR體驗。 但是,如果您改為創(chuàng)建自己的渲染引擎(或與第三方引擎集成),則ARKit還提供必要的所有支持以顯示具有自定義視圖的AR體驗。

In any AR experience, the first step is to configure an?ARSession?object to manage camera capture and motion processing. A session defines and maintains a correspondence between the real-world space the device inhabits and a virtual space where you model AR content. To display your AR experience in a custom view, you’ll need to:

Retrieve video frames and tracking information from the session.

Render those frame images as the backdrop for your view.

Use the tracking information to position and draw AR content atop the camera image.

在任何AR體驗中,第一步是配置一個ARSession對象來管理相機捕獲和運動處理。 會話定義并維護設(shè)備居住的真實世界空間與模擬AR內(nèi)容的虛擬空間之間的對應(yīng)關(guān)系。 要在自定義視圖中顯示您的AR體驗,您需要:

從會話中檢索視頻幀和跟蹤信息。

將這些幀圖像渲染為您視圖的背景。

使用跟蹤信息在相機圖像上定位和繪制AR內(nèi)容。

Note

This article covers code found in Xcode project templates. For complete example code, create a new iOS application with the Augmented Reality template, and choose Metal from the Content Technology popup menu.

注意

本文介紹了Xcode項目模板中的代碼。 有關(guān)完整的示例代碼,請使用增強現(xiàn)實模板創(chuàng)建一個新的iOS應(yīng)用程序,然后從內(nèi)容技術(shù)彈出式菜單中選擇Metal。

Get Video Frames and Tracking Data from the Session

Create and maintain your own?ARSession?instance, and run it with a session configuration appropriate for the kind of AR experience you want to support. The session captures video from the camera, tracks the device’s position and orientation in a modeled 3D space, and provides?ARFrame?objects. Each such object contains both an individual video frame image and position tracking information from the moment that frame was captured.

There are two ways to access?ARFrame?objects produced by an AR session, depending on whether your app favors a pull or a push design pattern.?

If you prefer to control frame timing (the pull design pattern), use the session’s?currentFrameproperty to get the current frame image and tracking information each time you redraw your view’s contents. The ARKit Xcode template uses this approach:


從Session獲取視頻幀和跟蹤數(shù)據(jù)

創(chuàng)建和維護自己的ARSession實例,并使用適合您希望支持的AR體驗類型的Session配置運行它。 該Session從相機捕獲視頻,在建模的3D空間中跟蹤設(shè)備的位置和方向,并提供ARFrame對象。 每個這樣的對象包含從捕獲幀的那一刻起的單獨的視頻幀圖像和位置跟蹤信息。

有兩種方法可以訪問ARSession產(chǎn)生的ARFrame對象,具體取決于您的應(yīng)用是否支持拉式或推式設(shè)計模式。

如果您更愿意控制幀時序(拉式設(shè)計模式),則每次重新繪制視圖內(nèi)容時,請使用會話的currentFrame屬性來獲取當(dāng)前幀圖像和跟蹤信息。 ARKit Xcode模板使用這種方法:

Alternatively, if your app design favors a push pattern, implement the?session:didUpdateFrame:?delegate method, and the session will call it once for each video frame it captures (at 60 frames per second by default).

Upon obtaining a frame, you’ll need to draw the camera image, and update and render any overlay content your AR experience includes.

或者,如果您的應(yīng)用設(shè)計偏好推式模式,請實施Session:didUpdateFrame:delegate方法,并且Session將為其捕獲的每個視頻幀調(diào)用一次(默認情況下為每秒60幀)。

在獲取框架后,您需要繪制相機圖像,并更新和渲染AR體驗包含的任何疊加內(nèi)容。

Draw the Camera Image

Each?ARFrame?object’s?capturedImage?property contains a pixel buffer captured from the device camera. To draw this image as the backdrop for your custom view, you’ll need to create textures from the image content and submit GPU rendering commands that use those textures.

The pixel buffer’s contents are encoded in a biplanar YCbCr (also called YUV) data format; to render the image you’ll need to convert this pixel data to a drawable RGB format. For rendering with Metal, you can perform this conversion most efficiently in GPU shader code. Use?CVMetalTextureCache?APIs to create two Metal textures from the pixel buffer—one each for the buffer’s luma (Y) and chroma (CbCr) planes:


繪制相機圖像

每個ARFrame對象的capturedImage屬性都包含從設(shè)備攝像頭捕獲的像素緩沖區(qū)。 要將此圖像作為自定義視圖的背景,您需要從圖像內(nèi)容創(chuàng)建紋理并提交使用這些紋理的GPU渲染命令。

像素緩沖區(qū)的內(nèi)容以雙平面YCbCr(也稱為YUV)數(shù)據(jù)格式編碼; 要渲染圖像,您需要將此像素數(shù)據(jù)轉(zhuǎn)換為可繪制的RGB格式。 對于使用Metal進行渲染,您可以在GPU著色器代碼中最有效地執(zhí)行此轉(zhuǎn)換。 使用CVMetalTextureCache API從像素緩沖區(qū)創(chuàng)建兩個Metal紋理 - 緩沖區(qū)的亮度(Y)和色度(CbCr)平面各一個:

Next, encode render commands that draw those two textures using a fragment function that performs YCbCr to RGB conversion with a color transform matrix:

接下來,編碼使用片段函數(shù)繪制這兩個紋理的渲染命令,該片段函數(shù)使用顏色轉(zhuǎn)換矩陣執(zhí)行YCbCr轉(zhuǎn)換為RGB轉(zhuǎn)換:

Note

Use the?displayTransformForOrientation:viewportSize:?method to make sure the camera image covers the entire view. For example use of this method, as well as complete Metal pipeline setup code, see the full Xcode template. (Create a new iOS application with the Augmented Reality template, and choose Metal from the Content Technology popup menu.)


注意

使用displayTransformForOrientation:viewportSize:方法確保攝像機圖像覆蓋整個視圖。 例如,使用此方法以及完整的Metal管道設(shè)置代碼,請參閱完整的Xcode模板。 (使用增強現(xiàn)實模板創(chuàng)建一個新的iOS應(yīng)用程序,并從內(nèi)容技術(shù)彈出式菜單中選擇Metal。)

Track and Render Overlay Content?

AR experiences typically focus on rendering 3D overlay content so that the content appears to be part of the real world seen in the camera image. To achieve this illusion, use the?ARAnchorclass to model the position and orientation of your own 3D content relative to real-world space. Anchors provide transforms that you can reference during rendering.?

For example, the Xcode template creates an anchor located about 20 cm in front of the device whenever a user taps on the screen:


跟蹤和渲染疊加內(nèi)容

AR體驗通常專注于渲染3D疊加內(nèi)容,以便內(nèi)容看起來是攝像機圖像中看到的真實世界的一部分。 為了達到這種幻想,請使用ARAnchor類對相對于現(xiàn)實世界空間的3D內(nèi)容的位置和方向進行建模。 錨點提供可在渲染過程中引用的變換。

例如,無論用戶何時點擊屏幕,Xcode模板都會在設(shè)備前創(chuàng)建一個位于設(shè)備前方約20厘米的錨點:

In your rendering engine, use the?transform?property of each?ARAnchor?object to place visual content. The Xcode template uses each of the anchors added to the session in its?handleTap?method to position a simple cube mesh:


在渲染引擎中,使用每個ARAnchor對象的transform屬性來放置可視內(nèi)容。 Xcode模板使用每個在其handleTap方法中添加到會話中的錨來定位簡單的多維數(shù)據(jù)集網(wǎng)格:

Note

In a more complex AR experience, you can use hit testing or plane detection to find the positions of real-world surfaces. For details, see the?planeDetection?property and the?hitTest:types:?method. In both cases, ARKit provides results as?ARAnchor?objects, so you still use anchor transforms to place visual content.

注意

在更復(fù)雜的AR體驗中,您可以使用命中測試或平面檢測來查找真實世界曲面的位置。 有關(guān)詳細信息,請參閱planeDetection屬性和hitTest:types:方法。 在這兩種情況下,ARKit都會提供結(jié)果作為ARAnchor對象,因此您仍然使用錨點轉(zhuǎn)換來放置可視內(nèi)容。

Render with Realistic Lighting

When you configure shaders for drawing 3D content in your scene, use the estimated lighting information in each?ARFrame?object to produce more realistic shading:

當(dāng)您配置著色器以在場景中繪制3D內(nèi)容時,請使用每個ARFrame對象中的估計光照信息來生成更逼真的著色:


Note

For the complete set of Metal setup and rendering commands that go with this example, see the full Xcode template. (Create a new iOS application with the Augmented Reality template, and choose Metal from the Content Technology popup menu.)

注意

有關(guān)此示例中完整的Metal安裝和渲染命令集,請參閱完整的Xcode模板。 (使用增強現(xiàn)實模板創(chuàng)建一個新的iOS應(yīng)用程序,并從內(nèi)容技術(shù)彈出式菜單中選擇金屬。)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,818評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,185評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,656評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,647評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,446評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,951評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,041評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,189評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,718評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,602評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,800評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,316評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,045評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,419評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,671評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,420評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,755評論 2 371

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,388評論 0 10
  • ARkit Introducing ARKit iOS 11引入ARKit,這是 個全新的框架,允許開發(fā)者輕松地為...
    坤哥愛卿閱讀 1,363評論 0 1
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,503評論 5 6
  • 文字是表達思想的工具。碼字砌文則是一件有趣的工作。寫文章和小時候搭積木是同一個原理。...
    冰夫閱讀 442評論 0 0
  • 南京以東的城市交通方便,空氣濕潤,環(huán)境好,之前沒有來過這邊要是一畢業(yè)就在這邊工作,真的應(yīng)該在這邊生活。
    藍天白云789閱讀 409評論 0 0