结论先行
在目前(2022 LTS / 2023.2)的 SRP API 里,脚本侧 无法(也不被官方支持)“手写”一个 CullingResults 并把它塞回 ScriptableRenderContext。想要跳过 context.Cull()、自己组装可见物体列表,从而继续用 DrawRenderers()/RendererList 一整套下游流程,是做不到的。
为什么做不到
-
CullingResults 是 native struct
- 在 C# 里它只暴露了一个 空壳:没有 public 构造函数,也没有 public setter。
- 真正的数据(VisibleLights、VisibleReflectionProbes、BatchCullingOutput 等)在 C++ 层维护,并通过内部句柄与 SRP 下游渲染管线联动。
-
下游 API 严格要求来自内部的句柄
ScriptableRenderContext.DrawRenderers() / RendererList / RenderGraph 都会校验传入的 CullingResults 句柄是否合法。这个校验在 C++ 侧完成,脚本无法伪造。
- 即使你用
UnsafeUtility 在托管侧构造内存布局,也无法生成对应的 native 资源 ID,调用时依旧会触发 “Invalid CullingResults” 报错或直接崩溃。
-
Unity 没有留“扩展点”
- SRP 中真正可自定义的是 “光照 / 渲染Pass / 后处理”,而剔除(Hierarchical Z-Buffer + BVH + 批剔除等)仍然被视为引擎核心不可替换的部分,暂未暴露替换接口。
变通 / 替代方案
如果你想:
A. 完全自定义剔除逻辑
• 可以自己在 CPU/JobSystem/ComputeShader 中做剔除,拿到可见对象列表后——
① 用 Graphics.DrawMesh*() 或 CommandBuffer.Draw*() 直接下发绘制;
② 或用 BatchRendererGroup(BRG)API:你负责提供 GetBatchMatrices, GetInstanceData, 并在 OnPerformCulling 回调里返回 BatchCullingResult。这里的 CullingResult 和 SRP 的 CullingResults 概念不同,是完全开放给脚本侧的。
B. 只想“全量渲染 / 不剔除”
• 可以把 ScriptableCullingParameters.cullingOptions 设置为 0,但 context.Cull() 依旧必须调用,它只是会把所有 renderer 都标记为可见。
C. 部分控制剔除
• 利用 ScriptableCullingParameters.SetLayerMask, shadowDistance, cullingMatrix 等 API 去影响引擎剔除,而不是完全重写。
核心 API 速览(可行路径)
// 标准 SRP 流程(必须存在)
var cullingParams = new ScriptableCullingParameters();
if (!camera.TryGetCullingParameters(out cullingParams)) return;
cullingParams.cullingOptions = CullingOptions.None; // 不想剔除可以全关
var cullingResults = context.Cull(ref cullingParams); // 依赖引擎生成
// 后续仍可用 cullingResults
context.DrawRenderers(
cullingResults, ref drawingSettings, ref filteringSettings
);
// BatchRendererGroup 自定义剔除
public void OnPerformCulling(
BatchRendererGroup group,
BatchRendererCullingContext ctx,
ref BatchCullingOutput output)
{
// 这里完全由你决定哪些实例 visible
// 填充 output.drawCommands / output.visibleInstances...
}
总结
• CullingResults 本身不能也不需要自己手填。
• 若真要完全掌控剔除,请走 BatchRendererGroup 或手动 Graphics.DrawMesh* 路线;否则照常调用 context.Cull(),通过 ScriptableCullingParameters 间接调控即可。