Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Sprite set color of vertices on individual meshes.

Discussion in '2D' started by ptm_oo, Mar 21, 2023.

  1. ptm_oo

    ptm_oo

    Joined:
    Oct 30, 2014
    Posts:
    32
    I'm trying to assign vertex colors to Sprite, different for each instance.

    I've managed to write a script that sets vertex colors for Sprite mesh:

    Code (CSharp):
    1.  
    2. private void SetVertexColors(Color32[] _vertexColors, Sprite _sprite) {
    3.         // set Colors NativeArray from Array
    4.         NativeArray<Color32> _vertexColorsNA = new NativeArray<Color32>(_vertexColors, Allocator.Temp);
    5.  
    6.         // set Vertex Colors
    7.         _sprite.SetVertexAttribute(VertexAttribute.Color, _vertexColorsNA);
    8.  
    9.         // dispose NativeArray
    10.         _vertexColorsNA.Dispose();
    11. }
    The issue is, vertex colors are changed for every Sprite instance:
    ice_screenshot_20230321-122600.png

    Is there a way to make vertex colors individual for every Sprite?
    From what I know, Sprite Renderer sets vertex color with Color on individual level somehow. How to copy that behavior?
     
    Last edited: Mar 21, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    33,467
    I imagine you probably just need to clone the sprite first, then change that clone.

    Steps:

    - get a reference to the original Sprite
    - clone it with
    Instantiate<Sprite>( original);

    - modify that copied Sprite
    - assign that copy to the SpriteRenderer
     
    ptm_oo likes this.
  3. ptm_oo

    ptm_oo

    Joined:
    Oct 30, 2014
    Posts:
    32
    Thank you, that actually works!

    One downside of this is - Sprites no longer are 'saved by batching', which is real issue for eg. grass objects.
     
    Kurt-Dekker likes this.
  4. ptm_oo

    ptm_oo

    Joined:
    Oct 30, 2014
    Posts:
    32
    [edit] it batches nicely, 'GPU instancing' na material need to be turned off.