I get AR object from an API call and store then in the file manager storage, during this process, I merged all the nodes in the one node, and also add an SCNFloor below the object:
let mergedScene = SCNScene()
let nodeContainer = SCNNode()
let floor = SCNNode()
let scnFloor = SCNFloor()
scnFloor.length = 2
scnFloor.width = 2
floor.geometry = scnFloor
floor.geometry?.firstMaterial!.colorBufferWriteMask = []
floor.geometry?.firstMaterial!.readsFromDepthBuffer = true
floor.geometry?.firstMaterial!.writesToDepthBuffer = true
floor.geometry?.firstMaterial!.lightingModel = .constant
nodeContainer.addChildNode(floor)
for node in scene.rootNode.childNodes {
nodeContainer.addChildNode(node)
}
mergedScene.rootNode.addChildNode(nodeContainer)
if mergedScene.write(to: url, options: nil, delegate: nil, progressHandler: { float, error, pointer in
if let error = error {
completion(.error(error))
}
})
and here is the default lighting that I added in viewDidLoad
let light = SCNLight()
light.type = .directional
light.shadowColor = UIColor(white: 0, alpha: 0.6)
light.color = UIColor.white
light.castsShadow = true
light.automaticallyAdjustsShadowProjection = true
light.shadowMode = .deferred
light.shadowRadius = 10
light.shadowSampleCount = 3
let sunLightNode = SCNNode()
sunLightNode.position = SCNVector3(x: 1_000, y: 1_000, z: 0)
sunLightNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: .pi * 1.5)
sunLightNode.light = light
light.orthographicScale = 5
sceneView.scene.rootNode.addChildNode(sunLightNode)
And the configuration:
sceneView.preferredFramesPerSecond = 30
sceneView.automaticallyUpdatesLighting = true
sceneView.autoenablesDefaultLighting = true
let configuration = ARWorldTrackingConfiguration()
configuration.isLightEstimationEnabled = false
configuration.planeDetection = .horizontal
configuration.providesAudioData = false
configuration.isAutoFocusEnabled = true
sceneView.session.run(configuration)
The problem is, maybe 4 out of 10 times, when you place the object in the scene, there is no shadow, or even when there is the shadows, maybe when you move camera away from the object and come back to the object, you will see the shadow is gone, it's not happened all the time but it happens quite a lot. But on the other times, when you place the object you can see the shadow it won't be gone by moving the camera!
And when I render a snapshot from sceneView
when there is no shadow on the screen, you can see the shadow on the rendered snapshot, so the shadow is there, but only not visible on the screen! Could anyone help me to solve this issue? Thank you