如何解决ARKit / SceneKit影子问题
我正在开发适用于iPhone和iPad的ARKit应用程序,但遇到一些麻烦,无法从相机的实时取景中获取阴影。我的场景(ShadowTest.scn
)如下:
我未选中飞机材质上的Red
属性的Green
,Blue
,Alpha
和Write to Color
,并设置了可见性到Cull back
。
对于定向光,选择Enable Shadows
并将模式设置为Deferred
。
在场景文件本身中,一切看起来都不错。阴影可见,飞机看不见。
我的问题是何时将其添加到场景视图中。这就是我将其添加到场景中的方式:
func addTestBox(at position: SCNVector3,rotation: SCNVector4) {
guard let scene = SCNScene(named: "Assets.scnassets/ShadowTest.scn") else {
return
}
let node = SCNNode()
scene.rootNode.childNodes.forEach(node.addChildNode(_:))
node.position = position
node.rotation = rotation
updateQueue.async { [weak self] in
self?.sceneView.scene.rootNode.addChildNode(node)
}
}
将节点添加到我的场景中后,阴影完全不可见。
该灯似乎正在工作,因为它应该照亮了。
我在灯光/平面/立方体/场景视图上缺少一些非常明显的设置吗?
非常感谢您提供所有帮助。
解决方法
您应该可以在代码中执行此操作。试试看吧:
func directionalLight() -> SCNLight {
let light = SCNLight()
light.type = .directional
light.castsShadow = true
light.color = UIColor.white
light.shadowColor = UIColor(red: 0,green: 0,blue: 0,alpha: 0.75)
light.shadowMode = .deferred
light.shadowRadius = 2.0
light.shadowCascadeCount = 3
light.shadowCascadeSplittingFactor = 0.09
light.shadowBias = 0.1
light.shadowSampleCount = 8 // (the smaller the value,the better the performance)
light.categoryBitMask = -1 // Shine on Everything
return light
}
这是材料的
func shadowMaterialStandard() -> SCNMaterial {
let material = SCNMaterial()
material.diffuse.contents = UIColor(red: 1.0,green: 1.0,blue: 1.0,alpha: 1.0)
material.lightingModel = .physicallyBased
material.colorBufferWriteMask = SCNColorMask(rawValue: 0)
material.isLitPerPixel = false
material.isDoubleSided = true
return material
}
对于SCNPlane(影子平面)而言,这是这样的:
func setupShadowPlane() {
let objectShape = SCNPlane(width: 50.0,height: 50.0)
objectShape.heightSegmentCount = 1
objectShape.widthSegmentCount = 1
let objectNode = SCNNode(geometry: objectShape)
objectNode.renderingOrder = -10 // for Shadow Material Standard
objectNode.position = initialStartPosition
objectNode.geometry?.firstMaterial = shadowMaterialStandard()
objectNode.castsShadow = false // Important
objectNode.eulerAngles = SCNVector3(-90.degreesToRadians,0.0,0.0)
objectNode.name = "floor"
objectNode.physicsBody = Physics.floorPhysicsBody(shape: objectShape)
sceneView.scene.rootNode.addChildNode(objectNode)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。