Shadows in Unity 阴影

优质
小牛编辑
136浏览
2023-12-01
Desktop

Unity 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.

Unity专业版功能,在任何灯光都可以使用实时阴影。物体可以投射阴影到彼此的上面,以及投射到自身上(自身阴影)。包括灯光的所有类型,平行光,聚光灯和点光灯,支持阴影。

Using shadows can be as simple as choosing Hard Shadows or Soft Shadows on a Light. However, if you want optimal shadow quality and performance, there are some additional things to consider.

使用阴影可以简单的在灯光上选择硬阴影或软阴影。不过,如果你想要最佳的阴影质量和性能,有一些其他的事情要考虑。

The Shadow Troubleshooting page contains solutions to common shadowing problems.

阴影故障排除页包含常见的阴影问题解决方法。

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...

奇怪的是,最好的阴影并不是实时的!只要是游戏关卡的物理和灯光师静态的,才在3D程序中预先计算光照贴图。离线计算阴影总能比实时阴影产生更高质量和性能。

Tweaking shadow quality 调整阴影质量

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:

Unity使用阴影贴图来显示阴影。阴影贴图是基于纹理的方式,最简单的方法把它看作是"阴影纹理",从灯光投射阴影到场景。因此很像普通的纹理,阴影贴图的质量主要取决于两个因素:

  1. The resolution (size) of the shadow maps. The larger the shadow maps, the better the shadow quality.
    阴影贴图的分辨率(大小)。 较大的阴影贴图,阴影质量更好。
  2. 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米外不计算显示阴影。因此,制作游戏尽可能的降低阴影的距离,这将有助于提高性能(和提高平行光阴影质量,见上文)。

Hardware support for shadows 阴影的硬件支持

Built-in shadows require a fragment program (pixel shader 2.0) capable graphics card. This is the list of supported cards:

内置的阴影需要有一个小程序(pixel shader 2.0)提供的显卡。这是支持的显卡列表:

  • On Windows:
    • ATI Radeon 9500 and up, Radeon X series, Radeon HD series.
    • NVIDIA GeForce 6xxx, 7xxx, 8xxx, 9xxx, GeForce GT, GTX series.
    • Intel GMA X3000 (965) and up.
  • On Mac OS X:
    • Mac OS X 10.4.11 or later.
    • ATI Radeon 9500 and up, Radeon X, Radeon HD series.
    • NVIDIA GeForce FX, 6xxx, 7xxx, 8xxx, 9xxx, GT, GTX series.
    • Intel GMA 950 and later.
      • Soft shadows are disabled because of driver bugs (hard shadows will be used instead).
        软阴影被禁用,因为驱动程序的错误(将使用硬阴影替代)。

Notes 注意

  • Forward rendering path supports only one directional shadow casting light. Vertex Lit rendering path does not support realtime shadows.
    正向渲染路径仅支持一个平行光阴影投射灯。顶点光照渲染路径不支持实时阴影。
  • Vertex-lit lights don't have shadows.
    顶点光照灯光没有阴影。
  • Vertex-lit materials won't receive shadows (but do cast shadows).
    顶点光照材质不接收阴影(但能投射阴影)。
  • Transparent objects don't cast or receive shadows. Transparent Cutout objects do cast and receive shadows.
    透明物体不投射或接收阴影。透明抠图物体能投射和接收阴影。
iOS

This feature is currently not supported for iOS targets.

此功能目前不支持的iOS设备。

Android

This feature is currently not supported for Android targets.

此功能目前不支持的Android设备。

页面最后更新: 2010-09-24