每光源、每层阴影剔除距离。
动态阴影可以从远离摄像机很远的阴影投射物投射到视野中。入射光角度较小时,这可能导致许多对象需要投射动态阴影,继而可能导致阴影贴图生成期间渲染成本过高。
使用 Light.layerShadowCullDistances,您可以在每层的基础上限制在距离摄像机多远时允许阴影投射物出现(在超过此距离后,将从阴影贴图生成中剔除这些投射物)。该功能是对 Camera.layerCullDistances 的补充,但仅影响阴影投射,不影响常规对象渲染。
就像 Camera.layerCullDistances 一样,Light.layerShadowCullDistances 要求您分配恰好 32 个值的浮点数组。层索引中的距离为 0 意味着保持该层的当前行为。分配 null 将完全禁用阴影距离剔除,与传递 32 个零的数组的效果完全相同。
默认情况下,每层阴影剔除将使用与摄像机对齐的平面。可以通过将 Camera.layerCullSpherical 设置为 true 来更改为球面。该标志的效果对于 Camera.layerCullDistances 和 Light.layerShadowCullDistances 是相同的。
请注意,使用 Camera.layerCullDistances 来限制摄像机剔除距离时,还会将阴影投射距离限制为与剔除距离相同的值。因此,如果对*同一层索引*同时使用 Camera.layerCullDistances 和 Light.layerShadowCullDistances,则该层的有效阴影剔除距离将是这两个距离中的最小值。如果层索引中其中一个值为零,则会直接使用另一个值;如果层索引中两个值均为零,则不会对该层应用特殊的剔除行为。
另请参阅:Camera.layerCullDistances、Camera.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; } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.