Extension.cs 11 KB

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