Extension.cs 9.5 KB

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