SceneKit shadows on wall

青春壹個敷衍的年華 提交于 2019-12-08 04:28:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!