Advertisement
Bunny83

DrawLines.cs

Aug 27th, 2018
2,726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class DrawLines : MonoBehaviour
  4. {
  5.     private Texture2D m_Texture;
  6.     private Color[] m_Colors;
  7.     RaycastHit2D hit;
  8.     SpriteRenderer spriteRend;
  9.     Color zeroAlpha = Color.clear;
  10.     public int erSize;
  11.     public Vector2Int lastPos;
  12.     public bool Drawing = false;
  13.     void Start ()
  14.     {
  15.         spriteRend = gameObject.GetComponent<SpriteRenderer>();
  16.         var tex = spriteRend.sprite.texture;
  17.         m_Texture = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false);
  18.         m_Texture.filterMode = FilterMode.Bilinear;
  19.         m_Texture.wrapMode = TextureWrapMode.Clamp;
  20.         m_Colors = tex.GetPixels();
  21.         m_Texture.SetPixels(m_Colors);
  22.         m_Texture.Apply();
  23.         spriteRend.sprite = Sprite.Create(m_Texture, spriteRend.sprite.rect, new Vector2(0.5f, 0.5f));
  24.     }
  25.  
  26.     void Update()
  27.     {
  28.         if (Input.GetMouseButton(0))
  29.         {
  30.             hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
  31.             if (hit.collider != null)
  32.             {
  33.                 UpdateTexture();
  34.                 Drawing = true;
  35.             }
  36.         }
  37.         else
  38.             Drawing = false;
  39.     }
  40.  
  41.     public void UpdateTexture()
  42.     {
  43.         int w = m_Texture.width;
  44.         int h = m_Texture.height;
  45.         var mousePos = hit.point - (Vector2)hit.collider.bounds.min;
  46.         mousePos.x *= w / hit.collider.bounds.size.x;
  47.         mousePos.y *= h / hit.collider.bounds.size.y;
  48.         Vector2Int p = new Vector2Int((int)mousePos.x, (int)mousePos.y);
  49.         Vector2Int start = new Vector2Int();
  50.         Vector2Int end = new Vector2Int();
  51.         if(!Drawing)
  52.             lastPos = p;
  53.         start.x = Mathf.Clamp(Mathf.Min(p.x, lastPos.x) - erSize, 0, w);
  54.         start.y = Mathf.Clamp(Mathf.Min(p.y, lastPos.y) - erSize, 0, h);
  55.         end.x = Mathf.Clamp(Mathf.Max(p.x, lastPos.x) + erSize, 0, w);
  56.         end.y = Mathf.Clamp(Mathf.Max(p.y, lastPos.y) + erSize, 0, h);
  57.         Vector2 dir = p - lastPos;
  58.         for (int x = start.x; x < end.x; x++)
  59.         {
  60.             for (int y = start.y; y < end.y; y++)
  61.             {
  62.                 Vector2 pixel = new Vector2(x, y);
  63.                 Vector2 linePos = p;
  64.                 if (Drawing)
  65.                 {
  66.                     float d = Vector2.Dot(pixel - lastPos, dir) / dir.sqrMagnitude;
  67.                     d = Mathf.Clamp01(d);
  68.                     linePos = Vector2.Lerp(lastPos, p, d);
  69.                 }
  70.                 if ((pixel - linePos).sqrMagnitude <= erSize * erSize)
  71.                 {
  72.                     m_Colors[x + y * w] = zeroAlpha;
  73.                 }
  74.             }
  75.         }
  76.         lastPos = p;
  77.         m_Texture.SetPixels(m_Colors);
  78.         m_Texture.Apply();
  79.         spriteRend.sprite = Sprite.Create(m_Texture, spriteRend.sprite.rect, new Vector2(0.5f, 0.5f));
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement