Extension.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.Unity
  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 Color RGBMutiply(this UnityEngine.Color c, float multiplier)
  30. {
  31. return new Color(c.r * multiplier, c.g * multiplier, c.b * multiplier, c.a);
  32. }
  33. public static Vector2 Size(this Texture tex) => new Vector2(tex.width, tex.height);
  34. private static Dictionary<Color, string> colorNames = new Dictionary<Color, string>()
  35. {
  36. { Color.red, "Red" },
  37. { Color.green, "Green" },
  38. { Color.blue, "Blue" },
  39. { Color.black, "Black" },
  40. { Color.white, "White" },
  41. { Color.yellow, "Yellow" },
  42. { Color.cyan, "Cyan" },
  43. { Color.magenta, "Magenta" },
  44. // Add more predefined colors here
  45. };
  46. public static string GetColorName(this UnityEngine.Color c)
  47. {
  48. if (colorNames.TryGetValue(c, out var name))
  49. return name;
  50. return c.ToString();
  51. }
  52. public static string GetColorName(this UnityEngine.Color? c)
  53. {
  54. if (c == null)
  55. return "null";
  56. return c.Value.GetColorName();
  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. public static Texture2D Merge(this Texture2D a, Texture2D b)
  71. {
  72. if (a.width != b.width || a.height != b.height)
  73. throw new InvalidOperationException("尺寸不同无法合并texture");
  74. var pixel0 = a.GetPixels();
  75. var pixel1 = b.GetPixels();
  76. for (int i = 0; i < pixel0.Length; i++)
  77. pixel0[i] += pixel1[i];
  78. var texAdd = new Texture2D(a.width, a.height);
  79. texAdd.SetPixels(pixel0);
  80. texAdd.Apply();
  81. return texAdd;
  82. }
  83. // 像素坐标映射到Unity局部坐标
  84. public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector2 pixel, Vector2 size, Rect dstRect)
  85. {
  86. var xp = pixel.x / size.x - 0.5f;
  87. var yp = pixel.y / size.y - 0.5f;
  88. //var origin = rawImage.anchoredPosition;
  89. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  90. }
  91. public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector pixel, Vector size, Rect dstRect)
  92. {
  93. var xp = pixel.x / size.x - 0.5f;
  94. var yp = pixel.y / size.y - 0.5f;
  95. //var origin = rawImage.anchoredPosition;
  96. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  97. }
  98. // Unity局部坐标转回像素坐标
  99. public static Vector2 localPositionToPixel_AnchorCenter(this Vector2 localPosition, Vector2 size, Rect dstRect)
  100. {
  101. var xp = localPosition.x / dstRect.width + 0.5f;
  102. var yp = localPosition.y / dstRect.height + 0.5f;
  103. return new Vector2(xp * size.x, yp * size.y);
  104. }
  105. // 像素坐标映射到Unity局部坐标
  106. public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector2 pixel, o0.Geometry2D.Vector<int> size, Rect dstRect)
  107. {
  108. var xp = pixel.x / size.x - 0.5f;
  109. var yp = pixel.y / size.y - 0.5f;
  110. //var origin = rawImage.anchoredPosition;
  111. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  112. }
  113. public static Vector2 pixelToLocalPosition_AnchorLeftBottom(this Vector2 pixel, o0.Geometry2D.Vector<int> size, Rect dstRect)
  114. {
  115. var xp = pixel.x / size.x;
  116. var yp = pixel.y / size.y;
  117. //var origin = rawImage.anchoredPosition;
  118. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  119. }
  120. public static float DegreeToXAxis(this Vector2 v)
  121. {
  122. var a = v.x > 0 ? Math.Atan(v.y / v.x) * 180 / Math.PI : 180 + Math.Atan(v.y / v.x) * 180 / Math.PI;
  123. a = a < 0 ? 360 + a : a;
  124. return (float)a;
  125. }
  126. public static Texture2D zimAutoLightSimple(this WebCamTexture texture)
  127. {
  128. var pixel = texture.GetPixels();
  129. return zimAutoLightSimple(pixel, texture.width, texture.height);
  130. }
  131. public static Texture2D zimAutoLightSimple(this Texture2D texture)
  132. {
  133. var pixel = texture.GetPixels();
  134. return zimAutoLightSimple(pixel, texture.width, texture.height);
  135. }
  136. public static Vector2 Size(this Texture2D texture)
  137. {
  138. return new Vector2(texture.width, texture.height);
  139. }
  140. // 亮点放大到1
  141. public static Texture2D zimAutoLightSpot(this WebCamTexture texture)
  142. {
  143. var pixel = texture.GetPixels();
  144. float[] brightness = new float[pixel.Length];
  145. Parallel.For(0, pixel.Length, (i) => brightness[i] = pixel[i].Brightness());
  146. var max = Enumerable.Max(brightness);
  147. var scale = max >= 0.5f ? 1 / max : 1.3f; // 设个阈值 0.5
  148. Parallel.For(0, pixel.Length, (i) => pixel[i] = pixel[i].RGBMutiply(scale));
  149. var tex = new Texture2D(texture.width, texture.height);
  150. tex.SetPixels(pixel);
  151. tex.Apply();
  152. return tex;
  153. }
  154. // 效率高,效果一般,benny写的效果更好
  155. public static Texture2D zimAutoLightSimple(this Color[] pixel, int width, int height)
  156. {
  157. Color[] p = new Color[pixel.Length];
  158. var brightness256 = new int[256];
  159. for (int i = 0; i < width / 7; i++) // 间隔采样,得到256色彩下的直方图
  160. {
  161. for (int j = 0; j < height / 7; j++)
  162. {
  163. var index = j * 7 * width + i * 7;
  164. var b = pixel[index].Brightness(256);
  165. brightness256[b]++;
  166. }
  167. }
  168. var limit = (int)(width * height / 49 * 0.001f); // 取亮度大约在前0.1%的值(不取最大的因为可能会被个别亮点影响)
  169. int select = 255;
  170. for (; select >= 0; select--)
  171. {
  172. limit -= brightness256[select];
  173. if (limit <= 0)
  174. break;
  175. }
  176. var scale = 0.85f / (select / 256f); // 找到高亮的像素,计算放大系数
  177. //var avgBrightness = 0f;
  178. //Parallel.For(0, pixel.Length / 256, (i) => avgBrightness += pixel[i * 256].Brightness());
  179. //avgBrightness /= pixel.Length / 256;
  180. //var scale = 1 / avgBrightness / 2f;
  181. if (float.IsInfinity(scale))
  182. return null;
  183. Parallel.For(0, p.Length, (i) => p[i] = pixel[i].RGBMutiply(scale));
  184. var tex = new Texture2D(width, height);
  185. tex.SetPixels(p);
  186. tex.Apply();
  187. return tex;
  188. }
  189. public static Texture2D zimAutoLight(this WebCamTexture texture)
  190. {
  191. var pixel = texture.GetPixels();
  192. return zimAutoLight(pixel, texture.width, texture.height, 0);
  193. }
  194. public static Texture2D zimAutoLight(this Texture2D texture, int brightness)
  195. {
  196. var pixel = texture.GetPixels();
  197. return zimAutoLight(pixel, texture.width, texture.height, brightness);
  198. }
  199. public static Texture2D zimAutoLight(this Color[] pixel, int width, int height, [DefaultValue("0")] int brightness)
  200. {
  201. float[] p = new float[pixel.Length];
  202. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].Brightness());
  203. var scale = 1 / Enumerable.Max(p) * (2 + brightness);
  204. if (float.IsInfinity(scale))
  205. return null;
  206. Parallel.For(0, p.Length, (i) => pixel[i] = pixel[i].RGBMutiply(scale)); // 归一化
  207. var tex = new Texture2D(width, height);
  208. tex.SetPixels(pixel);
  209. tex.Apply();
  210. return tex;
  211. }
  212. public static Texture2D zimAutoLevelEqualization(this WebCamTexture texture, int bit = 256)
  213. {
  214. var pixel = texture.GetPixels();
  215. float[] p = new float[pixel.Length];
  216. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].r); // 红通道
  217. bit -= 1;
  218. var scale = 1 / Enumerable.Max(p);
  219. if (float.IsInfinity(scale))
  220. return null;
  221. Parallel.For(0, p.Length, (i) => p[i] *= scale); // 归一化
  222. int[] pint = new int[p.Length];
  223. Parallel.For(0, p.Length, i => pint[i] = (int)(p[i] * bit)); // 映射到256色彩
  224. //Debug.Log(Enumerable.Max(pint) + ", " + scale);
  225. int[] H = new int[bit + 1];
  226. for (int i = 0; i < p.Length; i++)
  227. H[pint[i]]++;
  228. float[] HF = new float[bit + 1];
  229. for (int i = 0; i <= bit; i++)
  230. HF[i] = H[i] / (float)p.Length; // 概率
  231. float[] D = new float[bit + 1];
  232. float temp = 0;
  233. for (int i = 1; i <= bit; i++) // 不取颜色为0的概率
  234. {
  235. temp += HF[i];
  236. D[i] = temp; // 累计密度
  237. }
  238. Parallel.For(0, p.Length, i => p[i] = D[pint[i]] / D.Last()); // 取密度为颜色,并归一化
  239. UnityEngine.Color[] pixelLighted = new UnityEngine.Color[pixel.Length];
  240. Parallel.For(0, pixel.Length, (i) => pixelLighted[i] = new UnityEngine.Color(p[i], 0, 0));
  241. var tex = new Texture2D(texture.width, texture.height);
  242. tex.SetPixels(pixelLighted);
  243. tex.Apply();
  244. return tex;
  245. }
  246. public static void CopyTo(this RenderTexture src, Texture2D dst)
  247. {
  248. var pre = RenderTexture.active;
  249. RenderTexture.active = src;
  250. dst.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);
  251. dst.Apply();
  252. RenderTexture.active = pre;
  253. }
  254. }
  255. }