【如何降低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