Version: 2020.2

Light.layerShadowCullDistances

切换到手册
public float[] layerShadowCullDistances ;

描述

Per-light, per-layer shadow culling distances. Directional lights only.

动态阴影可以从远离摄像机很远的阴影投射物投射到视野中。入射光角度较小时,这可能导致许多对象需要投射动态阴影,继而可能导致阴影贴图生成期间渲染成本过高。

使用 Light.layerShadowCullDistances,您可以在每层的基础上限制在距离摄像机多远时允许阴影投射物出现(在超过此距离后,将从阴影贴图生成中剔除这些投射物)。该功能是对 Camera.layerCullDistances 的补充,但仅影响阴影投射,不影响常规对象渲染。

就像 Camera.layerCullDistances 一样,Light.layerShadowCullDistances 要求您分配恰好 32 个值的浮点数组。层索引中的距离为 0 意味着保持该层的当前行为。分配 null 将完全禁用阴影距离剔除,与传递 32 个零的数组的效果完全相同。

默认情况下,每层阴影剔除将使用与摄像机对齐的平面。可以通过将 Camera.layerCullSpherical 设置为 true 来更改为球面。该标志的效果对于 Camera.layerCullDistancesLight.layerShadowCullDistances 是相同的。

请注意,使用 Camera.layerCullDistances 来限制摄像机剔除距离时,还会将阴影投射距离限制为与剔除距离相同的值。因此,如果对*同一层索引*同时使用 Camera.layerCullDistancesLight.layerShadowCullDistances,则该层的有效阴影剔除距离将是这两个距离中的最小值。如果层索引中其中一个值为零,则会直接使用另一个值;如果层索引中两个值均为零,则不会对该层应用特殊的剔除行为。

Only works with Directional lights.

另请参阅:Camera.layerCullDistancesCamera.layerCullSpherical

using UnityEngine;

[RequireComponent(typeof(Light))] public class LayerShadowCullDistancesExample : MonoBehaviour { void OnEnable() { // Setup shadow cull distances var shadowCullDistances = new float[32]; shadowCullDistances[10] = 5f; // Let's imagine this is our 'Tiny Objects' layer shadowCullDistances[11] = 15f; // Let's imagine this is our 'Small Things' layer shadowCullDistances[12] = 100f; // Let's imagine this is our 'Trees' layer

// Assign shadow cull distances. This will only affect layers 10, 11 and 12. GetComponent<Light>().layerShadowCullDistances = shadowCullDistances; }

void OnDisable() { // Completely disable shadow cull distances GetComponent<Light>().layerShadowCullDistances = null; } }