ARKit:平面追蹤與坐標(biāo)轉(zhuǎn)換

上文說到AR的一些基礎(chǔ)內(nèi)容,這篇我們就來把這個demo走一遍,在做的過程中蝇闭,補(bǔ)充一些ARKit里常用的方法,這樣應(yīng)該會容易理解一些硬毕。

本文demo鏈接

1呻引、Demo的基本思路

用ARKit追蹤地板的平面。取首次追蹤到的平面作為參考平面吐咳,此時這個平面與地面大致是重合的(這里讀者也可以自己通過一些方法提高精準(zhǔn)度逻悠,筆者就不再繼續(xù)深入了)。

取現(xiàn)實(shí)中的地面上的點(diǎn)韭脊,得到它在我們的平面上的坐標(biāo)童谒,通過坐標(biāo)計算距離。

2乾蓬、追蹤平面

在上一篇demo上(沒看上一篇的可以不看直接創(chuàng)建一個項(xiàng)目惠啄,template選argumented reality app就好了)繼續(xù)。

我們先在viewDidLoad里設(shè)置一下debugOptions任内,作用是顯示點(diǎn)位撵渡,方便后面看追蹤情況。

self.sceneView.debugOptions = ARSCNDebugOptionShowFeaturePoints;//顯示追蹤到的點(diǎn)位

追蹤錨點(diǎn)的代理方法:- (void)renderer:(id)renderer didAddNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor;死嗦,沒有設(shè)置代理的先設(shè)置一下代理

#pragma mark - ARSCNViewDelegate

//添加節(jié)點(diǎn)時候調(diào)用(當(dāng)開啟平地捕捉模式之后趋距,如果捕捉到平地,ARKit會自動添加一個平地節(jié)點(diǎn))

-(void)renderer:(id)renderer didAddNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor{

if ([anchor isMemberOfClass:[ARAnchor class]]) {

NSLog(@"平地捕捉");

}

// 添加一個3D平面模型越除,ARKit只有捕捉能力节腐,錨點(diǎn)只是一個空間位置,要想更加清楚看到這個空間摘盆,我們需要給空間添加一個平地的3D模型來渲染他

ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;

//放一個box翼雀,長寬為錨點(diǎn)平面的長寬

SCNBox *plane = [SCNBox boxWithWidth:planeAnchor.extent.x height:0.001 length:planeAnchor.extent.z chamferRadius:0];

SCNMaterial *transparentMaterial = [SCNMaterial new];

transparentMaterial = [self materialNamed:@"tron"];

plane.materials = @[transparentMaterial, transparentMaterial, transparentMaterial, transparentMaterial, transparentMaterial, transparentMaterial];

SCNNode *planeNode = [SCNNode nodeWithGeometry:plane];

planeNode.position =SCNVector3Make(planeAnchor.center.x, 0, planeAnchor.center.z);

[node addChildNode:planeNode];

//為了方便看,在錨點(diǎn)處放一個“坐標(biāo)系”

SCNTube *tubey = [SCNTube tubeWithInnerRadius:0 outerRadius:0.001 height:5];

tubey.firstMaterial.diffuse.contents = [UIColor blackColor];

SCNNode *tubeNodey = [SCNNode nodeWithGeometry:tubey];

tubeNodey.position = SCNVector3Make(0, 2.5, 0);

[node addChildNode:tubeNodey];

SCNTube *tubex = [SCNTube tubeWithInnerRadius:0 outerRadius:0.001 height:5];

tubex.firstMaterial.diffuse.contents = [UIColor whiteColor];

SCNNode *tubeNodex = [SCNNode nodeWithGeometry:tubex];

tubeNodex.position =? SCNVector3Make(0, 0, 0);

tubeNodex.rotation = SCNVector4Make(1, 0, 0, M_PI/2);

[node addChildNode:tubeNodex];

SCNTube *tubez = [SCNTube tubeWithInnerRadius:0 outerRadius:0.001 height:5];

tubez.firstMaterial.diffuse.contents = [UIColor grayColor];

SCNNode *tubeNodez = [SCNNode nodeWithGeometry:tubez];

tubeNodez.position =? SCNVector3Make(0, 0, 0);

tubeNodez.rotation = SCNVector4Make(0, 0, 1, M_PI/2);

[node addChildNode:tubeNodez];

_zeroNode = [[SCNNode alloc]init];

_zeroNode = node;

//捕捉到第一個平面之后關(guān)閉捕捉

self.arConfiguration.planeDetection = ARPlaneDetectionNone;

[self.sceneView.session runWithConfiguration:self.arConfiguration];

//添加第一根隨著屏幕移動的桿子

[self handleTap:tapGesture];

// [self insertGeometry];

}

//材質(zhì)修改孩擂,具體可以看這篇http://www.reibang.com/p/07b96c800a1d

- (SCNMaterial *)materialNamed:(NSString *)name {

NSMutableDictionary *materials = [NSMutableDictionary new];

SCNMaterial *mat = materials[name];

if (mat) {

return mat;

}

mat = [SCNMaterial new];

mat.lightingModelName = SCNLightingModelPhysicallyBased;

mat.diffuse.contents = [UIImage imageNamed:@"tron-albedo"];

mat.roughness.contents = [UIImage imageNamed:@"tron-albedo"];

mat.metalness.contents = [UIImage imageNamed:@"tron-albedo"];

mat.normal.contents = [UIImage imageNamed:@"tron-albedo"];

mat.diffuse.wrapS = SCNWrapModeRepeat;

mat.diffuse.wrapT = SCNWrapModeRepeat;

mat.roughness.wrapS = SCNWrapModeRepeat;

mat.roughness.wrapT = SCNWrapModeRepeat;

mat.metalness.wrapS = SCNWrapModeRepeat;

mat.metalness.wrapT = SCNWrapModeRepeat;

mat.normal.wrapS = SCNWrapModeRepeat;

mat.normal.wrapT = SCNWrapModeRepeat;

materials[name] = mat;

return mat;

}


這里是個坑狼渊,一定要選ARHitTestResultTypeExistingPlane這個,并且關(guān)掉追蹤类垦。

- (NSArray*)hitTest:(CGPoint)point types:(ARHitTestResultType)types;此方法會返回空值狈邑,返回空值的原因:

ARHitTestResultTypeExistingPlane:手機(jī)晃動導(dǎo)致無法確定此平面與重力相垂直

ARHitTestResultTypeExistingPlaneUsingExtent:觸碰到點(diǎn)在平面之外,則不返回值蚤认。

ARHitTestResultTypeEstimatedHorizontalPlane:正常情況一定會返回值米苹。

#pragma mark - handleTap

- (void) handleTap:(UIGestureRecognizer*)gestureRecognize{CGPoint tapPoint = CGPointMake(redPoint.center.x, redPoint.center.y);NSArray*result = [self.sceneView hitTest:tapPoint types:ARHitTestResultTypeExistingPlane];

if (result.count == 0) {

return;

}

// If there are multiple hits, just pick the closest plane

ARHitTestResult * hitResult = [result firstObject];

SCNTube *calculareTube = [SCNTube tubeWithInnerRadius:0 outerRadius:0.01 height:1];

calculareTube.firstMaterial.diffuse.contents = [UIColor colorWithRed:0 green:255 blue:0 alpha:0.6];

_calculateNode = [SCNNode nodeWithGeometry:calculareTube ];

// 設(shè)置節(jié)點(diǎn)的位置為捕捉到的平地的錨點(diǎn)的中心位置? SceneKit框架中節(jié)點(diǎn)的位置position是一個基于3D坐標(biāo)系的矢量坐標(biāo)SCNVector3Make

_calculateNode.position =? SCNVector3Make(

hitResult.worldTransform.columns[3].x,

hitResult.worldTransform.columns[3].y+0.5,

hitResult.worldTransform.columns[3].z

);

[self.sceneView.scene.rootNode addChildNode:_calculateNode];

if (cubeNumber > 0) {

SCNNode *previousNode;

previousNode = self.sceneView.scene.rootNode.childNodes[self.sceneView.scene.rootNode.childNodes.count-2];

previousNode.geometry.firstMaterial.diffuse.contents = [UIColor greenColor];

if (cubeNumber>1) {

SCNNode *currentNode;

currentNode = self.sceneView.scene.rootNode.childNodes[self.sceneView.scene.rootNode.childNodes.count-3];

currentNode.geometry.firstMaterial.diffuse.contents = [UIColor redColor];

if (cubeNumber>2) {

SCNNode *oldNode;

oldNode = self.sceneView.scene.rootNode.childNodes[self.sceneView.scene.rootNode.childNodes.count-4];

oldNode.geometry.firstMaterial.diffuse.contents = [UIColor whiteColor];

}

[self calculateDistance];

}

}

cubeNumber += 1;

}


//讓透明的桿子隨著攝像頭移動在捕捉到的平面上動起來

#pragma mark - ARSessionDelegate

- (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame{

if (cubeNumber == 0)? return;

CGPoint tapPoint = CGPointMake(redPoint.center.x, redPoint.center.y);NSArray*result = [self.sceneView hitTest:tapPoint types:ARHitTestResultTypeExistingPlane];

if (result.count == 0) {

return;

}

// If there are multiple hits, just pick the closest plane

ARHitTestResult * hitResult = [result firstObject];

SCNNode *currentNode;

currentNode = [self.sceneView.scene.rootNode.childNodes lastObject];

// 設(shè)置節(jié)點(diǎn)的位置為捕捉到的平地的錨點(diǎn)的中心位置? SceneKit框架中節(jié)點(diǎn)的位置position是一個基于3D坐標(biāo)系的矢量坐標(biāo)SCNVector3Make

currentNode.position =? SCNVector3Make(

hitResult.worldTransform.columns[3].x,

hitResult.worldTransform.columns[3].y+0.5,

hitResult.worldTransform.columns[3].z

);

}

//計算距離

-(void)calculateDistance{

CGFloat distance;

SCNNode *previousNode;

previousNode = self.sceneView.scene.rootNode.childNodes[self.sceneView.scene.rootNode.childNodes.count-3];

SCNNode *currentNode;

currentNode = self.sceneView.scene.rootNode.childNodes[self.sceneView.scene.rootNode.childNodes.count-2];

distance = sqrt((previousNode.position.x-currentNode.position.x)*(previousNode.position.x-currentNode.position.x)+(previousNode.position.z-currentNode.position.z)*(previousNode.position.z-currentNode.position.z));

distanceLab.text = [NSString stringWithFormat:@"上兩點(diǎn)距離:%f 米",distance];

}

//手勢添加桿子方法

#pragma mark - handleTap

- (void) handleTap:(UIGestureRecognizer*)gestureRecognize{

CGPoint tapPoint = CGPointMake(redPoint.center.x, redPoint.center.y);NSArray*result = [self.sceneView hitTest:tapPoint types:ARHitTestResultTypeExistingPlane];

if (result.count == 0) {

return;

}

// If there are multiple hits, just pick the closest plane

ARHitTestResult * hitResult = [result firstObject];

SCNTube *calculareTube = [SCNTube tubeWithInnerRadius:0 outerRadius:0.01 height:1];

calculareTube.firstMaterial.diffuse.contents = [UIColor colorWithRed:0 green:255 blue:0 alpha:0.6];

_calculateNode = [SCNNode nodeWithGeometry:calculareTube ];

// 設(shè)置節(jié)點(diǎn)的位置為捕捉到的平地的錨點(diǎn)的中心位置? SceneKit框架中節(jié)點(diǎn)的位置position是一個基于3D坐標(biāo)系的矢量坐標(biāo)SCNVector3Make

_calculateNode.position =? SCNVector3Make(

hitResult.worldTransform.columns[3].x,

hitResult.worldTransform.columns[3].y+0.5,

hitResult.worldTransform.columns[3].z

);

[self.sceneView.scene.rootNode addChildNode:_calculateNode];

if (cubeNumber > 0) {

SCNNode *previousNode;

previousNode = self.sceneView.scene.rootNode.childNodes[self.sceneView.scene.rootNode.childNodes.count-2];

previousNode.geometry.firstMaterial.diffuse.contents = [UIColor greenColor];

if (cubeNumber>1) {

SCNNode *currentNode;

currentNode = self.sceneView.scene.rootNode.childNodes[self.sceneView.scene.rootNode.childNodes.count-3];

currentNode.geometry.firstMaterial.diffuse.contents = [UIColor redColor];

if (cubeNumber>2) {

SCNNode *oldNode;

oldNode = self.sceneView.scene.rootNode.childNodes[self.sceneView.scene.rootNode.childNodes.count-4];

oldNode.geometry.firstMaterial.diffuse.contents = [UIColor whiteColor];

}

[self calculateDistance];

}

}

cubeNumber += 1;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市砰琢,隨后出現(xiàn)的幾起案子蘸嘶,更是在濱河造成了極大的恐慌良瞧,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件亏较,死亡現(xiàn)場離奇詭異莺褒,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)雪情,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門遵岩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人巡通,你說我怎么就攤上這事尘执。” “怎么了宴凉?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵誊锭,是天一觀的道長。 經(jīng)常有香客問我弥锄,道長丧靡,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任籽暇,我火速辦了婚禮温治,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘戒悠。我一直安慰自己熬荆,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布绸狐。 她就那樣靜靜地躺著卤恳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪寒矿。 梳的紋絲不亂的頭發(fā)上突琳,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機(jī)與錄音符相,去河邊找鬼拆融。 笑死,一個胖子當(dāng)著我的面吹牛主巍,可吹牛的內(nèi)容都是我干的冠息。 我是一名探鬼主播挪凑,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼孕索,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了躏碳?” 一聲冷哼從身側(cè)響起搞旭,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后肄渗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體镇眷,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年翎嫡,在試婚紗的時候發(fā)現(xiàn)自己被綠了欠动。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡惑申,死狀恐怖具伍,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情圈驼,我是刑警寧澤人芽,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站绩脆,受9級特大地震影響萤厅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜靴迫,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一惕味、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧矢劲,春花似錦赦拘、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至丸逸,卻和暖如春蹋艺,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背黄刚。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工捎谨, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人憔维。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓涛救,卻偏偏與公主長得像,于是被迫代替她去往敵國和親业扒。 傳聞我的和親對象是個殘疾皇子检吆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評論 2 355

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