Sprite set color of vertices on individual meshes.

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:

private void SetVertexColors(Color32[] _vertexColors, Sprite _sprite) {
        // set Colors NativeArray from Array
        NativeArray<Color32> _vertexColorsNA = new NativeArray<Color32>(_vertexColors, Allocator.Temp);

        // set Vertex Colors
        _sprite.SetVertexAttribute(VertexAttribute.Color, _vertexColorsNA);

        // dispose NativeArray
        _vertexColorsNA.Dispose();
}

The issue is, vertex colors are changed for every Sprite instance:
8891328--1215891--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?

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
1 Like

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.

1 Like


[edit] it batches nicely, 'GPU instancing' na material need to be turned off.