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

问题描述

在我的代码中,我检测到平面并在平面上方显示对象的阴影.如果只有一个平面,则可以正常工作,但是如果检测到多个平面,则将显示冗余阴影.

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.

如图所示,在#1平面上,阴影是正确的,但是如果我添加另一个#2平面,则#2平面具有错误的阴影,即使我移除飞机,#1平面上的阴影也是如此消失了,但#2飞机上的阴影仍然存在.我不想删除2号平面,但是如何去除2号平面上的错误阴影?

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.

如果我将飞机换成地板,那会更好.

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

推荐答案

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

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.

这是您的代码的外观:

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

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

The key point of this code is not an update itself but rather the fact that you get a bigger single plane instead of two different coplanar planes. Thus shadows, that this single plane catches, must work as expected.

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

It's up to you if you wanna use an infinite plane (SCNFloor) but it's not effective if you track a non-infinite surface like a table top.

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

09-24 14:16