ARKit浮动飞机 [英] ARKit floating planes

查看:121
本文介绍了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, for: 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天全站免登陆