画图改进版,
链接: http://pan.baidu.com/s/1jHn8b9W 密码: uiu5
public static void Paint(Vector2 from, Vector2 to, Color color, int burshSize,Texture2D tex)
{
int extend = burshSize;
int stX = Mathf.RoundToInt(Mathf.Clamp(Mathf.Min(from.x, to.x) - extend, 0, tex.width));
int stY = Mathf.RoundToInt(Mathf.Clamp(Mathf.Min(from.y, to.y) - extend, 0, tex.height));
int endX = Mathf.RoundToInt(Mathf.Clamp(Mathf.Max(from.x, to.x) + extend, 0, tex.width));
int endY = Mathf.RoundToInt(Mathf.Clamp(Mathf.Max(from.y, to.y) + extend, 0, tex.height));
int height = endY - stY;
int width = endX - stX;
Color[] pixels = tex.GetPixels(stX, stY, width, height);
for(int y = 0;y<height;y++)
for (int x = 0; x < width; x++)
{
Vector2 p = new Vector2(x, y) + new Vector2(stX, stY);
Vector2 center = new Vector2(0.5f, 0.5f) + p;
Vector2 nearestPos = nearestPoint(from, to, center);
float disk = Vector2.Distance(p, nearestPos);
if (disk > (float)(burshSize + 1))
continue;
pixels[y * width + x] = color;
}
tex.SetPixels(stX, stY, width, height, pixels,0);
tex.Apply();
}
static Vector2 nearestPoint(Vector2 from, Vector2 to, Vector2 center)
{
Vector2 fullDirection = to - from;
Vector2 NorDirection = fullDirection.normalized;
float clostestPoint = Vector2.Dot((center - from), NorDirection);
return from + Mathf.Clamp(clostestPoint, 0, fullDirection.magnitude) * NorDirection;
}
这是画图的核心代码
学习了一个网上的Demo做的,比原来的橡皮擦单纯设置像素点效果要好。。。。。。。