此篇文章皆出自苹果官方文档, 我只不过信手拿来记述,再加入点自己的理解。揉合揉合,以加深理解。不愿意看下面的,可以直接点击链接查看官方文档,并且包括例子都可以下载。
概述
本篇文章主要是技术苹果官网教程来使用SceneKit来进行平面检测,并且生成一个虚拟的平面,自己可以延伸在平面上记载3D图像。
为了演示平面检测,应用程序只需放置一个SCNPlane对象来可视化每个检测到的对象。
SCNPlane 根据其平面width和height属性,在其局部坐标空间的x轴和y轴尺寸上定义一个平面。要使平面定向不同,请调整transfrom包含平面几何的节点属性。你也可以使用属性创建圆角矩形平面cornerRadius
配置并运行AR会话
ARSCNView类中包含一个SceneKit视图ARSession管理创建的增强现实(AR)的经验所需要的运动跟踪和图像处理对象。如果要运行会话,必须提供会话配置:
要启动AR会话,请使用所需选项(如平面检测)创建会话配置对象,然后调用如下方法:
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal //水平检测
sceneView.session.run(configuration)
检测到平面放置3D内容
设置AR会话后,可以使用SceneKit在视图中放置虚拟内容。
当启用平面检测时,ARKit会为每个检测到的平面添加和更新锚点。
默认情况下,ARSCNView该类SCNNode为每个锚点SceneKit场景添加一个对象。您的视图的代理可以实现向场景添加内容的方法
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
// Place content only for anchors found by plane detection.
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
// Create a SceneKit plane to visualize the plane anchor using its position and extent.
let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z))
let planeNode = SCNNode(geometry: plane)
planeNode.simdPosition = float3(planeAnchor.center.x, 0, planeAnchor.center.z)
/*
`SCNPlane` is vertically oriented in its local coordinate space, so
rotate the plane to match the horizontal orientation of `ARPlaneAnchor`.
*/
planeNode.eulerAngles.x = -.pi / 2
// Make the plane visualization semitransparent to clearly show real-world placement.
planeNode.opacity = 0.25
/*
Add the plane visualization to the ARKit-managed node so that it tracks
changes in the plane anchor as plane estimation continues.
*/
node.addChildNode(planeNode)
}
如果将内容添加为对应于锚点的节点的子节点,则ARSCNView类将自动移动该内容,因为ARKit会优化其对飞机位置和范围的估计。为了显示估计平面的完整范围,此示例应用程序还实现了该方法,更新对象的大小以反映ARKit提供的esitmate.
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
// Update content only for plane anchors and nodes matching the setup created in `renderer(_:didAdd:for:)`.
guard let planeAnchor = anchor as? ARPlaneAnchor,
let planeNode = node.childNodes.first,
let plane = planeNode.geometry as? SCNPlane
else { return }
// Plane estimation may shift the center of a plane relative to its anchor's transform.
planeNode.simdPosition = float3(planeAnchor.center.x, 0, planeAnchor.center.z)
/*
Plane estimation may extend the size of the plane, or combine previously detected
planes into a larger one. In the latter case, `ARSCNView` automatically deletes the
corresponding node for one plane, then calls this method to update the size of
the remaining plane.
*/
plane.width = CGFloat(planeAnchor.extent.x)
plane.height = CGFloat(planeAnchor.extent.z)
}
官网的代码在github放了一份,可以运行查看一下
知行办公,专业移动办公平台https://zx.naton.cn/
【总监】十二春秋之,3483099@qq.com;
【Master】zelo,616701261@qq.com;
【运营】运维艄公,897221533@qq.com;****
【产品设计】流浪猫,364994559@qq.com;
【体验设计】兜兜,2435632247@qq.com;
【iOS】淘码小工,492395860@qq.com;iMcG33K,imcg33k@gmail.com;
【Android】人猿居士,1059604515@qq.com;思路的顿悟,1217022114@qq.com;
【java】首席工程师MR_W,feixue300@qq.com;
【测试】土镜问道,847071279@qq.com;
【数据】fox009521,42151960@qq.com;