ARKit + SceneKit 不渲染任何阴影
ARKit + SceneKit not rendering any shadows
我正在使用 ARKit 和 SceneKit 渲染一个非常简单的场景,其中有一个球体悬停在平面上方。但是,无论我尝试什么,我都无法渲染阴影。球体正确遮挡了光线,但没有绘制阴影。
这是我的完整 ARSCNView
:
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
public var baseNode = SCNNode()
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
sceneView.autoenablesDefaultLighting = false
sceneView.automaticallyUpdatesLighting = false
sceneView.rendersCameraGrain = true
sceneView.preferredFramesPerSecond = 0
sceneView.debugOptions = [.showBoundingBoxes]
let scene = SCNScene()
sceneView.scene = scene
self.baseNode = SCNNode()
baseNode.position.z -= 1 // draw in front of viewer at arts
self.sceneView.scene.rootNode.addChildNode(baseNode)
// Plane to catch shadows
let shadowCatcher = SCNNode(geometry: SCNPlane(width: 0.5, height: 0.5))
shadowCatcher.name = "shadow catcher"
shadowCatcher.castsShadow = false
shadowCatcher.renderingOrder = -10
let groundMaterial = SCNMaterial()
groundMaterial.lightingModel = .constant
groundMaterial.isDoubleSided = true
shadowCatcher.geometry!.materials = [groundMaterial]
self.baseNode.addChildNode(shadowCatcher)
// A shere that should cast shadows
let sphere = SCNNode(geometry: SCNSphere(radius: 0.05))
sphere.position = SCNVector3(0, 0, 0.3)
sphere.castsShadow = true
baseNode.addChildNode(sphere)
// The light
let light = SCNLight()
light.type = .spot
light.intensity = 1000
light.castsShadow = true
light.shadowMode = .deferred
light.automaticallyAdjustsShadowProjection = true
light.shadowMapSize = CGSize(width: 2048, height: 2048)
let lightNode = SCNNode()
lightNode.name = "light"
lightNode.light = light
lightNode.position = SCNVector3(0, 0, 2)
lightNode.look(at: SCNVector3(0, 0, 0))
baseNode.addChildNode(lightNode)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARWorldTrackingConfiguration()
if ARWorldTrackingConfiguration.supportsFrameSemantics(.personSegmentation) {
configuration.frameSemantics.insert(.personSegmentation)
}
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}
}
为什么不渲染阴影?如果我使用普通的 SCNView 而不是 ARSCNView
,相同的基本场景图会渲染阴影
这是由启用 personSegmentation
框架语义引起的。删除它后,应该再次正确渲染阴影:
这花了我很长时间才找到,看起来像是一个错误。我已针对 Apple 提出问题,但不幸的是,目前我不知道有任何解决方法
我正在使用 ARKit 和 SceneKit 渲染一个非常简单的场景,其中有一个球体悬停在平面上方。但是,无论我尝试什么,我都无法渲染阴影。球体正确遮挡了光线,但没有绘制阴影。
这是我的完整 ARSCNView
:
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
public var baseNode = SCNNode()
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
sceneView.autoenablesDefaultLighting = false
sceneView.automaticallyUpdatesLighting = false
sceneView.rendersCameraGrain = true
sceneView.preferredFramesPerSecond = 0
sceneView.debugOptions = [.showBoundingBoxes]
let scene = SCNScene()
sceneView.scene = scene
self.baseNode = SCNNode()
baseNode.position.z -= 1 // draw in front of viewer at arts
self.sceneView.scene.rootNode.addChildNode(baseNode)
// Plane to catch shadows
let shadowCatcher = SCNNode(geometry: SCNPlane(width: 0.5, height: 0.5))
shadowCatcher.name = "shadow catcher"
shadowCatcher.castsShadow = false
shadowCatcher.renderingOrder = -10
let groundMaterial = SCNMaterial()
groundMaterial.lightingModel = .constant
groundMaterial.isDoubleSided = true
shadowCatcher.geometry!.materials = [groundMaterial]
self.baseNode.addChildNode(shadowCatcher)
// A shere that should cast shadows
let sphere = SCNNode(geometry: SCNSphere(radius: 0.05))
sphere.position = SCNVector3(0, 0, 0.3)
sphere.castsShadow = true
baseNode.addChildNode(sphere)
// The light
let light = SCNLight()
light.type = .spot
light.intensity = 1000
light.castsShadow = true
light.shadowMode = .deferred
light.automaticallyAdjustsShadowProjection = true
light.shadowMapSize = CGSize(width: 2048, height: 2048)
let lightNode = SCNNode()
lightNode.name = "light"
lightNode.light = light
lightNode.position = SCNVector3(0, 0, 2)
lightNode.look(at: SCNVector3(0, 0, 0))
baseNode.addChildNode(lightNode)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARWorldTrackingConfiguration()
if ARWorldTrackingConfiguration.supportsFrameSemantics(.personSegmentation) {
configuration.frameSemantics.insert(.personSegmentation)
}
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}
}
为什么不渲染阴影?如果我使用普通的 SCNView 而不是 ARSCNView
这是由启用 personSegmentation
框架语义引起的。删除它后,应该再次正确渲染阴影:
这花了我很长时间才找到,看起来像是一个错误。我已针对 Apple 提出问题,但不幸的是,目前我不知道有任何解决方法