PaintBoard.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System.Linq;
  2. using UnityEngine;
  3. /// <summary>
  4. /// 画板
  5. /// </summary>
  6. public class PaintBoard : MonoBehaviour
  7. {
  8. //当画笔移动速度很快时,为了不出现断断续续的点,所以需要对两个点之间进行插值,lerp就是插值系数
  9. [Range(0, 1)]
  10. public float lerp = 0.05f;
  11. //初始化背景的图片
  12. public Texture2D initailizeTexture;
  13. //当前背景的图片
  14. private Texture2D currentTexture;
  15. //画笔所在位置映射到画板图片的UV坐标
  16. private Vector2 paintPos;
  17. private bool isDrawing = false;//当前画笔是不是正在画板上
  18. //离开时画笔所在的位置
  19. private int lastPaintX;
  20. private int lastPaintY;
  21. //画笔所代表的色块的大小
  22. private int painterTipsWidth = 30;
  23. private int painterTipsHeight = 15;
  24. //当前画板的背景图片的尺寸
  25. private int textureWidth;
  26. private int textureHeight;
  27. //画笔的颜色
  28. private Color32[] painterColor;
  29. private Color32[] currentColor;
  30. private Color32[] originColor;
  31. private void Start()
  32. {
  33. //获取原始图片的大小
  34. Texture2D originTexture = GetComponent<MeshRenderer>().material.mainTexture as Texture2D;
  35. textureWidth = originTexture.width;//1920
  36. textureHeight = originTexture.height;//1080
  37. //设置当前图片
  38. currentTexture = new Texture2D(textureWidth, textureHeight, TextureFormat.RGBA32, false, true);
  39. currentTexture.SetPixels32(originTexture.GetPixels32());
  40. currentTexture.Apply();
  41. //赋值给黑板
  42. GetComponent<MeshRenderer>().material.mainTexture = currentTexture;
  43. //初始化画笔的颜色
  44. painterColor = Enumerable.Repeat<Color32>(new Color32(255, 0, 0, 255), painterTipsWidth * painterTipsHeight).ToArray<Color32>();
  45. }
  46. private void LateUpdate()
  47. {
  48. if (Input.GetKeyUp(KeyCode.Space))
  49. {
  50. currentTexture.ClearRequestedMipmapLevel();
  51. currentTexture.Apply();
  52. }
  53. //计算当前画笔,所代表的色块的一个起始点
  54. int texPosX = (int)(paintPos.x * (float)textureWidth - (float)(painterTipsWidth / 2));
  55. int texPosY = (int)(paintPos.y * (float)textureHeight - (float)(painterTipsHeight / 2));
  56. if (isDrawing)
  57. {
  58. //改变画笔所在的块的像素值
  59. currentTexture.SetPixels32(texPosX, texPosY, painterTipsWidth, painterTipsHeight, painterColor);
  60. //如果快速移动画笔的话,会出现断续的现象,所以要插值
  61. if (lastPaintX != 0 && lastPaintY != 0)
  62. {
  63. int lerpCount = (int)(1 / lerp);
  64. for (int i = 0; i <= lerpCount; i++)
  65. {
  66. int x = (int)Mathf.Lerp((float)lastPaintX, (float)texPosX, lerp);
  67. int y = (int)Mathf.Lerp((float)lastPaintY, (float)texPosY, lerp);
  68. currentTexture.SetPixels32(x, y, painterTipsWidth, painterTipsHeight, painterColor);
  69. }
  70. }
  71. currentTexture.Apply();
  72. lastPaintX = texPosX;
  73. lastPaintY = texPosY;
  74. }
  75. else
  76. {
  77. lastPaintX = lastPaintY = 0;
  78. }
  79. }
  80. /// <summary>
  81. /// 设置当前画笔所在的UV位置
  82. /// </summary>
  83. /// <param name="x"></param>
  84. /// <param name="y"></param>
  85. public void SetPainterPositon(float x, float y)
  86. {
  87. paintPos.Set(x, y);
  88. }
  89. /// <summary>
  90. /// 画笔当前是不是在画画
  91. /// </summary>
  92. public bool IsDrawing
  93. {
  94. get
  95. {
  96. return isDrawing;
  97. }
  98. set
  99. {
  100. isDrawing = value;
  101. }
  102. }
  103. /// <summary>
  104. /// 使用当前正在画板上的画笔的颜色
  105. /// </summary>
  106. /// <param name="color"></param>
  107. public void SetPainterColor(Color32 color)
  108. {
  109. if (!painterColor[0].IsEqual(color))
  110. {
  111. for (int i = 0; i < painterColor.Length; i++)
  112. {
  113. painterColor[i] = color;
  114. }
  115. }
  116. }
  117. }
  118. public static class MethodExtention
  119. {
  120. /// <summary>
  121. /// 用于比较两个Color32类型是不是同种颜色
  122. /// </summary>
  123. /// <param name="origin"></param>
  124. /// <param name="compare"></param>
  125. /// <returns></returns>
  126. public static bool IsEqual(this Color32 origin, Color32 compare)
  127. {
  128. if (origin.g == compare.g && origin.r == compare.r)
  129. {
  130. if (origin.a == compare.a && origin.b == compare.b)
  131. {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. }