码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3D 画笔实现系列04-Texture2D

时间:2017-12-03 18:58:10      阅读:2017      评论:0      收藏:0      [点我收藏+]

标签:art   render   public   unity3d   手册   颜色   poi   ==   click   

被厕所熏醒,继续画线。在文档中找到Texture2D类中的SetPixel方法。

技术分享图片
 1 public class example : MonoBehaviour {
 2     void Start() {
 3         Texture2D texture = new Texture2D(128, 128);
 4         renderer.material.mainTexture = texture;
 5         int y = 0;
 6         while (y < texture.height) {
 7             int x = 0;
 8             while (x < texture.width) {
 9                 Color color = ((x & y) ? Color.white : Color.gray);
10                 texture.SetPixel(x, y, color);
11                 ++x;
12             }
13             ++y;
14         }
15         texture.Apply();
16     }
17 }
手册实例

瞬间看到光明大道。(呜呜,最后发现.....设置像素谁用谁知道)

第一步,创建一个空白图片,

 Texture2D t2d = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false); 

 

第二步,取得鼠标(手指)在屏幕上的坐标。

var pos =Input.mousePosition;

 

第三步,将屏幕坐标转化到图片坐标。

当图片背景和屏幕一样大时鼠标在的屏幕坐标和图片上的uv坐标一样(偷懒)

当不一样时

if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rt, pos, camera, out pixelUV))
            {
                pixelUV.x += drawBackgroudWidth / 2;
                pixelUV.y += drawBackgroudHeight / 2;
            }

 

 

第四步,设置相应位置的颜色值。

我用了两种方法

技术分享图片
    public  void DrawRect(int x, int y, Color color, Vector2 drawRect)
    {
        int startx = x - (int)(drawRect.x) / 2;
        int starty = y - (int)(drawRect.y) / 2;
        for (int i = startx; i < startx + (int)(drawRect.x); i++)
        {
            for (int j = starty; j < starty + (int)(drawRect.y); j++)
            {
                t2d.SetPixel(i, j, color);
            }
        }
        t2d.Apply();
    }
1,画矩形
技术分享图片
 public  void DrawCircle(int x, int y,Color color, int radius = 1)
    {
       
        int r2 = radius * radius;
        int area = r2 << 2;
        int rr = radius << 1;
        for (int i = 0; i < area; i++)
        {

            int tx = (i % rr) - radius;
            int ty = (i / rr) - radius;
            if (tx * tx + ty * ty < r2)
            {
                t2d.SetPixel(x+tx, y+ty, color);
            }
        }
        t2d.Apply();

    }
2,画圆

第五步,插值画线

技术分享图片
 1  public   void DrawLine(Vector2 start, Vector2 end, int offset,Color color)
 2     {
 3         int x0 = (int)start.x;
 4         int y0 = (int)start.y;
 5         int x1 = (int)end.x;
 6         int y1 = (int)end.y;
 7         int dx = Mathf.Abs(x1 - x0);
 8         int dy = Mathf.Abs(y1 - y0);
 9         int sx, sy;
10         if (x0 < x1) { sx = 1; } else { sx = -1; }
11         if (y0 < y1) { sy = 1; } else { sy = -1; }
12         int err = dx - dy;
13         bool loop = true;
14         int minDistance = (int)(offset >> 1);
15         int pixelCount = 0;
16         int e2;
17         while (loop)
18         {
19             pixelCount++;
20             if (pixelCount > minDistance)
21             {
22                 pixelCount = 0;
23                 t2d.SetPixel(x0, y0, color);
24 
25             }
26             if ((x0 == x1) && (y0 == y1)) loop = false;
27             e2 = 2 * err;
28             if (e2 > -dy)
29             {
30                 err = err - dy;
31                 x0 = x0 + sx;
32             }
33             if (e2 < dx)
34             {
35                 err = err + dx;
36                 y0 = y0 + sy;
37             }
38         }
39         t2d.Apply();
40     }
Bresenham快速画直线算法

第六步,记录所有的点保存,

第七步,恢复。

好了。高高兴兴的上手机测试。。。。。。又得换手机

 

Unity3D 画笔实现系列04-Texture2D

标签:art   render   public   unity3d   手册   颜色   poi   ==   click   

原文地址:http://www.cnblogs.com/PandaHome/p/7966648.html

(1)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!