A view for displaying AR experiences that augment the camera view with 3D SceneKit content.
顯示使用3D SceneKit內(nèi)容增強(qiáng)相機(jī)視圖的AR體驗(yàn)的視圖栋荸。
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類提供了最簡(jiǎn)單的方法來(lái)創(chuàng)建增強(qiáng)現(xiàn)實(shí)體驗(yàn)嫁盲,將虛擬3D內(nèi)容與真實(shí)世界的設(shè)備相機(jī)視圖融合在一起。當(dāng)你運(yùn)行視圖的提供的ARSession對(duì)象時(shí):
該視圖自動(dòng)將設(shè)備攝像機(jī)的實(shí)時(shí)視頻饋送呈現(xiàn)為場(chǎng)景背景楚里。
視圖的SceneKit場(chǎng)景的世界坐標(biāo)系直接響應(yīng)由Session配置建立的AR世界坐標(biāo)系刻蟹。
該視圖會(huì)自動(dòng)移動(dòng)其SceneKit相機(jī)以匹配設(shè)備的實(shí)際移動(dòng)糕非。
由于ARKit會(huì)自動(dòng)將SceneKit空間與現(xiàn)實(shí)世界相匹配悠就,因此放置虛擬對(duì)象以使其看起來(lái)保持真實(shí)世界的位置僅需要適當(dāng)?shù)卦O(shè)置該對(duì)象的SceneKit位置蚤吹。 (請(qǐng)參閱使用SceneKit提供3D虛擬內(nèi)容例诀。)
您不一定需要使用ARAnchor類來(lái)跟蹤添加到場(chǎng)景中的對(duì)象的位置,但通過(guò)實(shí)現(xiàn)ARSCNViewDelegate方法裁着,可以將SceneKit內(nèi)容添加到ARKit自動(dòng)檢測(cè)到的任何錨點(diǎn)繁涂。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?First Steps
Providing 3D Virtual Content with SceneKit
Use SceneKit to add realistic three-dimensional objects to your AR experience.
使用SceneKit為您的AR體驗(yàn)添加逼真的三維物體。
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自動(dòng)將SceneKit空間與現(xiàn)實(shí)世界相匹配二驰,放置虛擬對(duì)象以使其看起來(lái)保持真實(shí)世界的位置扔罪,因此需要適當(dāng)?shù)卦O(shè)置對(duì)象的SceneKit位置。 例如桶雀,在默認(rèn)配置下矿酵,以下代碼在攝像機(jī)初始位置的前面放置一個(gè)距離攝像機(jī)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:
上面的代碼將一個(gè)對(duì)象直接放置在視圖的SceneKit場(chǎng)景中。 該對(duì)象自動(dòng)顯示為跟蹤真實(shí)世界的位置矗积,因?yàn)锳RKit將SceneKit空間與實(shí)際空間相匹配全肮。
或者,您可以使用ARAnchor類來(lái)跟蹤真實(shí)世界的位置漠魏,既可以通過(guò)自己創(chuàng)建錨點(diǎn)并將其添加到會(huì)話中倔矾,也可以通過(guò)觀察ARKit自動(dòng)創(chuàng)建的錨點(diǎn)來(lái)實(shí)現(xiàn)。 例如柱锹,啟用平面檢測(cè)功能后哪自,ARKit會(huì)為每個(gè)檢測(cè)到的平面添加并更新錨點(diǎn)。 要為這些錨點(diǎn)添加可視內(nèi)容禁熏,請(qǐng)實(shí)施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.
遵循設(shè)計(jì)3D Asset的最佳實(shí)踐
將SceneKit基于物理的照明模型用于材料以獲得更逼真的外觀。 (請(qǐng)參閱SceneKit示例代碼項(xiàng)目中的SCNMaterial類和Badger:高級(jí)渲染瞧毙。)
烘烤環(huán)境遮擋陰影胧华,使物體在各種場(chǎng)景照明條件下正常點(diǎn)亮。
如果您創(chuàng)建了一個(gè)虛擬對(duì)象宙彪,您打算將其放置在AR中的真實(shí)世界平面上矩动,請(qǐng)?jiān)?D素材資源中的對(duì)象下方包含一個(gè)透明平面,并在其中包含柔和陰影紋理释漆。
session
The AR session that manages motion tracking and camera image processing for the view's contents.
AR Session管理視圖內(nèi)容的運(yùn)動(dòng)跟蹤和攝像頭圖像處理悲没。
Discussion
A view creates its own session object; use this property to access and configure the view's session.
討論
視圖創(chuàng)建自己的Session對(duì)象; 使用此屬性來(lái)訪問(wèn)和配置視圖的Session。
scene
The SceneKit scene to be displayed in the view.
SceneKit場(chǎng)景將顯示在視圖中男图。
Discussion
Note
Unlike the parent?SCNView?class, an?ARSCNView?object requires a non-nil?scene to display.
討論
注意
與父SCNView類不同示姿,ARSCNView對(duì)象需要顯示非零場(chǎng)景甜橱。
? ? ? ? ? ? ? ? ? ? ? Responding to AR Updates
delegate
An object you provide to mediate synchronization of the view's AR scene information with SceneKit content.
您提供的一個(gè)對(duì)象,用于調(diào)解視圖的AR場(chǎng)景信息與SceneKit內(nèi)容的同步
ARSCNViewDelegate
Methods you can implement to mediate the automatic synchronization of SceneKit content with an AR session.
您可以實(shí)現(xiàn)的方法來(lái)調(diào)解SceneKit內(nèi)容與AR Session的自動(dòng)同步栈戳。
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.
概述
實(shí)現(xiàn)此協(xié)議以提供與由視圖的ARSession跟蹤的ARAnchor對(duì)象相對(duì)應(yīng)的SceneKit內(nèi)容岂傲,或管理視圖對(duì)此類內(nèi)容的自動(dòng)更新。
這個(gè)協(xié)議擴(kuò)展了ARSessionObserver協(xié)議子檀,所以你的會(huì)話委托也可以實(shí)現(xiàn)這些方法來(lái)響應(yīng)會(huì)話狀態(tài)的變化镊掖。
Handling Content Updates
renderer:nodeForAnchor:
Asks the delegate to provide a SceneKit node corresponding to a newly added anchor.
渲染:nodeForAnchor:請(qǐng)求委托提供與新添加的錨相對(duì)應(yīng)的SceneKit節(jié)點(diǎn)。
Parameters
renderer
The?ARSCNView?object rendering the scene.
ARSCNView對(duì)象呈現(xiàn)場(chǎng)景命锄。
anchor
The anchor for which a node is requested.
請(qǐng)求節(jié)點(diǎn)的錨點(diǎn)堰乔。
Return Value
A new SceneKit node, which ARKit will add to the scene and update to follow its corresponding AR anchor.
一個(gè)新的SceneKit節(jié)點(diǎn),ARKit將添加到場(chǎng)景并更新以跟隨其相應(yīng)的AR定位點(diǎn)脐恩。
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.
討論
根據(jù)Session配置的不同,ARKit可能會(huì)自動(dòng)將錨定添加到Session中侦讨。 ARKit還調(diào)用此方法為使用Session的addAnchor:方法手動(dòng)添加的任何ARAnchor對(duì)象提供可視內(nèi)容驶冒。
您可以實(shí)現(xiàn)此方法來(lái)提供一個(gè)新的SCNNode對(duì)象(或SCNNode子類的實(shí)例),其中包含您打算用作錨的可視表示的附件韵卤。 請(qǐng)注意骗污,ARKit控制節(jié)點(diǎn)的可見(jiàn)性及其變換屬性,所以您可能會(huì)發(fā)現(xiàn)添加子節(jié)點(diǎn)或調(diào)整節(jié)點(diǎn)的透視屬性以保持對(duì)所做位置或方向的任何更改很有用沈条。
如果您從此方法返回nil需忿,則不會(huì)將節(jié)點(diǎn)添加到場(chǎng)景中。
或者蜡歹,如果您未實(shí)現(xiàn)此方法屋厘,ARKit將創(chuàng)建一個(gè)空節(jié)點(diǎn),并且您可以實(shí)現(xiàn)渲染器:didAddNode:forAnchor:方法月而,以通過(guò)將其附加到該節(jié)點(diǎn)來(lái)提供可視內(nèi)容汗洒。
renderer:didAddNode:forAnchor:
Tells the delegate that a SceneKit node corresponding to a new AR anchor has been added to the scene.
告訴委托人已將與新的AR錨相對(duì)應(yīng)的SceneKit節(jié)點(diǎn)添加到場(chǎng)景中。
Parameters
renderer
The?ARSCNView?object rendering the scene.
ARSCNView對(duì)象呈現(xiàn)場(chǎng)景父款。
node
The newly added SceneKit node.
新添加的SceneKit節(jié)點(diǎn)溢谤。
anchor
The AR anchor corresponding to the node.
AR節(jié)點(diǎn)對(duì)應(yīng)的AR節(jié)點(diǎn)。
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.
討論
根據(jù)Session配置的不同憨攒,ARKit可能會(huì)自動(dòng)將錨定添加到Sessopm中世杀。 該視圖為每個(gè)新的錨點(diǎn)調(diào)用一次該方法。 ARKit還調(diào)用此方法為使用Session的addAnchor:方法手動(dòng)添加的任何ARAnchor對(duì)象提供可視內(nèi)容肝集。
您可以通過(guò)將幾何圖元(或其他SceneKit特征)附加到此節(jié)點(diǎn)或通過(guò)添加子節(jié)點(diǎn)來(lái)為錨點(diǎn)提供可視內(nèi)容瞻坝。
或者,您可以實(shí)現(xiàn)渲染器:nodeForAnchor:方法為錨點(diǎn)創(chuàng)建自己的節(jié)點(diǎn)(或SCNNode子類的實(shí)例)包晰。
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節(jié)點(diǎn)的屬性將被更新以匹配其相應(yīng)錨的當(dāng)前狀態(tài)湿镀。Parameters
Parameters
renderer
TheARSCNViewobject rendering the scene.
ARSCNView對(duì)象呈現(xiàn)場(chǎng)景炕吸。
node
The newly added SceneKit node.
新添加的SceneKit節(jié)點(diǎn)。
anchor
The AR anchor corresponding to the node.
AR節(jié)點(diǎn)對(duì)應(yīng)的AR節(jié)點(diǎn)
Discussion
Depending on the session configuration, ARKit may automatically update anchors in a session. The view calls this method once for each updated anchor.
討論
根據(jù)Session配置的不同勉痴,ARKit可能會(huì)自動(dòng)更新會(huì)話中的錨點(diǎn)赫模。 該視圖為每個(gè)更新的錨點(diǎn)調(diào)用一次該方法。
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節(jié)點(diǎn)的屬性已更新以匹配其相應(yīng)錨的當(dāng)前狀態(tài)蒸矛。
Parameters
renderer
TheARSCNViewobject rendering the scene.
ARSCNView對(duì)象呈現(xiàn)場(chǎng)景瀑罗。
node
The newly added SceneKit node.
新添加的SceneKit節(jié)點(diǎn)。
anchor
The AR anchor corresponding to the node.
AR節(jié)點(diǎn)對(duì)應(yīng)的AR節(jié)點(diǎn)
Discussion
Depending on the session configuration, ARKit may automatically update anchors in a session. The view calls this method once for each updated anchor.
討論
根據(jù)Session配置的不同雏掠,ARKit可能會(huì)自動(dòng)更新Session中的錨點(diǎn)斩祭。 該視圖為每個(gè)更新的錨點(diǎn)調(diào)用一次該方法。
renderer:didRemoveNode:forAnchor:
Tells the delegate that the SceneKit node corresponding to a removed AR anchor has been removed from the scene.
告訴委托人已經(jīng)從場(chǎng)景中移除了與已移除的AR錨點(diǎn)對(duì)應(yīng)的SceneKit節(jié)點(diǎn)乡话。
Parameters
renderer
TheARSCNViewobject rendering the scene.
ARSCNView對(duì)象呈現(xiàn)場(chǎng)景摧玫。
node
The newly added SceneKit node.
新添加的SceneKit節(jié)點(diǎn)。
anchor
The AR anchor corresponding to the node.
AR節(jié)點(diǎn)對(duì)應(yīng)的AR節(jié)點(diǎn)
Discussion
Depending on the session configuration, ARKit may automatically remove anchors from a session. The view calls this method once for each removed anchor.
討論
根據(jù)Session配置的不同绑青,ARKit可能會(huì)自動(dòng)從Session中刪除錨點(diǎn)诬像。 該視圖為每個(gè)已移除的錨點(diǎn)調(diào)用一次該方法。
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.
在捕獲的攝像機(jī)圖像中搜索與SceneKit視圖中的點(diǎn)相對(duì)應(yīng)的真實(shí)世界對(duì)象或AR錨闸婴。
Parameters
point
A point in the 2D coordinate system of the view.
視圖2D坐標(biāo)系中的一個(gè)點(diǎn)坏挠。
types
The types of hit-test result to search for.
要搜索的命中測(cè)試結(jié)果的類型。
Return Value
A list of results, sorted from nearest to farthest (in distance from the camera).
返回值
結(jié)果列表邪乍,從最近到最遠(yuǎn)(與相機(jī)的距離)排序降狠。
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.
討論
命中測(cè)試搜索通過(guò)ARSession處理相機(jī)圖像檢測(cè)到的真實(shí)世界對(duì)象或表面。 視圖坐標(biāo)系中的二維點(diǎn)可以指沿著三維線上的任意點(diǎn)庇楞,該三維線始于設(shè)備相機(jī)榜配,并沿著由設(shè)備方向和相機(jī)投影確定的方向延伸。 該方法沿著該線搜索姐刁,按距離相機(jī)的順序返回與其相交的所有對(duì)象芥牌。
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檢測(cè)到的AR錨點(diǎn)和真實(shí)世界對(duì)象,而不是視圖中顯示的SceneKit內(nèi)容聂使。 要搜索SceneKit對(duì)象壁拉,請(qǐng)改為使用視圖的hitTest:options:方法。
? ? ? ? ? ? ?Mapping Content to Real-World Positions
-?anchorForNode:
Returns the AR anchor associated with the specified SceneKit node, if any.
返回與指定的SceneKit節(jié)點(diǎn)關(guān)聯(lián)的AR定位點(diǎn)(如果有)柏靶。
Parameters
node
A SceneKit node in the view's scene.
場(chǎng)景中的SceneKit節(jié)點(diǎn)弃理。
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對(duì)象跟蹤節(jié)點(diǎn),或者如果節(jié)點(diǎn)未與視圖的場(chǎng)景中的錨點(diǎn)關(guān)聯(lián)屎蜓,則為零痘昌。
-?nodeForAnchor:
Returns the SceneKit node associated with the specified AR anchor, if any.
返回與指定的AR錨點(diǎn)相關(guān)的SceneKit節(jié)點(diǎn)(如果有的話)。
Parameters
anchor
An anchor in the view's AR session.
視圖的ARSession中的一個(gè)錨點(diǎn)。
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場(chǎng)景中錨點(diǎn)跟蹤的節(jié)點(diǎn)辆苔,或者如果錨點(diǎn)沒(méi)有關(guān)聯(lián)節(jié)點(diǎn)或不在視圖的ARSession中算灸,則為零。
? ? ? ? ? ? ? ? ? ? ? ?Managing Scene Lighting
automaticallyUpdatesLighting
A Boolean value that specifies whether ARKit creates and updates SceneKit lights in the view's scene.
一個(gè)布爾值驻啤,指定ARKit是否在視圖的場(chǎng)景中創(chuàng)建并更新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(默認(rèn)值),視圖會(huì)自動(dòng)創(chuàng)建一個(gè)或多個(gè)SCNLight對(duì)象骑冗,將它們添加到場(chǎng)景中赊瞬,并更新其屬性以反映來(lái)自相機(jī)場(chǎng)景的估計(jì)光照信息。 如果要直接控制SceneKit場(chǎng)景中的所有照明贼涩,請(qǐng)將此值設(shè)置為NO巧涧。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Debugging AR Display
ARSCNDebugOptions
Options for drawing overlay content to aid debugging of AR tracking in a SceneKit view.
用于繪制疊加內(nèi)容的選項(xiàng),以幫助在SceneKit視圖中調(diào)試AR追蹤遥倦。
Discussion
To use these debugging overlays, add them to the option set in the view's?debugOptionsproperty (inherited from the?SCNView?class).
討論
要使用這些調(diào)試覆蓋圖谤绳,請(qǐng)將它們添加到視圖的debugOptions屬性中的選項(xiàng)集(從SCNView類繼承)。
Inherits From ?SCNView