Version: Unity 6.0 (6000.0)
语言 : 中文
创建剔除组 (Culling Group)
Troubleshooting occlusion culling

获取剔除结果

通过 onStateChanged 回调来接收结果

为响应球体而更改其可见性或距离状态的最有效方法是使用 onStateChanged 回调字段。将其设置为一个函数,该函数以 CullingGroupEvent 结构作为参数;对于已改变状态的每个球体,将在剔除完成后调用此函数。CullingGroupEvent 结构的成员会告诉您球体的先前状态和新状态。

group.onStateChanged = StateChangedMethod;

private void StateChangedMethod(CullingGroupEvent evt)
{
    if(evt.hasBecomeVisible)
        Debug.LogFormat("Sphere {0} has become visible!", evt.index);
    if(evt.hasBecomeInvisible)
        Debug.LogFormat("Sphere {0} has become invisible!", evt.index);
}

通过 CullingGroup 查询 API 来接收结果

除了 onStateChanged 委托之外,CullingGroup 还提供一个 API,用于检索包围球体数组中任何球体的最新可见性和距离结果。要检查单个球体的状态,请使用 IsVisible 和 GetDistance 方法:

bool sphereIsVisible = group.IsVisible(0);
int sphereDistanceBand = group.GetDistance(0);

要检查多个球体的状态,可使用 QueryIndices 方法。此方法将扫描连续范围的球体以查找与指定可见性或距离状态相匹配的球体。

// Allocate an array to hold the resulting sphere indices - the size of the array determines the maximum spheres checked per call
int[] resultIndices = new int[1000];
// Also set up an int for storing the actual number of results that have been placed into the array
int numResults = 0;

// Find all spheres that are visible
numResults = group.QueryIndices(true, resultIndices, 0);
// Find all spheres that are in distance band 1
numResults = group.QueryIndices(1, resultIndices, 0);
// Find all spheres that are hidden in distance band 2, skipping the first 100
numResults = group.QueryIndices(false, 2, resultIndices, 100);

请记住,仅在 CullingGroup 使用的摄像机实际执行剔除时,才更新查询 API 检索的信息。

创建剔除组 (Culling Group)
Troubleshooting occlusion culling