Search Unity

Question Setting Vertex Color for Mesh vs Sprite Performance

Discussion in 'Scripting' started by ptm_oo, Mar 22, 2023.

  1. ptm_oo

    ptm_oo

    Joined:
    Oct 30, 2014
    Posts:
    35
    There is significant performance issue with setting Vertex Colors for Sprite.
    Setting vertex colors for Sprite, 3x slower than for Mesh..
    Code (CSharp):
    1.  
    2. private void SetVertexColors(Color32[] _vertexColors) {
    3.             // set Colors NativeArray from Array
    4.             NativeArray<Color32> _vertexColorsNA = new NativeArray<Color32>(_vertexColors, Allocator.Temp);
    5.             // set Vertex Colors for Sprite
    6.             sprite.SetVertexAttribute(VertexAttribute.Color, _vertexColorsNA);
    7.             // dispose NativeArray
    8.             _vertexColorsNA.Dispose();
    9. }
    10.  
    "SetVertexAttribute" part is the most costly.

    Setting vertex colors for mesh as usual:
    Code (CSharp):
    1. private void SetVertexColors(Color32[] _vertexColors) {
    2.           // set Vertex Colors for Mesh
    3.           mesh.colors32 = _vertexColors;
    4. }
    Is there anything that could be done to improve Sprite performance?
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,063
    Is it just the sprite.SetVertexAttribute(VertexAttribute.Color) which is slow or is it because you are making and disposing an array
     
  3. ptm_oo

    ptm_oo

    Joined:
    Oct 30, 2014
    Posts:
    35
    sprite.SetVertexAttribute(VertexAttribute.Color) alone is making things slow.

    And it's really an issue, calling this every frame:
    For 1000 Sprites - FPS drops to 30.
    For 1000 Meshes - FPS it over 100.
     
  4. ptm_oo

    ptm_oo

    Joined:
    Oct 30, 2014
    Posts:
    35
    Does anyone here or from Unity know more about this? How to set Sprite vertex colors faster? (at least as fast as Meshes)