我正在尝试在场景中使用聚光灯并为对象添加阴影。但是,我注意到当我增加spotInnerAngle
时,对象的阴影会发生显着变化。这是一个例子:
这些图像中的两个阴影看起来都非常不同 - 有谁知道为什么增加光斑角度会导致阴影不那么明显?
这是我用来为场景创建聚光灯/添加阴影的代码:
let spotLight = SCNNode()
spotLight.light = SCNLight()
spotLight.light?.type = SCNLight.LightType.spot
spotLight.light?.spotInnerAngle = 120
spotLight.light?.spotOuterAngle = 120
spotLight.light?.color = UIColor.white
spotLight.light?.castsShadow = true
spotLight.light?.automaticallyAdjustsShadowProjection = true
spotLight.light?.shadowSampleCount = 32
spotLight.light?.shadowRadius = 8
spotLight.light?.shadowMode = .deferred
spotLight.light?.shadowMapSize = CGSize(width: 2048, height: 2048)
spotLight.light?.shadowColor = UIColor.black.withAlphaComponent(1)
spotLight.position = SCNVector3(x: 0,y: 5,z: 0)
spotLight.eulerAngles = SCNVector3(-Float.pi / 2, 0, 0)
SceneKit的引擎计算阴影的程度与3D软件应用程序略有不同,如Maya或3dsMax。在SceneKit框架中,聚光灯的position
和scale
以及它的cone angle
值对于阴影生成至关重要。主要规则如下:当SceneKit中聚光灯的射线面积变大时,阴影边缘变得更加模糊(也称为模糊)。
这是使用聚光灯时必须考虑的属性:
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = .spot
lightNode.rotation = SCNVector4(x: 0, y: 0, z: 0, w: 1)
lightNode.castsShadow = true
/* THESE SEVEN SPOTLIGHT PROPERTIES AFFECT SHADOW'S APPEARANCE */
lightNode.position = SCNVector3(0, 10, 0)
lightNode.scale = SCNVector3(7, 7, 7)
lightNode.light?.spotOuterAngle = 120
lightNode.light?.shadowRadius = 10
lightNode.light?.zNear = 0
lightNode.light?.zFar = 1000000
lightNode.light?.shadowSampleCount = 20
lightNode.light?.shadowColor = UIColor(write: 0, alpha: 0.75)
lightNode.light?.shadowMode = .deferred
scene.rootNode.addChildNode(lightNode)
另外,我建议您使用Ambient Light
和非常低的Intensity
来照亮3D模型上的暗区。
希望这可以帮助。