Extension.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 localPositionToPixel_AnchorCenter(this Vector2 localPosition, Vector2 size, Rect dstRect)
  54. {
  55. var xp = localPosition.x / dstRect.width + 0.5f;
  56. var yp = localPosition.y / dstRect.height + 0.5f;
  57. return new Vector2(xp * size.x, yp * size.y);
  58. }
  59. // 像素坐标映射到Unity局部坐标
  60. public static Vector2 pixelToLocalPosition_AnchorCenter(this Vector2 pixel, o0.Geometry2D.Vector<int> size, Rect dstRect)
  61. {
  62. var xp = pixel.x / size.x - 0.5f;
  63. var yp = pixel.y / size.y - 0.5f;
  64. //var origin = rawImage.anchoredPosition;
  65. return new Vector2(dstRect.width * xp, dstRect.height * yp);
  66. }
  67. public static bool IsInScreen(this OrdinalQuadrilateral Quad, Vector2 size)
  68. {
  69. 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 &&
  70. 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)
  71. return true;
  72. return false;
  73. }
  74. public static float DegreeToXAxis(this Vector2 v)
  75. {
  76. var a = v.x > 0 ? Math.Atan(v.y / v.x) * 180 / Math.PI : 180 + Math.Atan(v.y / v.x) * 180 / Math.PI;
  77. a = a < 0 ? 360 + a : a;
  78. return (float)a;
  79. }
  80. public static Texture2D zimAutoLightSimple(this WebCamTexture texture)
  81. {
  82. var pixel = texture.GetPixels();
  83. return zimAutoLightSimple(pixel,texture.width,texture.height);
  84. }
  85. public static Texture2D zimAutoLightSimple(this Texture2D texture)
  86. {
  87. var pixel = texture.GetPixels();
  88. return zimAutoLightSimple(pixel, texture.width, texture.height);
  89. }
  90. public static Vector2 Size(this Texture2D texture)
  91. {
  92. return new Vector2(texture.width, texture.height);
  93. }
  94. // 效果不太好
  95. public static Texture2D zimAutoLightSimple(this Color[] pixel, int width, int height)
  96. {
  97. Color[] p = new Color[pixel.Length];
  98. var brightness16 = new int[256];
  99. for (int i = 0; i < width / 7; i++)
  100. {
  101. for (int j = 0; j < height / 7; j++)
  102. {
  103. var index = j * 7 * width + i * 7;
  104. var b = pixel[index].Brightness(256);
  105. brightness16[b]++;
  106. }
  107. }
  108. var limit = (int)(width * height / 49 * 0.001f); // 取亮度大约在前0.1%的值(不取最大的因为可能会被个别亮点影响)
  109. int select = 255;
  110. for (; select >= 0; select--)
  111. {
  112. limit -= brightness16[select];
  113. if (limit <= 0)
  114. break;
  115. }
  116. var scale = 0.85f / (select / 256f);
  117. //var avgBrightness = 0f;
  118. //Parallel.For(0, pixel.Length / 256, (i) => avgBrightness += pixel[i * 256].Brightness());
  119. //avgBrightness /= pixel.Length / 256;
  120. //var scale = 1 / avgBrightness / 2f;
  121. if (float.IsInfinity(scale))
  122. return null;
  123. Parallel.For(0, p.Length, (i) => p[i] = pixel[i] * scale);
  124. var tex = new Texture2D(width, height);
  125. tex.SetPixels(p);
  126. tex.Apply();
  127. return tex;
  128. }
  129. public static Texture2D zimAutoLight(this WebCamTexture texture)
  130. {
  131. var pixel = texture.GetPixels();
  132. return zimAutoLight(pixel, texture.width, texture.height,0);
  133. }
  134. public static Texture2D zimAutoLight(this Texture2D texture,int brightness)
  135. {
  136. var pixel = texture.GetPixels();
  137. return zimAutoLight(pixel, texture.width, texture.height, brightness);
  138. }
  139. public static Texture2D zimAutoLight(this Color[] pixel, int width, int height, [DefaultValue("0")] int brightness)
  140. {
  141. float[] p = new float[pixel.Length];
  142. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].Brightness());
  143. var scale = 1 / Enumerable.Max(p) * (2 + brightness);
  144. if (float.IsInfinity(scale))
  145. return null;
  146. Parallel.For(0, p.Length, (i) => pixel[i] *= scale); // 归一化
  147. var tex = new Texture2D(width, height);
  148. tex.SetPixels(pixel);
  149. tex.Apply();
  150. return tex;
  151. }
  152. public static Texture2D zimAutoLevelEqualization(this WebCamTexture texture, int bit = 256)
  153. {
  154. var pixel = texture.GetPixels();
  155. float[] p = new float[pixel.Length];
  156. Parallel.For(0, pixel.Length, (i) => p[i] = pixel[i].r); // 红通道
  157. bit -= 1;
  158. var scale = 1 / Enumerable.Max(p);
  159. if (float.IsInfinity(scale))
  160. return null;
  161. Parallel.For(0, p.Length, (i) => p[i] *= scale); // 归一化
  162. int[] pint = new int[p.Length];
  163. Parallel.For(0, p.Length, i => pint[i] = (int)(p[i] * bit)); // 映射到256色彩
  164. //Debug.Log(Enumerable.Max(pint) + ", " + scale);
  165. int[] H = new int[bit + 1];
  166. for (int i = 0; i < p.Length; i++)
  167. H[pint[i]]++;
  168. float[] HF = new float[bit + 1];
  169. for (int i = 0; i <= bit; i++)
  170. HF[i] = H[i] / (float)p.Length; // 概率
  171. float[] D = new float[bit + 1];
  172. float temp = 0;
  173. for (int i = 1; i <= bit; i++) // 不取颜色为0的概率
  174. {
  175. temp += HF[i];
  176. D[i] = temp; // 累计密度
  177. }
  178. Parallel.For(0, p.Length, i => p[i] = D[pint[i]] / D.Last()); // 取密度为颜色,并归一化
  179. UnityEngine.Color[] pixelLighted = new UnityEngine.Color[pixel.Length];
  180. Parallel.For(0, pixel.Length, (i) => pixelLighted[i] = new UnityEngine.Color(p[i], 0, 0));
  181. var tex = new Texture2D(texture.width, texture.height);
  182. tex.SetPixels(pixelLighted);
  183. tex.Apply();
  184. return tex;
  185. }
  186. public static void CopyTo(this RenderTexture src, Texture2D dst)
  187. {
  188. var pre = RenderTexture.active;
  189. RenderTexture.active = src;
  190. dst.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);
  191. dst.Apply();
  192. RenderTexture.active = pre;
  193. }
  194. }
  195. }