问题
I have a cube, a wall and two lightNodes in my SceneKit application and I would like to make the cube throw a shadow onto the wall.
My lightNodes are here:
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = SCNLightTypeSpot
lightNode.position = SCNVector3(x: 0, y: 0, z: 15)
lightNode.castsShadow = true
scene.rootNode.addChildNode(lightNode)
// create and add an ambient light to the scene
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = SCNLightTypeAmbient
ambientLightNode.light!.color = UIColor.darkGrayColor()
ambientLightNode.castsShadow = true
scene.rootNode.addChildNode(ambientLightNode)
This is my wall:
var wall = SCNNode(geometry: SCNBox(width: 400, height: 100, length: 4, chamferRadius: 0))
wall.geometry?.firstMaterial!.emission.contents = UIColor.lightGrayColor()
wall.geometry?.firstMaterial!.doubleSided = false
wall.castsShadow = true
wall.position = SCNVector3Make(0, 0, -5)
wall.physicsBody = SCNPhysicsBody.staticBody()
scene.rootNode.addChildNode(wall)
As you can see all of the nodes (including the cube) have the property to cast shadow, since it is true
.
How does it come that there is no shadow on the wall?
回答1:
same issue as here? You have to set castsShadow
on the light (instead of on the node that holds the light).
Also note that omnidirectional and ambient lights can not cast shadows. Only directional and spot lights can.
回答2:
I'm not sure why this is the case, but I was having a similar issue and found that setting the light
's shadowMode
to SCNShadowModeDeferred
fixed it.
来源:https://stackoverflow.com/questions/29827311/scenekit-shadows-on-wall