1
votes

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.

enter image description here

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

enter image description here

1
Do you absolutely need deferred shadows? if so, why?mnuages

1 Answers

0
votes

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.