- Home /
Question by
$$anonymous$$ · Oct 06, 2017 at 06:52 PM ·
texture2dpixelpaint
Painting issue, drawing is not consistent, how to fix?
Hello everyone! Im currently trying to paint a texture, a bit of code and it works, but i have a problem, if i move my mouse too quickly, painting has gaps, how can i fix this? Thanks in advance!
Texture2D tex = GetComponent<Image>().sprite.texture;
RectTransform rect = GetComponent<RectTransform>();
Vector2 pos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, Input.mousePosition, null, out pos);
pos = new Vector2(pos.x + rect.sizeDelta.x / 2, pos.y + rect.sizeDelta.y / 2);
for (int i = 0; i < brushSize; i++)
{
for (int j = 0; j < brushSize; j++)
{
tex.SetPixel(Mathf.FloorToInt(pos.x - 1 * brushSize / 2 + j), Mathf.FloorToInt(pos.y + 1 * brushSize / 2 - i), color);
}
}
tex.Apply();
gifka2.gif
(169.7 kB)
Comment
Best Answer
Answer by FortisVenaliter · Oct 06, 2017 at 07:31 PM
You need to interpolate between the mouse's last position and it's current position. Determine a step-size (a good starting point would be half the width of the brush) first. Then, if the mouse moved more than that step size in one frame, use the Vector.Lerp function to step across it and apply multiple brush points on that frame.
By the way, good on you for including the GIF. That made your question crystal clear.