- Home /
2D Sprite animation: Mesh swap or UV swap?
As stated in the question, what is better to use: mesh swapping or uv swapping? By UV swapping I mean changing the actual UVs of the quad mesh, not material tiling/offset. I'm assuming mesh swapping is worse memory wise, since meshes are larger than just rects, but is the performance better? I'm asking, because I'm developing for mobile and there's going to be a lot (100-200 animating) sprites on the scene and I'd want to know the best way to do it before starting.
--David
Answer by DavidDebnar · Jun 23, 2013 at 02:15 PM
After doing some testing, these are my results:
Mesh swapping seems to be on average 3.4x faster, but the memory size of a rect is 16 bytes, where the size of a mesh is 8192 bytes (simple quad; 4 verts, 2 tris). This means that 1024 frames in UV is 16kb and in Mesh data it's 8mb.
+--------------+-------------+-------------+
| Count | UV [s] | Mesh [s] |
+--------------+-------------+-------------+
| 1 | 0.000420207 | 0.000107616 |
+--------------+-------------+-------------+
| 100 | 0.000431643 | 0.000118762 |
+--------------+-------------+-------------+
| 10 000 | 0.005245706 | 0.001639191 |
+--------------+-------------+-------------+
| 1 000 000 | 0.5222157 | 0.1503426 |
+--------------+-------------+-------------+
| 10 000 000 | 5.099644 | 1.526234 |
+--------------+-------------+-------------+
| 100 000 000 | 51.83892 | 15.30725 |
+--------------+-------------+-------------+
| =111 010 101 | =57.4668772 | =16.9856921 |
+--------------+-------------+-------------+
+--------------+-------------------+---------------------+---------------+
| Count | UV avg. per 1 [s] | Mesh avg. per 1 [s] | Ratio UV/Mesh |
+--------------+-------------------+---------------------+---------------+
| 1 | 0.0004202072 | 0.0001076162 | 3.90468349561 |
+--------------+-------------------+---------------------+---------------+
| 100 | 0.00000431643 | 0.0000011876 | 3.63451869827 |
+--------------+-------------------+---------------------+---------------+
| 10 000 | ~0.00000053 | ~0.00000016 | 3.20017984481 |
+--------------+-------------------+---------------------+---------------+
| 1 000 000 | ~0.00000053 | ~0.00000016 | 3.47350451569 |
+--------------+-------------------+---------------------+---------------+
| 10 000 000 | ~0.00000053 | ~0.00000016 | 3.3413251179 |
+--------------+-------------------+---------------------+---------------+
| 100 000 000 | ~0.00000053 | ~0.00000016 | 3.38655996342 |
+--------------+-------------------+---------------------+---------------+
| =111 010 101 | =0.0000005177 | =0.000000153 | =3.3832520149 |
+--------------+-------------------+---------------------+---------------+
+--------------------------------------------------------------+
| Memory size |
+--------------+-------------+-------------+-------------------+
| UV [bytes] | 16 | = 4 x float | = 4 x 4 bytes |
+--------------+-------------+-------------+-------------------+
| Mesh [bytes] | 8192 | = quad | = 4 verts, 2 tris |
+--------------+-------------+-------------+-------------------+
Code used:
var mesh : Mesh;
var uv : Rect;
var filter : MeshFilter;
var t : float;
var tick : int = 100;
function Start () {
filter = GetComponent.<MeshFilter>();
mesh = filter.mesh;
uv = Rect(0,0,1,1);
Debug.Log("Float size: " + System.Runtime.InteropServices.Marshal.SizeOf(float).ToString() + " bytes");
Debug.Log("UV size: " + System.Runtime.InteropServices.Marshal.SizeOf(Rect).ToString() + " bytes"); // 4x float
Debug.Log("Mesh size: " + (8192).ToString() + " bytes"); // 8,192 = the size of a simple one sided quad
t = Time.realtimeSinceStartup;
for(var i = 0; i < tick; i++)
SetUV(uv,mesh);
t = Time.realtimeSinceStartup - t;
Debug.Log("UV: "+t.ToString());
t = Time.realtimeSinceStartup;
for(i = 0; i < tick; i++)
SetMesh(mesh,filter);
t = Time.realtimeSinceStartup - t;
Debug.Log("Mesh: "+t.ToString());
}
public function SetUV(uv : Rect, mesh : Mesh) {
var newUV = new Vector2[4];
var start = Vector2(uv.x,uv.y);
var size = Vector2(uv.width,uv.height);
newUV[0] = start + size;//UR
newUV[1] = start + Vector2.right * size.x;//BR
newUV[2] = start + Vector2.up * size.y;//UL
newUV[3] = start;//BL
mesh.uv = newUV;
}
public function SetMesh (mesh : Mesh, filter : MeshFilter) {
filter.mesh = mesh;
}
--David