画图程序进化版(橡皮擦同理)

画图改进版,
链接: 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 (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做的,比原来的橡皮擦单纯设置像素点效果要好。。。。。。。

你可能感兴趣的:(画图程序进化版(橡皮擦同理))