ARKit 浮动平面 [英] ARKit floating planes

查看:22
本文介绍了ARKit 浮动平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时 ARKit 在现有平面上创建平面.它不会合并它们.我正在封闭空间中使用 iPhone 7 测试 ARKit.放置在该平面上的物体似乎漂浮在太空中.这种情况并不经常发生,我也无法一直重现.有人能告诉我如何防止出现重叠和浮动平面吗?或者如何在底面放置3D对象?

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?

这是绘制平面的代码.

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)
}

推荐答案

我认为您无法阻止 ARKit 生成重叠平面,因为那部分几乎是一个黑盒子.然而,我想到了两种可能来处理这种情况.

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) 在找到第一个平面后停止跟踪,并在您的场景中插入一个无限平面.您可以为此使用 SCNFloor ,或者只是一个具有巨大范围的普通平面.当然,这仅在您的场景中需要一个平面时才可用.

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) 每隔一段时间(可能在委托方法 renderer(_: SCNSceneRenderer, didUpdate: SCNNode, for: ARAnchor)renderer(_: SCNSceneRenderer, didAdd: SCNNode,对于:ARAnchor)),通过将这些平面投影到 xz 平面上来手动检查场景中的平面是否重叠,进行 2D 碰撞测试,如果重叠部分高于某个阈值,则删除其中一个.您还可以检查 3D 中它们之间的距离是否低于阈值来确定这一点.

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.

这篇关于ARKit 浮动平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆