Unity3D高级手册05:Unity中的阴影

Unity 2.0 Pro makes it possible to use real-time shadows on any light. Objects can cast shadows onto each other and onto parts of themselves ("self shadowing"). All types of Lights - Directional, Spot and Point - support shadows.

Curiously enough, the best shadows are non-realtime ones! Whenever your game level geometry and lighting is static, just precompute lightmaps in your 3D application. Computing shadows offline will always result in better quality and performance than displaying them in real time. Now onto the realtime ones...

Unity uses so called shadow maps to display shadows. Shadow mapping is a texture based approach, it's easiest to think of it as "shadow textures" projecting out from lights onto the scene. Thus much like regular texturing, quality of shadow mapping mostly depends on two factors:

The resolution (size) of the shadow maps. The larger the shadow maps, the better the shadow quality. 该阴影图形分辨率（大小）。较大的阴影图形，更好的阴影质量。

The filtering of the shadows. Hard shadows take the nearest shadow map pixel. Soft shadows average several shadow map pixels, resulting in smoother looking shadows (but soft shadows are more expensive to render). 过滤的阴影。硬阴影采取就近的阴影图形像素。软阴影平均几个阴影图形像素，阴影看起来平滑的结果（但软阴影的渲染比较昂贵）。

Different Light types use different algorithms to calculate shadows. 不同的类型的光源使用不同的算法来计算阴影。

For Directional lights, the crucial settings for shadow quality are Shadow Distance and Shadow Cascades, found in Quality Settings. Shadow Resolution is also taken into account, but the first thing to try to improve directional shadow quality is reducing shadow distance. All the details about directional light shadows can be found here: Directional Shadow Details.

For Spot and Point lights, Shadow Resolution determines shadow map size. Additionally, for lights that cover small area on the screen, smaller shadow map resolutions are used. 对于聚光光源和点光源，阴影清晰度决定阴影映射的大小。此外，对于光源覆盖在屏幕上小的面积，小的阴影图形的清晰度被使用。

Details on how shadow map sizes are computed are in Shadow Size Details page.阴影图形尺寸的详情如何计算在阴影大小详细信息页。

Shadow performance 阴影性能

Realtime shadows are quite performance hungry, so use them sparingly. For each light to render its shadows, first any potential shadow casters must be rendered into the shadow map, then all shadow receivers are rendered with the shadow map. This makes shadow casting lights even more expensive than Pixel lights, but hey, computers are getting faster as well! 实时阴影要求相当高的性能，所以要适可而止。对于每一个光渲染的它自己的阴影，首先调整任何潜在的阴影都必须进入到阴影图形里，那么所有的阴影接收阴影图形渲染。这使得阴影投影甚至比像素光源昂贵。但是，嘿，电脑速度越来越快的！

Soft shadows are more expensive to render than Hard shadows. The cost is entirely on the graphics card though (it's only longer shaders), so Hard vs. Soft shadows don't make any impact on the CPU or memory.

软阴影是较昂贵的渲染比硬阴影。费用完全是图形卡，但（这只是长久的阴影），所以硬阴影与软阴影相比，不受任何CPU或内存的影响

Quality Settings contains a setting called Shadow Distance - this is how far from the camera shadows are drawn. Often it makes no sense to calculate and display shadows that are 500 meters away from the camera, so use as low shadow distance as possible for your game. This will help performance (and will improve quality of directional light shadows, see above). 质量设置包含一个名为阴影距离-这就是远距离摄像机阴影绘制。往往是没有意义的计算和显示阴影，有500米距离相机，因此使用尽可能低的阴影距离为你的游戏。这将有助于性能（和将提高定向阴影质量，见上文）。