ZAY*_*ZAY 5 shadow augmented-reality scenekit swift arkit
有一段时间我四处寻找我的问题的答案,但找不到任何真正有帮助的东西,或解释正在发生的事情。我也一遍又一遍地检查了我在 SO 上找到的所有内容,但找不到答案。
在 AR 世界中显示对象时,此问题不断发生。iEx 我将一个物体放置在地板上的一个平面上,这是我的隐形阴影平面。那么这完全取决于设备的视角。为了澄清,我添加了图像,它的视角略有不同。看看阴影发生了什么:
我希望一直有一个好的影子,而不是像你看到的那样的人工制品。
注意:我已经使用了shadowSampleCount、 Bias 和所有其他选项,这应该有助于获得适当的、低渲染成本的阴影。
以下是Lighting and Plane、Material等相关代码的摘录
对于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
}
Run Code Online (Sandbox Code Playgroud)
以及我如何添加它:
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)
}
Run Code Online (Sandbox Code Playgroud)
对于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
}
Run Code Online (Sandbox Code Playgroud)
以及我如何添加它:
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)
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?任何人都可以帮忙吗?
还有 3 个实例属性需要考虑:
\n\nvar shadowRadius: CGFloat { get set }\nvar shadowCascadeCount: Int { get set }\nvar shadowCascadeSplittingFactor: CGFloat { get set }\nRun Code Online (Sandbox Code Playgroud)\n\n如果您不设置这些,它们肯定会导致渲染伪影。
\n\nlet lightNode = SCNNode()\nlightNode.light = SCNLight()\n// POSITION OF DIRECTIONAL LIGHT ISN\'T IMPORTANT.\n// ONLY DIRECTION IS CRUCIAL FOR DIRECTIONAL LIGHTS.\nlightNode.rotation = SCNVector4(x: 0, y: 0, z: 0, w: 1)\nlightNode.light!.type = .directional\nlightNode.light!.castsShadow = true\nlightNode.light?.shadowMode = .deferred\n\n/* THREE INSTANCE PROPERTIES TO SETUP */\nlightNode.light?.shadowRadius = 3.25\nlightNode.light?.shadowCascadeCount = 3\nlightNode.light?.shadowCascadeSplittingFactor = 0.09\n\nlightNode.light?.shadowColor = UIColor(white: 0, alpha: 0.75)\nscene.rootNode.addChildNode(lightNode)\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n\n\n
Auto Adjust当关闭时,还有一件事\xe2\x80\x93 :光就像相机一样,具有
\nnear用于设置的far剪裁平面。
lightNode.light?.zNear = 0\nlightNode.light?.zFar = 1000000 // Far Clipping Plane is important \nRun Code Online (Sandbox Code Playgroud)\n\n\n\n希望这可以帮助。
\n