Extension.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using o0.Geometry2D.Float;
  2. using System;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Unity.VisualScripting;
  6. using UnityEngine;
  7. using UnityEngine.Internal;
  8. namespace ZIM
  9. {
  10. public static partial class Extension
  11. {
  12. public static Vector2 UnityVector(this Vector v)
  13. {
  14. return new Vector2(v.x, v.y);
  15. }
  16. public static Vector o0Vector(this Vector2 v)
  17. {
  18. return new Vector(v.x, v.y);
  19. }
  20. public static UnityEngine.Color UnityColor(this o0.Color c)
  21. {
  22. return new UnityEngine.Color(c.r, c.g, c.b, c.a);
  23. }
  24. public static o0.Color o0Color(this UnityEngine.Color c)
  25. {
  26. return new o0.Color(c.r, c.g, c.b, c.a);
  27. }
  28. public static float LengthManhattan(this Vector2 v)
  29. {
  30. return Math.Abs(v.x) + Math.Abs(v.y);
  31. }
  32. public static float Brightness(this Color c)
  33. {
  34. return 0.59f * c.r + 0.3f * c.r + 0.11f * c.r; // 红色为主
  35. }
  36. // 转换为整数再计算
  37. public static int Brightness(this Color c, int colorBits)
  38. {
  39. int r = (int)(c.r * colorBits);
  40. int g = (int)(c.g * colorBits);
  41. int b = (int)(c.b * colorBits);
  42. return (int)(0.59f * r + 0.3f * g + 0.11f * b); // 红色为主
  43. }
  44. // 像素坐标映射到Unity局部坐标
  45. public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector2 pixel, Vector2 size, Rect dstRect)
  46. {
  47. var xp = pixel.x / size.x - 0.5f;
  48. var yp = pixel.y / size.y - 0.5f;
  49. //var origin = rawImage.anchoredPosition;
  50. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  51. }
  52. // 像素坐标映射到Unity局部坐标
  53. public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector2 pixel, o0.Geometry2D.Vector<int> size, Rect dstRect)
  54. {
  55. var xp = pixel.x / size.x - 0.5f;
  56. var yp = pixel.y / size.y - 0.5f;
  57. //var origin = rawImage.anchoredPosition;
  58. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  59. }
  60. public static bool IsInScreen(this OrdinalQuadrilateral Quad, Vector2 size)
  61. {
  62. if (Quad.A.x < Quad.B.x && Quad.A.y < Quad.C.y && Quad.B.y < Quad.D.y && Quad.C.x < Quad.D.x && Quad.A.x > 0 && Quad.A.y > 0 &&
  63. Quad.B.x < size.x && Quad.B.y > 0 && Quad.C.x > 0 && Quad.C.y < size.y && Quad.D.x < size.x && Quad.D.y < size.y)
  64. return true;
  65. return false;
  66. }
  67. public static float DegreeToXAxis(this Vector2 v)
  68. {
  69. var a = v.x > 0 ? Math.Atan(v.y / v.x) * 180 / Math.PI : 180 + Math.Atan(v.y / v.x) * 180 / Math.PI;
  70. a = a < 0 ? 360 + a : a;
  71. return (float)a;
  72. }
  73. public static Texture2D zimAutoLightSimple(this WebCamTexture texture)
  74. {
  75. var pixel = texture.GetPixels();
  76. return zimAutoLightSimple(pixel,texture.width,texture.height);
  77. }
  78. public static Texture2D zimAutoLightSimple(this Texture2D texture)
  79. {
  80. var pixel = texture.GetPixels();
  81. return zimAutoLightSimple(pixel, texture.width, texture.height);
  82. }
  83. public static Vector2 Size(this Texture2D texture)
  84. {
  85. return new Vector2(texture.width, texture.height);
  86. }
  87. // 效果不太好
  88. public static Texture2D zimAutoLightSimple(this Color[] pixel, int width, int height)
  89. {
  90. Color[] p = new Color[pixel.Length];
  91. var brightness16 = new int[256];
  92. for (int i = 0; i < width / 7; i++)
  93. {
  94. for (int j = 0; j < height / 7; j++)
  95. {
  96. var index = j * 7 * width + i * 7;
  97. var b = pixel[index].Brightness(256);
  98. brightness16[b]++;
  99. }
  100. }
  101. var limit = (int)(width * height / 49 * 0.001f); // 取亮度大约在前0.1%的值(不取最大的因为可能会被个别亮点影响)
  102. int select = 255;
  103. for (; select >= 0; select--)
  104. {
  105. limit -= brightness16[select];
  106. if (limit <= 0)
  107. break;
  108. }
  109. var scale = 0.85f / (select / 256f);
  110. //var avgBrightness = 0f;
  111. //Parallel.For(0, pixel.Length / 256, (i) => avgBrightness += pixel[i * 256].Brightness());
  112. //avgBrightness /= pixel.Length / 256;
  113. //var scale = 1 / avgBrightness / 2f;
  114. if (float.IsInfinity(scale))
  115. return null;
  116. Parallel.For(0, p.Length, (i) => p[i] = pixel[i] * scale);
  117. var tex = new Texture2D(width, height);
  118. tex.SetPixels(p);
  119. tex.Apply();
  120. return tex;
  121. }
  122. public static Texture2D zimAutoLight(this WebCamTexture texture)
  123. {
  124. var pixel = texture.GetPixels();
  125. return zimAutoLight(pixel, texture.width, texture.height,0);
  126. }
  127. public static Texture2D zimAutoLight(this Texture2D texture,int brightness)
  128. {
  129. var pixel = texture.GetPixels();
  130. return zimAutoLight(pixel, texture.width, texture.height, brightness);
  131. }
  132. public static Texture2D zimAutoLight(this Color[] pixel, int width, int height, [DefaultValue("0")] int brightness)
  133. {
  134. float[] p = new float[pixel.Length];
  135. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].Brightness());
  136. var scale = 1 / Enumerable.Max(p) * (2 + brightness);
  137. if (float.IsInfinity(scale))
  138. return null;
  139. Parallel.For(0, p.Length, (i) => pixel[i] *= scale); // 归一化
  140. var tex = new Texture2D(width, height);
  141. tex.SetPixels(pixel);
  142. tex.Apply();
  143. return tex;
  144. }
  145. public static Texture2D zimAutoLevelEqualization(this WebCamTexture texture, int bit = 256)
  146. {
  147. var pixel = texture.GetPixels();
  148. float[] p = new float[pixel.Length];
  149. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].r); // 红通道
  150. bit -= 1;
  151. var scale = 1 / Enumerable.Max(p);
  152. if (float.IsInfinity(scale))
  153. return null;
  154. Parallel.For(0, p.Length, (i) => p[i] *= scale); // 归一化
  155. int[] pint = new int[p.Length];
  156. Parallel.For(0, p.Length, i => pint[i] = (int)(p[i] * bit)); // 映射到256色彩
  157. //Debug.Log(Enumerable.Max(pint) + ", " + scale);
  158. int[] H = new int[bit + 1];
  159. for (int i = 0; i < p.Length; i++)
  160. H[pint[i]]++;
  161. float[] HF = new float[bit + 1];
  162. for (int i = 0; i <= bit; i++)
  163. HF[i] = H[i] / (float)p.Length; // 概率
  164. float[] D = new float[bit + 1];
  165. float temp = 0;
  166. for (int i = 1; i <= bit; i++) // 不取颜色为0的概率
  167. {
  168. temp += HF[i];
  169. D[i] = temp; // 累计密度
  170. }
  171. Parallel.For(0, p.Length, i => p[i] = D[pint[i]] / D.Last()); // 取密度为颜色,并归一化
  172. UnityEngine.Color[] pixelLighted = new UnityEngine.Color[pixel.Length];
  173. Parallel.For(0, pixel.Length, (i) => pixelLighted[i] = new UnityEngine.Color(p[i], 0, 0));
  174. var tex = new Texture2D(texture.width, texture.height);
  175. tex.SetPixels(pixelLighted);
  176. tex.Apply();
  177. return tex;
  178. }
  179. public static void CopyTo(this RenderTexture src, Texture2D dst)
  180. {
  181. var pre = RenderTexture.active;
  182. RenderTexture.active = src;
  183. dst.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);
  184. dst.Apply();
  185. RenderTexture.active = pre;
  186. }
  187. }
  188. }