Home > Net >  Where a shadow plane should be defined in Scenekit
Where a shadow plane should be defined in Scenekit

Time:06-23

It's so confusing to me, would be grateful if anyone help me on it. I have a shadow plane to show the shadow below the AR object. I read some article that they define this shadow in viewDidLoadand add it as the child bode to sceneView.scene. The question is, it should be defined only once for the floor surface?

for instance, I can add the shadow plane to renderer(_:didAdd:for:), it call it once when a new surface is detected. That is so cool for me. But the position of the shadow plane should be changed as well? can someone explain it to me that where it should be defined and wehere/when it should be updated?

here how I define the shadow plane

private func addShadowPlane(node: SCNNode, planeAnchor: ARPlaneAnchor) {
    
    let anchorX = planeAnchor.center.x
    let anchorY: planeAnchor.center.y
    let anchorZ = planeAnchor.center.z
            
    let floor = SCNFloor()
    let floorNode = SCNNode(geometry: floor)
    floorNode.position = SCNVector3(anchorX, anchorY, anchorZ)
    
    floor.length = CGFloat(planeAnchor.extent.z)
    floor.width = CGFloat(planeAnchor.extent.x)
    floor.reflectivity = 0
    floor.materials = [shadowMaterialStandard()]
    node.addChildNode(floorNode)
}


func shadowMaterialStandard() -> SCNMaterial {
    let material = SCNMaterial()
    material.lightingModel = .physicallyBased
    material.writesToDepthBuffer = true
    material.readsFromDepthBuffer = true
    material.colorBufferWriteMask = []
    return material
}

CodePudding user response:

The issue you might run into is: Do you want one single shadow plane in a kind of initial defined position and then remains there (or can be repositioned). Or do you want a lots of shadow planes, like on any surface captured with the ARKit? The problem might be, that all those planes will not be exact and accurate to the surfaces on top they are created (just more or less). You can make more accurate shapes for surfaces, but they are built up in an ongoing process and need more time to complete (imagine you scan a table by walking around). I also did some ARApps with Shadow planes. I usually create one single shadow plane (like 20x20 meters) on my request using a focus square. I fetch the worldPosition from the focus square, then I add a plane to that location using Scenekit (and not the Renderer for plane anchors). Keep in mind, there are many ways to do this. There is no best way.

Try to study this Apple Sample App for more information on placing objects, casting shadows etc:

https://developer.apple.com/documentation/arkit/environmental_analysis/placing_objects_and_handling_3d_interaction

  • Related