Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DavidDebnar · Jun 23, 2013 at 10:30 AM · 2dspritessprite animation

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sprite Animation - Oneshot 0 Answers

Circular 9 slice? Draw a circle with constant thickness 1 Answer

Why is my sprite not the original resolution? 2 Answers

Having trouble changing sprites in 2 dimensional animation 1 Answer

I have an animaiton problem about sprite loading. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges