Directional Light is affected by Camera's movement

独自空忆成欢 提交于 2019-12-24 00:42:39

问题


I want to achieve the shadows effect like in IKEA Place, where the shadow is stick with the virtual object.

Here is my implementation. I added the direction light and shadow plane to the virtual object. When I moved the virtual object in the scene, shadow is not positioned right under the bottom of the virtual object.

Here is my code:

func setupShadows() {

    let flourPlane = SCNFloor()
    let groundPlane = SCNNode()
    let groundMaterial = SCNMaterial()
    groundMaterial.lightingModel = .constant
    groundMaterial.writesToDepthBuffer = true
    groundMaterial.readsFromDepthBuffer = true
    groundMaterial.colorBufferWriteMask = []
    flourPlane.materials = [groundMaterial]
    groundPlane.geometry = flourPlane
    addChildNode(groundPlane)

    // Create a ambient light
    let ambientLight = SCNNode()
    ambientLight.light = SCNLight()
    ambientLight.light?.shadowMode = .deferred
    ambientLight.light?.color = UIColor.white
    ambientLight.light?.type = .ambient
    addChildNode(ambientLight)

    // Create a directional light node with shadow
    let directionalLightNode = SCNNode()
    directionalLightNode.light = SCNLight()
    directionalLightNode.light?.type = .directional
    directionalLightNode.light?.color = UIColor.white
    directionalLightNode.light?.castsShadow = true
    directionalLightNode.light?.automaticallyAdjustsShadowProjection = true
    directionalLightNode.light?.shadowMode = .deferred
    directionalLightNode.light?.categoryBitMask = -1
    directionalLightNode.light?.shadowColor = UIColor.black.withAlphaComponent(0.4)
    directionalLightNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: .pi * 1.5)
    addChildNode(directionalLightNode)
}

来源:https://stackoverflow.com/questions/58073121/directional-light-is-affected-by-cameras-movement

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