3
votes

I am learning ARKit. I'm placing Virtual Objects in Augmented Reality scene and struggling in these problems!

I am using this demo project from github

1- How to add only one directional light for all (separate) nodes in SceneView of SceneKit & Move directional light with camera position? So that my added shadow can also move with light direction.

If I translate the object shadows are working as it should be. But If I rotate object now shadow should not move on the plan. They are moving because of light is at fixed position.

2- Shadow is looking fine only in case If I add only one object on the plane. But If I add two or more objects more directional lights are adding in SceneView. Now every object has more than one shadows. I want it to restrict only one shadow.

enter image description here enter image description here

I have added light and shadow plane in sceneKit editor. (not programmatically). Here are my scenekit editor's screenshots.

enter image description hereenter image description here

3- I have read and confirmed that shadow are adding only If I set directional light property to deffered. But in this case app is crashing If I call remove all nodes from sceneView's root node. My removing nodes code is.

self.sceneView.scene.rootNode.enumerateChildNodes { (node, stop) -> Void in
    node.removeFromParentNode()
    print("removed ", node.name as Any)
}

You can watch my apps video for more clearance. Apps video, How it is working now

My requirement is to add only one shadow for every object. When I Rotate and translate objects shadows should look real.

I also have tried it as removing light from scn file of vase, and add a separate light.scn file having only light in it. added These two (vase and light) nodes in sceneView. But No shadow is appearing.

1

1 Answers

0
votes
  1. Directional lights are interpreted from the shader only with their direction (that one light-red vector coming out of the node in Xcode). It does not matter where the position of the light is. Directional light are often used for imitating sun light.

  2. I implemented a similar project so here is my attempt. I added one directional light from a separate SCN-File to the scene when I initialize the SCNScene. My settings for it:

    • castsShadow: true
    • mode: deferred (my App is not crashing, if I remove my objects from the scene :/ )

And actually thats it to make it work in my project. One thing about your planes: I think you have not disabled castsShadow on the planes. Therefore a user can see the planes.

Edit: I could reproduce the crash. It does occur when removing the directional light. Thats why the app is not crashing in my project. So you could do it like me and add the directional light in viewDidLoad() for example.