【如何降低Unity程序的Drawcall】
Unity can combine a number of objects at runtime and draws them together with a single draw call. This operation is called “batching”
每帧能够有多少batch依赖于cpu。每个drawcall提交多少个三角形,对cpu压力变化不大,但是每帧有多少个drawcall则影响很明显。
Currently, only Mesh Renderers、Trail Renderers、Line Renderers、Particle Systems and Sprite Renderers are batched. This means that skinned Meshes, Cloth, and other types of rendering components are not batched.
Skinned Meshes 不能被合批。
一、Dynamic Batching。
Dynamic Batching会自动进行。有与顶点呈线性相关的overhead。所以只对顶点数小于900的mesh进行batch。
1)如果 Position, Normal and Single UV, then you can batch up to 300 verts
2)Position, Normal, UV0, UV1 and Tangent, then only 180 verts
动态Batching有以下要求:
1、Using different Material instances causes GameObjects not to batch together, even if they are essentially the same. The exception is shadow caster rendering.
使用同一个Material实例。
2、Lightmapped objects s hould point to exactly the same lightmap location to be batched。
想被batching的对象必须有相同的lightindex、及lightmapoffset/lightmapscale。
3、Objects that receive real-time shadows will not be batched。
勾选receive real-time shadow的对象不会被batch。
4、Batching dynamic objects has certain overhead per vertex,If your shader is using Vertex Position, Normal and single UV, then you can batch up to 300 verts; whereas if your shader is using Vertex Position, Normal, UV0, UV1 and Tangent, then only 180 verts.
5、 GameObjects are not batched if they contain mirroring on the transform (for example GameObject A with +1 scale and GameObject B with –1 scale cannot be batched together).
scale正负值相反的,没法动态合批。
6、Almost all Unity Shaders support several Lights in forward rendering, effectively doing additional passes for them. The draw calls for “additional per-pixel lights” are not batched.
前身渲染中的 additional pass不会被合批。
什么时候动态合批才有用?
Because it works by transforming all GameObject vertices into world space on the CPU, it is only an advantage if that work is smaller than doing a draw call. The resource requirements of a draw call depends on many factors, primarily the graphics API used. For example, on consoles or modern APIs like Apple Metal, the draw call overhead is generally much lower, and often dynamic batching cannot be an advantage at all.
合批要把 vertex 转换到 world space。所以只有在 draw-call 时间大于 vertex transformation 时间时,合批才有效。在Apple的Metal上,draw-call 的开销已经非常小了,所以动态合批没有什么价值。
二、Static Batching。
Using static batching will require additional memory for storing the combined geometry. If several objects shared the same geometry before static batching, then a copy of geometry will be created for each object. Sometimes you will have to sacrifice rendering performance by avoiding static batching for some objects to keep a smaller memory footprint.
使用StaticBatching会产生额外内存使用,因为Unity会把多个对象Join成一个对象。
StaticBatchingUtility.Combine()告诉UnityEngine把staticRoot作为static对象来处理。
once combined children can NOT change their Transform
properties, however staticBatchRoot
can be moved.
三、其它降低Drawcall的方法
1、改变表现方法。如流水粒子改为流水动画。
2、高级特性Shader降级为统一的低级特性的Shader。
参考:
2、http://docs.unity3d.com/Manual/DrawCallBatching.html