Extension.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.g + 0.11f * c.b; // 红色为主
  61. }
  62. public static float Lightness(this Color c)
  63. {
  64. return Mathf.Max(c.r, c.g, c.b);
  65. }
  66. // 转换为整数再计算
  67. public static int Brightness(this Color c, int colorBits)
  68. {
  69. colorBits -= 1;
  70. int r = (int)(c.r * colorBits);
  71. int g = (int)(c.g * colorBits);
  72. int b = (int)(c.b * colorBits);
  73. return (int)(0.59f * r + 0.3f * g + 0.11f * b); // 红色为主
  74. }
  75. // a 融合b,颜色直接相加
  76. public static Texture2D Merge(this Texture2D a, Texture2D b)
  77. {
  78. if (a.width != b.width || a.height != b.height)
  79. throw new InvalidOperationException("尺寸不同无法合并texture");
  80. var pixel0 = a.GetPixels();
  81. var pixel1 = b.GetPixels();
  82. for (int i = 0; i < pixel0.Length; i++)
  83. pixel0[i] += pixel1[i];
  84. var texAdd = new Texture2D(a.width, a.height);
  85. texAdd.SetPixels(pixel0);
  86. texAdd.Apply();
  87. return texAdd;
  88. }
  89. // a 覆盖 b,会剔除a的黑色背景
  90. public static Texture2D Overlay(this Texture2D a, Texture2D b)
  91. {
  92. if (a.width != b.width || a.height != b.height)
  93. throw new InvalidOperationException("尺寸不同无法叠加texture");
  94. var pixel0 = a.GetPixels();
  95. var pixel1 = b.GetPixels();
  96. for (int i = 0; i < pixel0.Length; i++)
  97. {
  98. float newA = (float)(Math.Clamp((pixel0[i].Lightness() - 0.2) / 0.6, 0, 1) * pixel0[i].a);
  99. pixel0[i] = pixel0[i].RGBMutiply(newA) + pixel1[i] * (1 - newA);
  100. }
  101. var texAdd = new Texture2D(a.width, a.height);
  102. texAdd.SetPixels(pixel0);
  103. texAdd.Apply();
  104. return texAdd;
  105. }
  106. // 长度平方,0-1
  107. public static float LengthSquare(this Color c) => (c.r * c.r + c.g * c.g + c.b * c.b) / 3;
  108. // 像素坐标映射到Unity局部坐标
  109. public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector2 pixel, Vector2 size, Rect dstRect)
  110. {
  111. var xp = pixel.x / size.x - 0.5f;
  112. var yp = pixel.y / size.y - 0.5f;
  113. //var origin = rawImage.anchoredPosition;
  114. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  115. }
  116. public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector pixel, Vector size, Rect dstRect)
  117. {
  118. var xp = pixel.x / size.x - 0.5f;
  119. var yp = pixel.y / size.y - 0.5f;
  120. //var origin = rawImage.anchoredPosition;
  121. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  122. }
  123. // Unity局部坐标转回像素坐标
  124. public static Vector2 localPositionToPixel_AnchorCenter(this Vector2 localPosition, Vector2 size, Rect dstRect)
  125. {
  126. var xp = localPosition.x / dstRect.width + 0.5f;
  127. var yp = localPosition.y / dstRect.height + 0.5f;
  128. return new Vector2(xp * size.x, yp * size.y);
  129. }
  130. // 像素坐标映射到Unity局部坐标
  131. public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector2 pixel, o0.Geometry2D.Vector<int> size, Rect dstRect)
  132. {
  133. var xp = pixel.x / size.x - 0.5f;
  134. var yp = pixel.y / size.y - 0.5f;
  135. //var origin = rawImage.anchoredPosition;
  136. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  137. }
  138. public static Vector2 pixelToLocalPosition_AnchorLeftBottom(this Vector2 pixel, o0.Geometry2D.Vector<int> size, Rect dstRect)
  139. {
  140. var xp = pixel.x / size.x;
  141. var yp = pixel.y / size.y;
  142. //var origin = rawImage.anchoredPosition;
  143. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  144. }
  145. public static float DegreeToXAxis(this Vector2 v)
  146. {
  147. var a = v.x > 0 ? Math.Atan(v.y / v.x) * 180 / Math.PI : 180 + Math.Atan(v.y / v.x) * 180 / Math.PI;
  148. a = a < 0 ? 360 + a : a;
  149. return (float)a;
  150. }
  151. public static Texture2D zimAutoLightSimple(this WebCamTexture texture)
  152. {
  153. var pixel = texture.GetPixels();
  154. return zimAutoLightSimple(pixel, texture.width, texture.height);
  155. }
  156. public static Texture2D zimAutoLightSimple(this Texture2D texture)
  157. {
  158. var pixel = texture.GetPixels();
  159. return zimAutoLightSimple(pixel, texture.width, texture.height);
  160. }
  161. public static Vector2 Size(this Texture2D texture)
  162. {
  163. return new Vector2(texture.width, texture.height);
  164. }
  165. // 亮点放大到1
  166. public static Texture2D zimAutoLightSpot(this WebCamTexture texture)
  167. {
  168. var pixel = texture.GetPixels();
  169. float[] brightness = new float[pixel.Length];
  170. Parallel.For(0, pixel.Length, (i) => brightness[i] = pixel[i].Brightness());
  171. var max = Enumerable.Max(brightness);
  172. var scale = max >= 0.5f ? 1 / max : 1.3f; // 设个阈值 0.5
  173. Parallel.For(0, pixel.Length, (i) => pixel[i] = pixel[i].RGBMutiply(scale));
  174. var tex = new Texture2D(texture.width, texture.height);
  175. tex.SetPixels(pixel);
  176. tex.Apply();
  177. return tex;
  178. }
  179. // 效率高,效果一般,benny写的效果更好
  180. public static Texture2D zimAutoLightSimple(this Color[] pixel, int width, int height)
  181. {
  182. Color[] p = new Color[pixel.Length];
  183. var brightness256 = new int[256];
  184. for (int i = 0; i < width / 7; i++) // 间隔采样,得到256色彩下的直方图
  185. {
  186. for (int j = 0; j < height / 7; j++)
  187. {
  188. var index = j * 7 * width + i * 7;
  189. var b = pixel[index].Brightness(256);
  190. brightness256[b]++;
  191. }
  192. }
  193. var limit = (int)(width * height / 49 * 0.001f); // 取亮度大约在前0.1%的值(不取最大的因为可能会被个别亮点影响)
  194. int select = 255;
  195. for (; select >= 0; select--)
  196. {
  197. limit -= brightness256[select];
  198. if (limit <= 0)
  199. break;
  200. }
  201. var scale = 0.85f / (select / 256f); // 找到高亮的像素,计算放大系数
  202. //var avgBrightness = 0f;
  203. //Parallel.For(0, pixel.Length / 256, (i) => avgBrightness += pixel[i * 256].Brightness());
  204. //avgBrightness /= pixel.Length / 256;
  205. //var scale = 1 / avgBrightness / 2f;
  206. if (float.IsInfinity(scale))
  207. return null;
  208. Parallel.For(0, p.Length, (i) => p[i] = pixel[i].RGBMutiply(scale));
  209. var tex = new Texture2D(width, height);
  210. tex.SetPixels(p);
  211. tex.Apply();
  212. return tex;
  213. }
  214. public static Texture2D zimAutoLight(this WebCamTexture texture)
  215. {
  216. var pixel = texture.GetPixels();
  217. return zimAutoLight(pixel, texture.width, texture.height, 0);
  218. }
  219. public static Texture2D zimAutoLight(this Texture2D texture, int brightness)
  220. {
  221. var pixel = texture.GetPixels();
  222. return zimAutoLight(pixel, texture.width, texture.height, brightness);
  223. }
  224. public static Texture2D zimAutoLight(this Color[] pixel, int width, int height, [DefaultValue("0")] int brightness)
  225. {
  226. float[] p = new float[pixel.Length];
  227. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].Brightness());
  228. var scale = 1 / Enumerable.Max(p) * (2 + brightness);
  229. if (float.IsInfinity(scale))
  230. return null;
  231. Parallel.For(0, p.Length, (i) => pixel[i] = pixel[i].RGBMutiply(scale)); // 归一化
  232. var tex = new Texture2D(width, height);
  233. tex.SetPixels(pixel);
  234. tex.Apply();
  235. return tex;
  236. }
  237. public static Texture2D zimAutoLevelEqualization(this WebCamTexture texture, int bit = 256)
  238. {
  239. var pixel = texture.GetPixels();
  240. float[] p = new float[pixel.Length];
  241. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].r); // 红通道
  242. bit -= 1;
  243. var scale = 1 / Enumerable.Max(p);
  244. if (float.IsInfinity(scale))
  245. return null;
  246. Parallel.For(0, p.Length, (i) => p[i] *= scale); // 归一化
  247. int[] pint = new int[p.Length];
  248. Parallel.For(0, p.Length, i => pint[i] = (int)(p[i] * bit)); // 映射到256色彩
  249. //Debug.Log(Enumerable.Max(pint) + ", " + scale);
  250. int[] H = new int[bit + 1];
  251. for (int i = 0; i < p.Length; i++)
  252. H[pint[i]]++;
  253. float[] HF = new float[bit + 1];
  254. for (int i = 0; i <= bit; i++)
  255. HF[i] = H[i] / (float)p.Length; // 概率
  256. float[] D = new float[bit + 1];
  257. float temp = 0;
  258. for (int i = 1; i <= bit; i++) // 不取颜色为0的概率
  259. {
  260. temp += HF[i];
  261. D[i] = temp; // 累计密度
  262. }
  263. Parallel.For(0, p.Length, i => p[i] = D[pint[i]] / D.Last()); // 取密度为颜色,并归一化
  264. UnityEngine.Color[] pixelLighted = new UnityEngine.Color[pixel.Length];
  265. Parallel.For(0, pixel.Length, (i) => pixelLighted[i] = new UnityEngine.Color(p[i], 0, 0));
  266. var tex = new Texture2D(texture.width, texture.height);
  267. tex.SetPixels(pixelLighted);
  268. tex.Apply();
  269. return tex;
  270. }
  271. public static void CopyTo(this RenderTexture src, Texture2D dst)
  272. {
  273. var pre = RenderTexture.active;
  274. RenderTexture.active = src;
  275. dst.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);
  276. dst.Apply();
  277. RenderTexture.active = pre;
  278. }
  279. }
  280. }