ARSCNView

A view for displaying AR experiences that augment the camera view with 3D SceneKit content.
顯示使用3D SceneKit內容增強相機視圖的AR體驗的視圖。


Overview

The?ARSCNView?class provides the easiest way to create augmented reality experiences that blend virtual 3D content with a device camera view of the real world. When you run the view's provided?ARSession?object:

The view automatically renders the live video feed from the device camera as the scene background.

The world coordinate system of the view's SceneKit scene directly responds to the AR world coordinate system established by the session configuration.

The view automatically moves its SceneKit camera to match the real-world movement of the device.

Because ARKit automatically matches SceneKit space to the real world, placing a virtual object such that it appears to maintain a real-world position requires only setting that object's SceneKit position appropriately. (See?Providing 3D Virtual Content with SceneKit.)

You don't necessarily need to use the?ARAnchor?class to track positions of objects you add to the scene, but by implementing?ARSCNViewDelegate?methods, you can add SceneKit content to any anchors that are automatically detected by ARKit.

概述

ARSCNView類提供了最簡單的方法來創建增強現實體驗,將虛擬3D內容與真實世界的設備相機視圖融合在一起。當你運行視圖的提供的ARSession對象時:

該視圖自動將設備攝像機的實時視頻饋送呈現為場景背景。

視圖的SceneKit場景的世界坐標系直接響應由Session配置建立的AR世界坐標系。

該視圖會自動移動其SceneKit相機以匹配設備的實際移動。

由于ARKit會自動將SceneKit空間與現實世界相匹配,因此放置虛擬對象以使其看起來保持真實世界的位置僅需要適當地設置該對象的SceneKit位置。 (請參閱使用SceneKit提供3D虛擬內容。)

您不一定需要使用ARAnchor類來跟蹤添加到場景中的對象的位置,但通過實現ARSCNViewDelegate方法,可以將SceneKit內容添加到ARKit自動檢測到的任何錨點。


? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?First Steps


Providing 3D Virtual Content with SceneKit
Use SceneKit to add realistic three-dimensional objects to your AR experience.
使用SceneKit為您的AR體驗添加逼真的三維物體。

Overview

Because ARKit automatically matches SceneKit space to the real world, placing a virtual object so that it appears to maintain a real-world position requires that you set the object's SceneKit position appropriately. For example, in a default configuration, the following code places a 10-centimeter cube 20 centimeters in front of the camera's initial position:

概述

由于ARKit自動將SceneKit空間與現實世界相匹配,放置虛擬對象以使其看起來保持真實世界的位置,因此需要適當地設置對象的SceneKit位置。 例如,在默認配置下,以下代碼在攝像機初始位置的前面放置一個距離攝像機20厘米20厘米的10厘米立方體:


The code above places an object directly in the view’s SceneKit scene. The object automatically appears to track a real-world position because ARKit matches SceneKit space to real-world space.

Alternatively, you can use the?ARAnchor?class to track real-world positions, either by creating anchors yourself and adding them to the session or by observing anchors that ARKit automatically creates. For example, when plane detection is enabled, ARKit adds and updates anchors for each detected plane. To add visual content for these anchors, implement?ARSCNViewDelegate?methods such as the following:

上面的代碼將一個對象直接放置在視圖的SceneKit場景中。 該對象自動顯示為跟蹤真實世界的位置,因為ARKit將SceneKit空間與實際空間相匹配。

或者,您可以使用ARAnchor類來跟蹤真實世界的位置,既可以通過自己創建錨點并將其添加到會話中,也可以通過觀察ARKit自動創建的錨點來實現。 例如,啟用平面檢測功能后,ARKit會為每個檢測到的平面添加并更新錨點。 要為這些錨點添加可視內容,請實施ARSCNViewDelegate方法,如下所示:


Follow Best Practices for Designing 3D Assets?

Use the SceneKit physically based lighting model for materials for a more realistic appearance. (See the?SCNMaterial?class and the?Badger: Advanced Rendering in SceneKit?sample code project.)

Bake ambient occlusion shading so that objects appear properly lit in a wide variety of scene lighting conditions.

If you create a virtual object that you intend to place on a real-world flat surface in AR, include a transparent plane with a soft shadow texture below the object in your 3D asset.

遵循設計3D Asset的最佳實踐

將SceneKit基于物理的照明模型用于材料以獲得更逼真的外觀。 (請參閱SceneKit示例代碼項目中的SCNMaterial類和Badger:高級渲染。)

烘烤環境遮擋陰影,使物體在各種場景照明條件下正常點亮。

如果您創建了一個虛擬對象,您打算將其放置在AR中的真實世界平面上,請在3D素材資源中的對象下方包含一個透明平面,并在其中包含柔和陰影紋理。

session
The AR session that manages motion tracking and camera image processing for the view's contents.
AR Session管理視圖內容的運動跟蹤和攝像頭圖像處理。

Discussion

A view creates its own session object; use this property to access and configure the view's session.

討論

視圖創建自己的Session對象; 使用此屬性來訪問和配置視圖的Session。

scene
The SceneKit scene to be displayed in the view.
SceneKit場景將顯示在視圖中。

Discussion

Note

Unlike the parent?SCNView?class, an?ARSCNView?object requires a non-nil?scene to display.

討論

注意

與父SCNView類不同,ARSCNView對象需要顯示非零場景。


? ? ? ? ? ? ? ? ? ? ? Responding to AR Updates


delegate
An object you provide to mediate synchronization of the view's AR scene information with SceneKit content.
您提供的一個對象,用于調解視圖的AR場景信息與SceneKit內容的同步

ARSCNViewDelegate
Methods you can implement to mediate the automatic synchronization of SceneKit content with an AR session.
您可以實現的方法來調解SceneKit內容與AR Session的自動同步。

Overview

Implement this protocol to provide SceneKit content corresponding to?ARAnchor?objects tracked by the view's AR session, or to manage the view's automatic updating of such content.

This protocol extends the?ARSessionObserver?protocol, so your session delegate can also implement those methods to respond to changes in session status.

概述

實現此協議以提供與由視圖的ARSession跟蹤的ARAnchor對象相對應的SceneKit內容,或管理視圖對此類內容的自動更新。

這個協議擴展了ARSessionObserver協議,所以你的會話委托也可以實現這些方法來響應會話狀態的變化。

Handling Content Updates


renderer:nodeForAnchor:
Asks the delegate to provide a SceneKit node corresponding to a newly added anchor.
渲染:nodeForAnchor:請求委托提供與新添加的錨相對應的SceneKit節點。

Parameters

renderer
The?ARSCNView?object rendering the scene.
ARSCNView對象呈現場景。

anchor
The anchor for which a node is requested.
請求節點的錨點。

Return Value

A new SceneKit node, which ARKit will add to the scene and update to follow its corresponding AR anchor.

一個新的SceneKit節點,ARKit將添加到場景并更新以跟隨其相應的AR定位點。

Discussion

Depending on the session configuration, ARKit may automatically add anchors to a session. ARKit also calls this method to provide visual content for any?ARAnchor?objects you manually add using the session's?addAnchor:?method.

You can implement this method to provide a new?SCNNode?object (or instance of an?SCNNodesubclass) containing any attachments you plan to use as a visual representation of the anchor. Note that ARKit controls the node's visibility and its?transform?property, so you may find it useful to add child nodes or adjust the node's?pivot?property to maintain any changes to position or orientation that you make.

If you return?nil?from this method, no node is added to the scene.

Alternatively, if you do not implement this method, ARKit creates an empty node, and you can implement the?renderer:didAddNode:forAnchor:?method instead to provide visual content by attaching it to that node.

討論

根據Session配置的不同,ARKit可能會自動將錨定添加到Session中。 ARKit還調用此方法為使用Session的addAnchor:方法手動添加的任何ARAnchor對象提供可視內容。

您可以實現此方法來提供一個新的SCNNode對象(或SCNNode子類的實例),其中包含您打算用作錨的可視表示的附件。 請注意,ARKit控制節點的可見性及其變換屬性,所以您可能會發現添加子節點或調整節點的透視屬性以保持對所做位置或方向的任何更改很有用。

如果您從此方法返回nil,則不會將節點添加到場景中。

或者,如果您未實現此方法,ARKit將創建一個空節點,并且您可以實現渲染器:didAddNode:forAnchor:方法,以通過將其附加到該節點來提供可視內容。

renderer:didAddNode:forAnchor:
Tells the delegate that a SceneKit node corresponding to a new AR anchor has been added to the scene.
告訴委托人已將與新的AR錨相對應的SceneKit節點添加到場景中。

Parameters

renderer
The?ARSCNView?object rendering the scene.
ARSCNView對象呈現場景。

node
The newly added SceneKit node.
新添加的SceneKit節點。

anchor
The AR anchor corresponding to the node.
AR節點對應的AR節點。

Discussion

Depending on the session configuration, ARKit may automatically add anchors to a session. The view calls this method once for each new anchor. ARKit also calls this method to provide visual content for any?ARAnchor?objects you manually add using the session's?addAnchor:?method.

You can provide visual content for the anchor by attaching geometry (or other SceneKit features) to this node or by adding child nodes.

Alternatively, you can implement the?renderer:nodeForAnchor:?method to create your own node (or instance of an?SCNNode?subclass) for an anchor.

討論

根據Session配置的不同,ARKit可能會自動將錨定添加到Sessopm中。 該視圖為每個新的錨點調用一次該方法。 ARKit還調用此方法為使用Session的addAnchor:方法手動添加的任何ARAnchor對象提供可視內容。

您可以通過將幾何圖元(或其他SceneKit特征)附加到此節點或通過添加子節點來為錨點提供可視內容。

或者,您可以實現渲染器:nodeForAnchor:方法為錨點創建自己的節點(或SCNNode子類的實例)。

renderer:willUpdateNode:forAnchor:
Tells the delegate that a SceneKit node's properties will be updated to match the current state of its corresponding anchor.
告訴委托人SceneKit節點的屬性將被更新以匹配其相應錨的當前狀態。Parameters

Parameters

renderer
TheARSCNViewobject rendering the scene.
ARSCNView對象呈現場景。

node
The newly added SceneKit node.
新添加的SceneKit節點。

anchor
The AR anchor corresponding to the node.
AR節點對應的AR節點

Discussion

Depending on the session configuration, ARKit may automatically update anchors in a session. The view calls this method once for each updated anchor.

討論

根據Session配置的不同,ARKit可能會自動更新會話中的錨點。 該視圖為每個更新的錨點調用一次該方法。

renderer:didUpdateNode:forAnchor:
Tells the delegate that a SceneKit node's properties have been updated to match the current state of its corresponding anchor.
告訴委托人SceneKit節點的屬性已更新以匹配其相應錨的當前狀態。

Parameters

renderer
TheARSCNViewobject rendering the scene.
ARSCNView對象呈現場景。

node
The newly added SceneKit node.
新添加的SceneKit節點。

anchor
The AR anchor corresponding to the node.
AR節點對應的AR節點

Discussion

Depending on the session configuration, ARKit may automatically update anchors in a session. The view calls this method once for each updated anchor.

討論

根據Session配置的不同,ARKit可能會自動更新Session中的錨點。 該視圖為每個更新的錨點調用一次該方法。

renderer:didRemoveNode:forAnchor:
Tells the delegate that the SceneKit node corresponding to a removed AR anchor has been removed from the scene.
告訴委托人已經從場景中移除了與已移除的AR錨點對應的SceneKit節點。

Parameters

renderer
TheARSCNViewobject rendering the scene.
ARSCNView對象呈現場景。

node
The newly added SceneKit node.
新添加的SceneKit節點。

anchor
The AR anchor corresponding to the node.
AR節點對應的AR節點

Discussion

Depending on the session configuration, ARKit may automatically remove anchors from a session. The view calls this method once for each removed anchor.

討論

根據Session配置的不同,ARKit可能會自動從Session中刪除錨點。 該視圖為每個已移除的錨點調用一次該方法。

Inherits From?ARSessionObserver, ?SCNSceneRendererDelegate


? ? ? ? ? ? ? ? ?Hit Testing for Real-World Surfaces


-?hitTest:types:
Searches for real-world objects or AR anchors in the captured camera image corresponding to a point in the SceneKit view.
在捕獲的攝像機圖像中搜索與SceneKit視圖中的點相對應的真實世界對象或AR錨。

Parameters

point
A point in the 2D coordinate system of the view.
視圖2D坐標系中的一個點。

types
The types of hit-test result to search for.
要搜索的命中測試結果的類型。

Return Value

A list of results, sorted from nearest to farthest (in distance from the camera).

返回值

結果列表,從最近到最遠(與相機的距離)排序。

Discussion

Hit testing searches for real-world objects or surfaces detected through the AR session's processing of the camera image. A 2D point in the view's coordinate system can refer to any point along a 3D line that starts at the device camera and extends in a direction determined by the device orientation and camera projection. This method searches along that line, returning all objects that intersect it in order of distance from the camera.

討論

命中測試搜索通過ARSession處理相機圖像檢測到的真實世界對象或表面。 視圖坐標系中的二維點可以指沿著三維線上的任意點,該三維線始于設備相機,并沿著由設備方向和相機投影確定的方向延伸。 該方法沿著該線搜索,按距離相機的順序返回與其相交的所有對象。

Note

This method searches for AR anchors and real-world objects detected by the AR session, not SceneKit content displayed in the view. To search for SceneKit objects, use the view's?hitTest:options:?method instead.

注意

此方法搜索由ARSession檢測到的AR錨點和真實世界對象,而不是視圖中顯示的SceneKit內容。 要搜索SceneKit對象,請改為使用視圖的hitTest:options:方法。


? ? ? ? ? ? ?Mapping Content to Real-World Positions


-?anchorForNode:
Returns the AR anchor associated with the specified SceneKit node, if any.
返回與指定的SceneKit節點關聯的AR定位點(如果有)。

Parameters

node
A SceneKit node in the view's scene.
場景中的SceneKit節點。

Return Value

The?ARAnchor?object tracking the node, or?nil?if the node is not associated with an anchor or not in the view's scene.

返回值

ARAnchor對象跟蹤節點,或者如果節點未與視圖的場景中的錨點關聯,則為零。

-?nodeForAnchor:
Returns the SceneKit node associated with the specified AR anchor, if any.
返回與指定的AR錨點相關的SceneKit節點(如果有的話)。

Parameters

anchor
An anchor in the view's AR session.
視圖的ARSession中的一個錨點。

Return Value

The node whose position in the AR scene the anchor tracks, or?nil?if the anchor has no associated node or is not in the view's AR session.

返回值

在AR場景中錨點跟蹤的節點,或者如果錨點沒有關聯節點或不在視圖的ARSession中,則為零。


? ? ? ? ? ? ? ? ? ? ? ?Managing Scene Lighting


automaticallyUpdatesLighting
A Boolean value that specifies whether ARKit creates and updates SceneKit lights in the view's scene.
一個布爾值,指定ARKit是否在視圖的場景中創建并更新SceneKit指示燈。

Discussion

If this value is?YES?(the default), the view automatically creates one or more?SCNLight?objects, adds them to the scene, and updates their properties to reflect estimated lighting information from the camera scene. Set this value to?NO?if you want to directly control all lighting in the SceneKit scene.

討論

如果此值為YES(默認值),視圖會自動創建一個或多個SCNLight對象,將它們添加到場景中,并更新其屬性以反映來自相機場景的估計光照信息。 如果要直接控制SceneKit場景中的所有照明,請將此值設置為NO。


? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Debugging AR Display


ARSCNDebugOptions
Options for drawing overlay content to aid debugging of AR tracking in a SceneKit view.
用于繪制疊加內容的選項,以幫助在SceneKit視圖中調試AR追蹤。

Discussion

To use these debugging overlays, add them to the option set in the view's?debugOptionsproperty (inherited from the?SCNView?class).

討論

要使用這些調試覆蓋圖,請將它們添加到視圖的debugOptions屬性中的選項集(從SCNView類繼承)。


Inherits From ?SCNView

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

推薦閱讀更多精彩內容