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?