如果检测到多个透明平面

如果检测到多个透明平面

本文介绍了如果检测到多个透明平面,则延迟阴影不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我检测了平面并为平面上方的对象显示阴影.如果只有一个平面,它工作正常,但如果检测到多个平面,就会出现多余的阴影.

如图所示,在#1 飞机上,阴影是正确的,但是如果我再添加一个#2 飞机,那么#2 飞机的阴影是错误的,即使我移除飞机,#1 飞机上的阴影也是如此消失了,但 2 号位面的阴影还在.我不想移除#2 飞机,但是如何移除#2 飞机上的错误阴影?

请帮我修复它,谢谢.

如果我把飞机改成地板,效果会好很多.

解决方案

当您使用平面检测功能并希望将两个共面的检测平面组合成更大的平面时,最好的方法是使用 renderer(_:didUpdate:for:) 实例方法,用于获得一个以一个 ARPlaneAnchor 为中心的联合平面.

您的代码可能如下所示:

extension ViewController: ARSCNViewDelegate {func 渲染器(_ 渲染器:SCNSceneRenderer,didUpdate 节点:SCNNode,对于锚点:ARAnchor) {守卫让planeAnchor =锚定为?ARPlaneAnchor,让planeNode = node.childNodes.first,让 myPlane = planeNode.geometry 作为?平面否则{返回}让宽度 = CGFloat(planeAnchor.extent.x)让高度 = CGFloat(planeAnchor.extent.z)myPlane.width = 宽度myPlane.height = 高度让 x = CGFloat(planeAnchor.center.x)让 y = CGFloat(planeAnchor.center.y)让 z = CGFloat(planeAnchor.center.z)平面节点.位置 = SCNVector3(x, y, z)}}

附言

此代码的关键点不是更新本身,而是您获得更大的单平面而不是两个不同的共面平面.因此,这架飞机捕捉到的阴影必须按预期工作.

是否要使用无限平面 (SCNFloor) 取决于您,但如果您跟踪非无限表面(如桌面)则无效.

In my code, I detect the plane and show shadow for the object above the plane. If there is one plane, it works fine, but if it detects multiple planes, the redundant shadow will show.

As the picture shows, on the plane #1, the shadow is right, but if I add another plane #2, the plane #2 has the wrong shadow, even if I remove the airplane, the shadow on plane #1 disappears, but the shadow on plane #2 is still there. I don't want to remove the plane #2, but how to remove the wrong shadow on plane #2?

Please help me fix it, thanks.

Edit: If I change the plane to floor, it will be much better.

解决方案

When you use a Plane Detection feature and want to combine two coplanar detected planes into bigger one, the best approach is to update detected planes using renderer(_:didUpdate:for:) instance method to get a united plane with one ARPlaneAnchor in its center.

Here's how your code may look like:

extension ViewController: ARSCNViewDelegate {

    func renderer(_ renderer: SCNSceneRenderer,
              didUpdate node: SCNNode,
                  for anchor: ARAnchor) {

        guard let planeAnchor = anchor as? ARPlaneAnchor,
              let planeNode = node.childNodes.first,
              let myPlane = planeNode.geometry as? SCNPlane
        else { return }

        let width = CGFloat(planeAnchor.extent.x)
        let height = CGFloat(planeAnchor.extent.z)
        myPlane.width = width
        myPlane.height = height

        let x = CGFloat(planeAnchor.center.x)
        let y = CGFloat(planeAnchor.center.y)
        let z = CGFloat(planeAnchor.center.z)
        planeNode.position = SCNVector3(x, y, z)
    }
}

这篇关于如果检测到多个透明平面,则延迟阴影不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:33