Deferred Shadow doesn't work if detecting Multiple Transparent Planes

↘锁芯ラ 提交于 2020-02-22 07:46:44

问题


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.


回答1:


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

P.S.

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.

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.



来源:https://stackoverflow.com/questions/60283239/deferred-shadow-doesnt-work-if-detecting-multiple-transparent-planes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!