问题
Sometimes ARKit
creates plane over existing one. It doesn't merge them. I am testing ARKit
with iPhone 7 in closed space. Object placed on that plane appears to float in space. This scenario doesn't happen often, and I can't reproduce it all the time. Can someone tell me how to prevent overlapping and floating planes to appear? Or how to place 3D object on bottom plane?
Here is the code for drawing planes.
func createPlaneNode(center: vector_float3, extent: vector_float3) -> SCNNode {
let plane = SCNPlane(width: CGFloat(extent.x), height: CGFloat(extent.z))
let planeMaterial = SCNMaterial()
planeMaterial.diffuse.contents = UIColor.blue.withAlphaComponent(0.4)
plane.materials = [planeMaterial]
let planeNode = SCNNode(geometry: plane)
planeNode.position = SCNVector3Make(center.x, 0, center.z)
planeNode.transform = SCNMatrix4MakeRotation(-Float.pi / 2, 1, 0, 0)
return planeNode
}
func createPlaneNode(center: vector_float3, extent: vector_float3) -> SCNNode {
let plane = SCNPlane(width: CGFloat(extent.x), height: CGFloat(extent.z))
let planeMaterial = SCNMaterial()
planeMaterial.diffuse.contents = UIColor.blue.withAlphaComponent(0.4)
plane.materials = [planeMaterial]
let planeNode = SCNNode(geometry: plane)
planeNode.position = SCNVector3Make(center.x, 0, center.z)
planeNode.transform = SCNMatrix4MakeRotation(-Float.pi / 2, 1, 0, 0)
return planeNode
}
func updatePlaneNode(_ node: SCNNode, center: vector_float3, extent: vector_float3) {
let geometry = node.geometry as! SCNPlane
geometry.width = CGFloat(extent.x)
geometry.height = CGFloat(extent.z)
node.position = SCNVector3Make(center.x, 0, center.z)
}
回答1:
I don't think you can prevent ARKit from generating overlapping planes as that part is pretty much a black box. However, two possibilities come to my mind to handle such a case.
1) Stop tracking after the first plane is found and insert an infinite plane to your scene. You can maybe use an SCNFloor for this purpose, or just a regular plane with enormous extents. This, of course, only is usable if you need a single plane in your scene.
2) Every once in a while (maybe inside delegate methods renderer(_: SCNSceneRenderer, didUpdate: SCNNode, for: ARAnchor)
or renderer(_: SCNSceneRenderer, didAdd: SCNNode, for: ARAnchor)
), manually check whether the planes in your scene are overlapping by projecting these planes on the xz-plane, make 2D collision tests and remove either one of them if the overlapping portion is higher than a certain threshold. You can also check if the distance between them in 3D is lower than a threshold to determine this.
来源:https://stackoverflow.com/questions/47980670/arkit-floating-planes