Displaying an AR Experience with Metal
通過渲染攝像機(jī)圖像和使用位置跟蹤信息來顯示覆蓋內(nèi)容,構(gòu)建自定義AR視圖。
arkit包括查看容易顯示AR的經(jīng)驗(yàn)與SceneKit或SpriteKit類。但是,如果你建立你自己的渲染引擎(或與第三方引擎集成),ARKit還提供了所有必要的支持,以顯示一個(gè)自定義視圖AR體驗(yàn)。
在任何AR經(jīng)驗(yàn)中,第一步是配置一個(gè)arsession對(duì)象來管理攝像機(jī)捕獲和運(yùn)動(dòng)處理。會(huì)話定義并保持設(shè)備所在的真實(shí)世界空間與虛擬空間之間的對(duì)應(yīng)關(guān)系,并在其中模擬AR內(nèi)容。要在自定義視圖中顯示AR體驗(yàn),您需要:
1.從會(huì)話中檢索視頻幀和跟蹤信息。
2.將這些框架圖像渲染為視圖的背景。
3.使用跟蹤信息在相機(jī)圖像上方定位和繪制AR內(nèi)容。
Note
本文介紹Xcode項(xiàng)目模板代碼中找到。對(duì)于完整的示例代碼,使用增強(qiáng)現(xiàn)實(shí)模板創(chuàng)建新的iOS應(yīng)用程序,并從“內(nèi)容技術(shù)”彈出菜單中選擇“金屬”菜單。
Get Video Frames and Tracking Data from the Session
創(chuàng)建和維護(hù)自己arsession實(shí)例,并運(yùn)行一個(gè)會(huì)話配置,該會(huì)話配置適合于要支持的AR體驗(yàn)。(要做到這一點(diǎn),請(qǐng)參見建立一個(gè)基本的AR體驗(yàn))會(huì)話捕獲攝像機(jī)的視頻,跟蹤設(shè)備的位置和方向在一個(gè)模擬的3D空間,并提供arframe物體.每一個(gè)這樣的對(duì)象都包含一個(gè)單獨(dú)的視頻幀圖像和從幀被捕獲的時(shí)刻的位置跟蹤信息。有兩種訪問方式arframe AR會(huì)話產(chǎn)生的對(duì)象,取決于您的應(yīng)用程序是否支持拉或推設(shè)計(jì)模式。
如果你喜歡控制幀定時(shí)(拉設(shè)計(jì)模式),使用會(huì)話的幀屬性獲取當(dāng)前幀圖像和跟蹤信息每次重畫視圖的內(nèi)容。使用這種方法的arkit Xcode模板:
// in Renderer class, called from MTKViewDelegate.draw(in:) via Renderer.update()
func updateGameState(){
guardletcurrentFrame = session.currentFrameelse{return}? ? ? ? updateSharedUniforms(frame: currentFrame)? ?
?updateAnchors(frame: currentFrame)? ??
updateCapturedImageTextures(frame: currentFrame)
if viewportSizeDidChange {? ? ? ?
?viewportSizeDidChange =false
?updateImagePlane(frame: currentFrame)? ??
}
}
另外,如果您的應(yīng)用程序設(shè)計(jì)有利于推模式,實(shí)現(xiàn)會(huì)議:didupdateframe:委托方法,該會(huì)話將為它捕獲的每個(gè)視頻幀調(diào)用一次(默認(rèn)為每秒60幀)。
在獲得一個(gè)框架,你需要繪制相機(jī)圖像,并更新和渲染任何內(nèi)容覆蓋你的AR經(jīng)驗(yàn)包括。
Draw the Camera Image
創(chuàng)建和維護(hù)自己arsession實(shí)例,并運(yùn)行一個(gè)會(huì)話配置,該會(huì)話配置適合于要支持的AR體驗(yàn)。(要做到這一點(diǎn),請(qǐng)參見建立一個(gè)基本的AR體驗(yàn))會(huì)話捕獲攝像機(jī)的視頻,跟蹤設(shè)備的位置和方向在一個(gè)模擬的3D空間,并提供arframe物體.每一個(gè)這樣的對(duì)象都包含一個(gè)單獨(dú)的視頻幀圖像和從幀被捕獲的時(shí)刻的位置跟蹤信息。
有兩種訪問方式arframe AR會(huì)話產(chǎn)生的對(duì)象,取決于您的應(yīng)用程序是否支持拉或推設(shè)計(jì)模式。
如果你喜歡控制幀定時(shí)(拉設(shè)計(jì)模式),使用會(huì)話的幀屬性獲取當(dāng)前幀圖像和跟蹤信息每次重畫視圖的內(nèi)容。使用這種方法的arkit Xcode模板:
// in Renderer class, called from MTKViewDelegate.draw(in:) via Renderer.update()
func updateGameState(){
guardletcurrentFrame = session.currentFrameelse{return}
updateSharedUniforms(frame: currentFrame)
updateAnchors(frame: currentFrame)
updateCapturedImageTextures(frame: currentFrame)
if viewportSizeDidChange {
viewportSizeDidChange =false
updateImagePlane(frame: currentFrame)
}
}
Draw the Camera Image
每個(gè)arframe對(duì)象的capturedimage屬性包含從設(shè)備照相機(jī)捕獲的像素緩沖區(qū)。若要將此圖像作為自定義視圖的背景繪制,則需要從圖像內(nèi)容創(chuàng)建紋理并提交使用這些紋理的GPU渲染命令。
像素緩沖區(qū)的內(nèi)容是biplanar YCbCr編碼(也稱為YUV)數(shù)據(jù)格式;渲染的圖像就需要將像素?cái)?shù)據(jù)的圖像的RGB格式。用金屬渲染,可以在GPU著色器代碼中最有效地執(zhí)行轉(zhuǎn)換。API的使用cvmetaltexturecache創(chuàng)建從像素緩沖區(qū)一個(gè)緩沖的亮度兩金屬材質(zhì)(Y)和色度(CbCr)飛機(jī):
func updateCapturedImageTextures(frame: ARFrame){/
/ Create two textures (Y and CbCr) from the provided frame's captured imageletpixelBuffer = frame.capturedImage
if(CVPixelBufferGetPlaneCount(pixelBuffer) <2) {return}
capturedImageTextureY = createTexture(fromPixelBuffer: pixelBuffer, pixelFormat:.r8Unorm, planeIndex:0)!
capturedImageTextureCbCr = createTexture(fromPixelBuffer: pixelBuffer, pixelFormat:.rg8Unorm, planeIndex:1)!
}
func createTexture(fromPixelBuffer pixelBuffer: CVPixelBuffer, pixelFormat: MTLPixelFormat, planeIndex: Int)->MTLTexture? {
varmtlTexture:MTLTexture? =nilletwidth =CVPixelBufferGetWidthOfPlane(pixelBuffer, planeIndex)letheight =CVPixelBufferGetHeightOfPlane(pixelBuffer, planeIndex)vartexture:CVMetalTexture? =nilletstatus =CVMetalTextureCacheCreateTextureFromImage(nil, capturedImageTextureCache, pixelBuffer,nil, pixelFormat, width, height, planeIndex, &texture)ifstatus == kCVReturnSuccess {
mtlTexture =CVMetalTextureGetTexture(texture!)
}returnmtlTexture
}
其次,編碼渲染命令,畫兩個(gè)紋理使用分段函數(shù)執(zhí)行YCbCr到RGB轉(zhuǎn)換顏色的變換矩陣:
fragment float4 capturedImageFragmentShader(ImageColorInOut in [[stage_in]],
texture2d capturedImageTextureY [[ texture(kTextureIndexY) ]],
texture2d capturedImageTextureCbCr [[ texture(kTextureIndexCbCr) ]]) {
constexpr sampler colorSampler(mip_filter::linear,
mag_filter::linear,
min_filter::linear);
const float4x4 ycbcrToRGBTransform = float4x4(
float4(+1.164380f, +1.164380f, +1.164380f, +0.000000f),
float4(+0.000000f, -0.391762f, +2.017230f, +0.000000f),
float4(+1.596030f, -0.812968f, +0.000000f, +0.000000f),
float4(-0.874202f, +0.531668f, -1.085630f, +1.000000f)
);
// Sample Y and CbCr textures to get the YCbCr color at the given texture coordinate
float4 ycbcr = float4(capturedImageTextureY.sample(colorSampler, in.texCoord).r,
capturedImageTextureCbCr.sample(colorSampler, in.texCoord).rg, 1.0);
// Return converted RGB color
return ycbcrToRGBTransform * ycbcr;
}
Note
使用displaytransformwithviewportsize:定位:方法確保相機(jī)圖像覆蓋整個(gè)視圖。對(duì)于這種方法的例子,以及完整的金屬管道安裝代碼,看到完整的Xcode模板。(創(chuàng)建一個(gè)新的iOS應(yīng)用程序的增強(qiáng)現(xiàn)實(shí)模板,并選擇金屬從內(nèi)容技術(shù)彈出菜單。)
Track and Render Overlay Content
AR的經(jīng)驗(yàn)通常側(cè)重于渲染3D覆蓋內(nèi)容,使內(nèi)容似乎是在相機(jī)圖像中看到的真實(shí)世界的一部分。為了實(shí)現(xiàn)這種錯(cuò)覺,使用aranchor類來模擬你自己3D內(nèi)容相對(duì)于真實(shí)世界空間的位置和方向。錨提供轉(zhuǎn)換,您可以在渲染過程中引用。
例如,Xcode模板創(chuàng)建一個(gè)錨位于設(shè)備前端約20厘米,每當(dāng)用戶點(diǎn)擊屏幕
func handleTap(gestureRecognize: UITapGestureRecognizer){
// Create anchor using the camera's current position
if letcurrentFrame = session.currentFrame {// Create a transform with a translation of 0.2 meters in front of the cameravartranslation = matrix_identity_float4x4
translation.columns.3.z = -0.2lettransform = simd_mul(currentFrame.camera.transform, translation)// Add a new anchor to the sessionletanchor =ARAnchor(transform: transform)
session.add(anchor: anchor)
}
}