方向光受相机运动的影响

时间:2019-09-24 04:32:30

标签: ios scenekit augmented-reality arkit

我想像在宜家广场(IKEA Place)那样实现阴影效果,在这里,阴影会粘附在虚拟物体上。

enter image description here

这是我的实现。我将方向光影平面添加到虚拟对象。当我在场景中移动虚拟对象时,阴影未位于虚拟对象底部的正下方。

这是我的代码:

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)
}

0 个答案:

没有答案