性能优化之:Transparent material 透明材质

问题

场景:一个由多个平面叠加的场景 ,每个平面使用半透明的 shader 进行渲染,每个平面的渲染结果互相叠加在最终的目标上,其中每个平面的大小都几乎占满显示区域。

这样的场景原以为不会有很大的渲染部分的耗时,但是性能测试数据却显示,在高通 845 的手机上,仅 3D 渲染部分需要耗时 37.87 ms.

使用 Profiler trace, 得到的结果显示渲染时间占满了每一帧,见下图。

实验

针对这个问题做如下分析:

  1. 半透明渲染的影响

  2. plane 大小的影响

进行不透明渲染,耗时数据对比:

  • 半透明渲染:37.87 ms

  • 不透明渲染:22.38 ms

半透明渲染有较大的耗时增加。因为半透明物体的呈现需要与原始背景作一次混合计算,甚至可能需要分开 diffuse 与 specular 部分分别计算混合。此处的计算逻辑不复杂,但是由于 color buffer 几乎占满屏幕,计算数据量较大。由此进入第二个实验:尝试较小的渲染区域。

减小 plane,耗时数据对比:

  • 原始大小渲染: 37.87 ms

  • 减小到约 1/4 尺寸渲染:18.99 ms

结论

由此可见减小了渲染区域后,渲染耗时下降明显。但是,在实际应用中无法这样粗暴地缩小 plane 的大小,考察该素材发现,虽然 plane 较大,但是实际需要绘制的图案仅占用很小一部分,于是建议设计师将 plane 缩小到接近实际渲染区域,减少不必要的全透明区域渲染计算。

最终素材的 3D 渲染耗时从 46.99 ms 减少到 16.5 ms, FPS 从 14 提升到 17.


附 Filament 的几种不同的混合模式:

  • Opaque: blending is disabled, the alpha channel of the material's output is ignored.

  • Transparent: blending is enabled. The material's output is alpha composited with the
    render target, using Porter-Duff's source over rule. This blending mode assumes
    pre-multiplied alpha.

  • Fade: acts as transparent but transparency is also applied to specular lighting. In
    transparent mode, the material's alpha values only applies to diffuse lighting. This
    blending mode is useful to fade lit objects in and out.

  • Add: blending is enabled. The material's output is added to the content of the
    render target.

  • Multiply: blending is enabled. The material's output is multiplied with the content of the
    render target, darkening the content.

  • Screen: blending is enabled. Effectively the opposite of the multiply , the content of the
    render target is brightened.

  • Masked: blending is disabled. This blending mode enables alpha masking. The alpha channel
    of the material's output defines whether a fragment is discarded or not. See the maskThreshold
    section for more information.

posted @ 2022-05-18 17:27  皮斯卡略夫  阅读(93)  评论(0编辑  收藏  举报