用途
该技能用于在 Unity 中为移动端或 WebGL 目标添加、创建或优化后处理效果。触发场景包括:用户提到 post-processing、bloom、vignette、color grading、blur、outline、edge detection、chromatic aberration、Renderer Feature、Full Screen Pass、Blit、post-process mobile,或任何在受限硬件上的屏幕空间特效;也包括用户反馈后处理导致移动端掉帧时。
核心规则
- 每个后处理 Pass 都意味着一次全屏 Blit:从主存读取整个帧缓冲再写回。在基于 Tile 的移动 GPU 上,这会清空 tile memory。中端移动设备上每个 Pass 预算约 0.5–2ms。
- 尽可能把所有效果合并进单个 Pass。Unity 默认后处理栈会把每个效果作为独立 Pass 执行,在移动端仅这一点就可能增加 6ms 以上(实测:某些设备上仅暗角就达到 6ms)。应改用单 Pass 方案。
- 除非明确需要 Bloom,否则禁用 HDR。HDR 会使帧缓冲带宽翻倍(每通道 16 位浮点 vs 8 位)。如果只需要色彩分级,请在 LDR 下进行。
- Bloom 是移动端默认效果中最昂贵的:它需要降采样、模糊和上采样,多个 Pass。如果必须使用,减少迭代次数(最多 2–3 次)、采用半分辨率模糊,并跳过高质量预过滤。
- 移动端绝不使用 Motion Blur 或 Depth of Field,它们需要跨多个 Pass、每片元多次纹理采样。
- 在 URP 中自定义效果时,优先使用 Renderer Features 而非基于 Volume 的后处理,因为 Renderer Features 让你直接控制 Blit 的时机与方式。
单 Pass 后处理模板
文档给出一个把暗角 + 色彩分级 + 简易泛光合并进一个 Pass 的 Shader 示例(Shader "Mobile/PostProcess_SinglePass"),包含 _VignetteIntensity、_VignetteRadius、_Contrast、_Saturation、_Brightness 等属性,使用 UniversalPipeline 标签,并在片元着色器中完成暗角(基于 UV 距离、无额外纹理采样)、亮度/对比度、饱和度(基于亮度点积)计算。完整代码见官方资料。
URP Renderer Feature(C# 设置)
文档给出将后处理 Pass 注入 URP 的 C# 示例 MobilePostProcessFeature,继承 ScriptableRendererFeature,包含 Settings(material、renderPassEvent)与内部 MobilePostProcessPass,在 Execute 中使用 Blitter.BlitCameraTexture 对相机颜色目标做单次 Blit 并执行命令缓冲。完整代码见官方资料。
移动端安全特效(开销排序)
| 效果 | 开销 | 移动端结论 | 说明 |
|---|---|---|---|
| 色彩分级(LDR) | ~0.1ms | 安全 | 纯数学,无额外采样 |
| 暗角 | ~0.1ms | 安全 | 仅 UV 距离计算 |
| 胶片颗粒 | ~0.2ms | 安全 | 由 UV 数学生成噪声,无纹理 |
| 简易描边(深度) | ~0.5ms | 谨慎 | 需要深度纹理 + 4 邻域采样 |
| 泛光(2 次迭代) | ~1–2ms | 谨慎 | 半分辨率,最多 2 次迭代 |
| 色差 | ~0.3ms | 谨慎 | 3 次纹理采样而非 1 次 |
| 模糊(单 Pass 盒式) | ~0.5ms | 谨慎 | 使用 4-tap 盒式模糊,绝不用高斯 |
| 动态模糊 | ~3–5ms | 避免 | 沿速度多次采样 |
| 景深 | ~3–5ms | 避免 | CoC 计算 + 可变模糊 |
| 屏幕空间反射 | ~4–8ms | 避免 | 屏幕空间光线步进 |
| 环境光遮蔽(SSAO) | ~2–4ms | 避免 | 每片元多次深度采样 |
参考仓库
- demonixis/FastPostProcessing:单 Pass 移动端后处理,移动端首选,所有内容放在一个 Pass。
- TPiotr/SimpleMobilePostProcessing-Unity:模糊 + 暗角,因 Unity 暗角单独就耗费 6ms 而创建。
- yahiaetman/URPCustomPostProcessingStack:兼容 URP Volume 的自定义特效,含 8 个带正确 URP 集成的示例。
- QianMo/X-PostProcessing-Library:数十种效果(MIT),桌面质量参考,需为移动端改造。
- GarrettGunnell/Post-Processing:模块化泛光/分级/边缘,含单 Pass 合并笔记。
来源链接
- Febucci: Custom Post-Processing in URP — https://blog.febucci.com/2022/05/custom-post-processing-in-urp/
- ARM Community: Post-Processing on Mobile — https://community.arm.com/arm-community-blogs/b/graphics-gaming-and-vr-blog/posts/post-processing-effects-on-mobile-optimization-and-alternatives
- Unity Forum: Unity 6 Mobile Tips (Bloom/HDR benchmarks) — https://discussions.unity.com/t/unity-6-mobile-graphics-optimization-tips-and-tricks/1593293
- Android Developer: URP Asset Settings — https://developer.android.com/develop/xr/unity/performance/urp-asset-settings
- UWA Blog: Bloom Optimization — https://blog.en.uwa4d.com/2022/12/28/unity-mobile-game-performance-optimization-the-balance-between-graphics-performance-and-gpu-pressure/