为什么SceneKit聚光灯角度会影响阴影?

Pra*_*idu 5 shadow augmented-reality scenekit swift arkit

我正在尝试在场景中使用聚光灯并向对象添加阴影。然而,我注意到当我增加 时spotInnerAngle,物体的阴影会发生显着变化。这是一个例子:

\n\n

在此输入图像描述

\n\n

这些图像中的两个阴影看起来完全不同 \xe2\x80\x93 有谁知道为什么增加聚光角会导致阴影不那么明显?

\n\n

这是我用来创建聚光灯/向场景添加阴影的代码:

\n\n
    let spotLight = SCNNode()\n    spotLight.light = SCNLight()\n    spotLight.light?.type = SCNLight.LightType.spot\n    spotLight.light?.spotInnerAngle = 120\n    spotLight.light?.spotOuterAngle = 120\n    spotLight.light?.color = UIColor.white\n    spotLight.light?.castsShadow = true\n    spotLight.light?.automaticallyAdjustsShadowProjection = true\n    spotLight.light?.shadowSampleCount = 32\n    spotLight.light?.shadowRadius = 8\n    spotLight.light?.shadowMode = .deferred\n    spotLight.light?.shadowMapSize = CGSize(width: 2048, height: 2048)\n    spotLight.light?.shadowColor = UIColor.black.withAlphaComponent(1)\n    spotLight.position = SCNVector3(x: 0,y: 5,z: 0)\n    spotLight.eulerAngles = SCNVector3(-Float.pi / 2, 0, 0)\n
Run Code Online (Sandbox Code Playgroud)\n

ARG*_*Geo 0

SceneKit 的引擎计算阴影的方式与 3D 软件应用程序(例如 Maya 或 3dsMax)的计算方式略有不同。在 SceneKit 框架中, Spotlight 的positionscale以及它的值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)
Run Code Online (Sandbox Code Playgroud)

另外,我建议您使用Ambient Light非常低的亮度Intensity来照亮 3D 模型上的黑暗区域

希望这可以帮助。