Drawing performance issues. Color32.op_lmplicit() very slow
I try to paint by texture2d and get very poor performance, especially when trying to use a brush texture
public void PenBrush(Vector2 world_point)
{
Vector2 pixelPos = WorldToPixelCoordinates(world_point);
currentColors = drawableTexture.GetPixels32();
if (previousDragPosition == Vector2.zero)
MarkPixelsToColour(pixelPos, PenWidth, PenColour);
else
ColourBetween(previousDragPosition, pixelPos, PenWidth, PenColour);
ApplyMarkedPixelChanges();
previousDragPosition = pixelPos;
}
public void MarkPixelsToColour(Vector2 center_pixel, int pen_thickness, Color color_of_pen)
{
int center_x = (int)center_pixel.x;
int center_y = (int)center_pixel.y;
for (int x = center_x - pen_thickness; x <= center_x + pen_thickness; x++)
{
if (x >= drawableSpriteWidth || x < 0)
continue;
for (int y = center_y - pen_thickness; y <= center_y + pen_thickness; y++)
MarkPixelToChange(x, y, color_of_pen);
}
/*
int center_x = (int)center_pixel.x;
int center_y = (int)center_pixel.y;
int drawableSpriteWidth = drawableSpriteWidth;
pen_thickness = BrushTexture.width;
int x = pen_thickness;
while (x > 0)
{
x--;
int y = pen_thickness;
while (y > 0)
{
y--;
if (x >= (int)drawableSprite.rect.width || x < 0)
continue;
Color brushColor = BrushTexture.GetPixel(x, y);
int array_pos = (y + center_y) * drawableSpriteWidth + (x + center_x);
Color _endcolor = new Color();
_endcolor.r = color_of_pen.r;
_endcolor.g = color_of_pen.g;
_endcolor.b = color_of_pen.b;
_endcolor.a = 1;
currentColors[array_pos] = Color.Lerp(currentColors[array_pos], _endcolor, brushColor.a > .9f ? 100 : brushColor.a / 5);
}
}
*/
public void MarkPixelToChange(int x, int y, Color color)
{
int array_pos = y * drawableSpriteWidth + x;
if (array_pos > currentColors.Length || array_pos < 0)
return;
currentColors[array_pos] = color;
}
public void ApplyMarkedPixelChanges()
{
drawableTexture.SetPixels32(currentColors);
drawableTexture.Apply();
}
How can I improve my performance?
Where does PenColour come from? My first suggestion would be to make it a Color32, and, in general convert or your Color fields and Color parameters to Color32. Could you do that? If you can't, you could convert color_of_pen to Color32 outside of the loop in MarkPixelsToColour and pass a Color32 to MarkPixelToChange.
EDIT
That code seems very prone to get exponentially slow as things scale. If the pen size doubles width and height, and the distance doubles too, you'd be drawing 8 times the amount of pixels. A way to handle it could be a coroutine; you draw a certain amount of pixels, and if there are still pixels to be drawn, you yield, then continue in the next frame. Another idea would be to use Burst and Jobs, setting multiple values in a collection can be a lot faster that way.
Here's an example of painting a texture with Burst and Jobs: https://github.com/Unity-Technologies/UnityTextureAccessApiExamples