4
votes

I have a spotlight, created with the code beneath, casting shadows on all of my nodes:

 spotLight.type = SCNLightTypeSpot
 spotLight.spotInnerAngle = 50.0
 spotLight.spotOuterAngle = 150.0
 spotLight.castsShadow = true
 spotLight.shadowMode = SCNShadowMode.Deferred
 spotlightNode.light = spotLight
 spotlightNode.eulerAngles = SCNVector3(x: GLKMathDegreesToRadians(-90), y: 0, z: 0)
 spotlightNode.position = levelData.coordinatesForGridPosition(column: 0, row: playerGridRow)
 spotlightNode.position.y = 1.5
 rootNode.addChildNode(spotlightNode)

The scene is moving along the z axis, and the camera has an infinite animation that makes it move:

let moveAction = SCNAction.moveByX(0.0, y: 0.0, z: CGFloat(-GameVariables.segmentSize / 2), duration: 2.0)
cameraContainerNode.runAction(SCNAction.repeatActionForever(moveAction))

As the camera moves though, the light doesn't, so after a while, the whole scene is dark. I want to move the light with the camera, however if I apply to the light node the same moving animation, all the shadows start to flicker. I tried to change the SCNShadowMode to Forward and the light type to Directional, but the flickering is still there. With directional, I actually loose most of my shadows. If I create a new light node later on, it will seem that I have two "suns", which of course is impossible. The final aim is simply to have an infinite light that shines parallel to the scene from the left, casting all the shadows to the right. Any ideas?

1

1 Answers

0
votes

Build a node tree to hold both spotlight and camera.

Create, say, cameraRigNode as an SCNNode with no geometry. Create cameraContainerNode and spotlightNode the same way you are now. But make them children of cameraRigNode, not the scene's root node.

Apply moveAction to cameraRigNode. Both the camera and the light will now move together.