我想使用ARkit检测地板,当存在物体(例如桌子等)并希望对检测到的表面着色时,是否可以使用ARkit进行检测?我是ARKit世界中的新手,不确定是否可以这样做以及从何处开始。
最佳答案
您的 session 配置函数应如下所示,您可以在viewWillAppear上调用它
func setUpSceneView() {
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal
sceneView.session.run(configuration)
sceneView.delegate = self
sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints]
}
您也可以将.vertical用作墙壁或其他材质。
在视图上确实加载了一个tapGestureRecognizer到场景中,以便您可以将您点击的SCNNode添加到检测到的表面
viewDidLoad中的addTapGestureToSceneView()
func addTapGestureToSceneView() {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(placeOnSurface.addNodeToSceneView(withGestureRecognizer:)))
sceneView.addGestureRecognizer(tapGestureRecognizer)
}
方法addNodeToSceneView在检测到平面时会将SNNode放置在表面上。
@objc func addShipToSceneView(withGestureRecognizer recognizer: UIGestureRecognizer) {
let tapLocation = recognizer.location(in: sceneView)
let hitTestResults = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)
guard let hitTestResult = hitTestResults.first else { return }
let translation = hitTestResult.worldTransform.translation
let x = translation.x
let y = translation.y
let z = translation.z
var name:String = "YourModel.obj" //you can add node whatever
let group = DispatchGroup()
group.enter()
DispatchQueue.global(qos: .background).async {
print("This is run on the background queue")
let tempScene = SCNScene(named: name)!
var geom:SCNGeometry = tempScene.rootNode.childNodes[0].geometry!
var boxNode:SCNNode = SCNNode(geometry: geom)
boxNode.position = SCNVector3(x,y,z)
self.sceneView.scene.rootNode.addChildNode(boxNode)
group.leave()
}
group.notify(queue: .main){
//Here you know that the node is has been put
}
}
and you need some functions for render
extension float4x4 {
var translation: float3 {
let translation = self.columns.3
return float3(translation.x, translation.y, translation.z)
}
}
extension placeOnSurface: ARSCNViewDelegate {
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
// 1
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
// 2
let width = CGFloat(planeAnchor.extent.x)
let height = CGFloat(planeAnchor.extent.z)
let plane = SCNPlane(width: width, height: height)
// 3
plane.materials.first?.diffuse.contents = UIColor.transparentLightBlue
// 4
let planeNode = SCNNode(geometry: plane)
// 5
let x = CGFloat(planeAnchor.center.x)
let y = CGFloat(planeAnchor.center.y)
let z = CGFloat(planeAnchor.center.z)
planeNode.position = SCNVector3(x,y,z)
planeNode.eulerAngles.x = -.pi / 2
// 6
node.addChildNode(planeNode)
}
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
// 1
guard let planeAnchor = anchor as? ARPlaneAnchor,
let planeNode = node.childNodes.first,
let plane = planeNode.geometry as? SCNPlane
else { return }
// 2
let width = CGFloat(planeAnchor.extent.x)
let height = CGFloat(planeAnchor.extent.z)
plane.width = width
plane.height = height
// 3
let x = CGFloat(planeAnchor.center.x)
let y = CGFloat(planeAnchor.center.y)
let z = CGFloat(planeAnchor.center.z)
planeNode.position = SCNVector3(x, y, z)
}
}
我还建议您在viewDidLoad中调用函数configureLighting()以获得更好的用户体验
func configureLighting() {
sceneView.autoenablesDefaultLighting = true
sceneView.automaticallyUpdatesLighting = true
}
关于ios - 使用ARKit是否可以检测放置有物体的地板?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48787022/