SceneKit / ARKit中的不可见阴影平面存在问题

时间:2019-03-14 11:17:34

标签: ios scenekit augmented-reality arkit shadow

一段时间以来,我一直在寻找问题的答案,但找不到真正有用的东西或无法解释的事情。我也一遍又一遍地检查了在SO上找到的所有内容,但找不到答案。

在AR World中显示对象时,问题持续发生。 iEx我在地板上的平面上放置了一个对象,这是我看不见的阴影平面。然后,这完全取决于设备的视角。为了澄清起见,我在图像中添加了一个,只是视角略有不同。看看阴影发生了什么:

enter image description here

我希望一直保持良好的阴影,而不是您看到的伪影。

注意:我已经尝试过使用 shadowSampleCount ,“偏移”以及所有其他选项,它们应该有助于获得适当的,较低的渲染成本阴影。

以下是有关照明和飞机,材质等的相关代码的摘录

对于 SCNLight

class func directionalLight() -> SCNLight {

    let light = SCNLight()

    light.type = .directional
    light.castsShadow = true
    light.color = UIColor.white
    light.shadowMode = .deferred
    light.shadowSampleCount = 8
    light.shadowRadius = 1
    // light.automaticallyAdjustsShadowProjection = false
    light.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.75)
    light.categoryBitMask = -1

    return light

}

以及添加方式:

func setupLights() {

    lightDirectionNode.light = Lighting.directionalLight()
    // lightDirectionNode.eulerAngles = SCNVector3(-66.degreesToRadians, 0, 0)
    lightDirectionNode.eulerAngles = SCNVector3(0, 90.degreesToRadians, 45.degreesToRadians)
    sceneView.scene.rootNode.addChildNode(lightDirectionNode)

}

对于 SCNPlane

class func shadowPlane() -> SCNNode {

    let objectShape = SCNPlane(width: 200, height: 200)
    objectShape.heightSegmentCount = 2
    objectShape.widthSegmentCount = 2
    objectShape.cornerRadius = 100
    objectShape.cornerSegmentCount = 16

    let objectNode = SCNNode(geometry: objectShape)

    objectNode.geometry?.firstMaterial?.diffuse.contents = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    objectNode.geometry?.firstMaterial?.colorBufferWriteMask = SCNColorMask(rawValue: 0)

    objectNode.physicsBody = Physics.floorPhysicsBody(shape: objectShape)
    objectNode.name = "floor"
    objectNode.renderingOrder = -10 // renderingOrder // 0

    return objectNode
}

以及添加方式:

func setupShadowPlane() {

    let shadowPlane = NodeFactory.shadowPlane()

    // Set the Node's properties
    shadowPlane.position = SCNVector3(x: (focusSquare.lastPosition?.x)!, y: (focusSquare.lastPosition?.y)!, z: (focusSquare.lastPosition?.z)!)
    shadowPlane.eulerAngles = SCNVector3(-90.degreesToRadians, 0.0, 0.0)
    sceneView.scene.rootNode.addChildNode(shadowPlane)

}

我在做什么错?有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

还有3个实例属性需要考虑:

var shadowRadius: CGFloat { get set }
var shadowCascadeCount: Int { get set }
var shadowCascadeSplittingFactor: CGFloat { get set }

如果您不设置这些设置,则肯定会导致渲染失真。

let lightNode = SCNNode()
lightNode.light = SCNLight()
// POSITION OF DIRECTIONAL LIGHT ISN'T IMPORTANT.
// ONLY DIRECTION IS CRUCIAL FOR DIRECTIONAL LIGHTS.
lightNode.rotation = SCNVector4(x: 0, y: 0, z: 0, w: 1)
lightNode.light!.type = .directional
lightNode.castsShadow = true
lightNode.light?.shadowMode = .deferred

/* THREE INSTANCE PROPERTIES TO SETUP */
lightNode.light?.shadowRadius = 3.25
lightNode.light?.shadowCascadeCount = 3
lightNode.light?.shadowCascadeSplittingFactor = 0.09

lightNode.light?.shadowColor = UIColor(white: 0, alpha: 0.75)
scene.rootNode.addChildNode(lightNode)
  

还有一件事情 –当Auto Adjust处于关闭状态时:

     

光像照相机一样,具有nearfar 剪切平面用于设置。

lightNode.light?.zNear = 0
lightNode.light?.zFar = 1000000    // Far Clipping Plane is important 

enter image description here

希望这会有所帮助。