ScreenLocate.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using o0;
  2. using o0.Geometry2D.Float;
  3. using o0.Project;
  4. using System;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using ZIM;
  9. using Color = UnityEngine.Color;
  10. [RequireComponent(typeof(Canvas))]
  11. public class ScreenLocate : MonoBehaviour
  12. {
  13. enum Mode
  14. {
  15. InfraredLocate,
  16. ScreenMap,
  17. ScreenLocateManual
  18. }
  19. // 2个灯,顺序根据红外灯的大小 由大到小, 坐标通过 InfraredSpot.ScreenUV 和 InfraredSpot.CameraLocation 获得
  20. public InfraredSpot[] InfraredSpots { get; private set; }
  21. public ZIMWebCamera WebCamera;
  22. public Text Info;
  23. public List<RectTransform> CrosshairInCamera;
  24. public List<RectTransform> CrosshairInScreen;
  25. public RectTransform ScreenQuad;
  26. public Toggle SaveToggle;
  27. public bool ShowScreenQuad = false;
  28. public RawImage rawImage;
  29. public RawImage rawImage1;
  30. public RawImage rawImage2;
  31. public RawImage rawImage3;
  32. public RawImage rawImage4;
  33. public RawImage rawImage5;
  34. public RawImage FullScreenImage;
  35. public InfraredSpotSettings InfraredSpotSettings;
  36. public Texture2D DebugScreenImage;
  37. InfraredLocate infraredLocate;
  38. RectTransform canvas;
  39. Mode mode;
  40. List<Vector2> pointManual = new List<Vector2>();
  41. //o0.Project.WebCam o0WebCam = null;
  42. o0.Project.ScreenIdentification screenIdentification;
  43. static public ScreenLocate Main;
  44. static public List<RawImage> DebugImage = new List<RawImage>();
  45. static public RectTransform BackQuad = null;
  46. static public void DebugTexture(int index, Texture texture)
  47. {
  48. Destroy(DebugImage[index].texture);
  49. DebugImage[index].texture = texture;
  50. }
  51. static public void SetScreen(UnityEngine.Color? color = null)
  52. {
  53. if (BackQuad == null)
  54. {
  55. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  56. var background = canvas.Find("Background");
  57. BackQuad = background.GetChild(0).GetComponent<RectTransform>();
  58. }
  59. BackQuad.parent.gameObject.SetActive(color != null);
  60. BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
  61. }
  62. static public void SetScreen(Rect rect, UnityEngine.Color? color = null)
  63. {
  64. if (BackQuad == null)
  65. {
  66. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  67. var background = canvas.Find("Background");
  68. BackQuad = background.GetChild(0).GetComponent<RectTransform>();
  69. }
  70. BackQuad.parent.gameObject.SetActive(color != null);
  71. BackQuad.anchorMin = rect.min;
  72. BackQuad.anchorMax = rect.max;
  73. BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
  74. }
  75. static void DebugBackQuad(Rect? rect = null)
  76. {
  77. if (BackQuad)
  78. {
  79. BackQuad.parent.GetComponent<RawImage>().enabled = false;
  80. BackQuad.GetComponent<RawImage>().color = Color.white;
  81. BackQuad.parent.gameObject.SetActive(!BackQuad.parent.gameObject.activeSelf);
  82. if (rect.HasValue)
  83. {
  84. BackQuad.anchorMin = rect.Value.min;
  85. BackQuad.anchorMax = rect.Value.max;
  86. }
  87. }
  88. }
  89. void Awake()
  90. {
  91. //var quad = new Quadrilateral(new Vector2(0, 0), new Vector2(89, 0), new Vector2(0, 95), new Vector2(100, 100));
  92. //var x = quad.InterpolationFactors(new Vector2(-1, -1));
  93. //Debug.Log(x);
  94. }
  95. void Start()
  96. {
  97. Main = this;
  98. DebugImage.Add(rawImage);
  99. DebugImage.Add(rawImage1);
  100. DebugImage.Add(rawImage2);
  101. DebugImage.Add(rawImage3);
  102. DebugImage.Add(rawImage4);
  103. DebugImage.Add(rawImage5);
  104. DebugImage.Add(FullScreenImage);
  105. if (WebCamera.webCamTexture)
  106. screenIdentification = new o0.Project.ScreenIdentification(WebCamera.webCamTexture);
  107. //crosshairInCamera = rawImage1.transform.GetChild(0).GetComponent<RectTransform>();
  108. canvas = transform.GetComponent<RectTransform>();
  109. mode = Mode.InfraredLocate;
  110. if (DebugScreenImage)
  111. {
  112. screenIdentification = new o0.Project.ScreenIdentification(new o0.Geometry2D.Vector<int>(DebugScreenImage.width, DebugScreenImage.height));
  113. screenIdentification.LocateScreen();
  114. }
  115. //screenIdentification = new o0.Project.ScreenIdentification(WebCamera.webCamTexture);
  116. //infraredLocate = new InfraredLocate(WebCamera, screenIdentification, InfraredSpotSettings);
  117. }
  118. void Update()
  119. {
  120. WebCamTexture texture = WebCamera?.webCamTexture;
  121. if (texture == null) return;
  122. if (screenIdentification == null)
  123. screenIdentification = new o0.Project.ScreenIdentification(WebCamera.webCamTexture);
  124. if (infraredLocate == null)
  125. infraredLocate = new InfraredLocate(WebCamera, screenIdentification, InfraredSpotSettings);
  126. if (mode == Mode.ScreenLocateManual)
  127. {
  128. //var newTex = WebCamera.webCamTexture.AutoLight(10);
  129. //DebugTexture(6, newTex);
  130. if (Input.GetMouseButtonDown(0))
  131. {
  132. var mouse = Input.mousePosition;
  133. var u = mouse.x / Screen.width;
  134. var v = mouse.y / Screen.height;
  135. //Debug.Log(u + ", " + v);
  136. u = Math.Clamp(u, 0, 1);
  137. v = Math.Clamp(v, 0, 1);
  138. pointManual.Add(new Vector2(u * WebCamera.width, v * WebCamera.height));
  139. var obj = Instantiate(Resources.Load("Point")) as GameObject;
  140. obj.transform.SetParent(FullScreenImage.transform);
  141. obj.transform.localPosition = new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), FullScreenImage.rectTransform.rect);
  142. if (pointManual.Count == 1)
  143. Info.text = "左键单击屏幕 右下角";
  144. else if (pointManual.Count == 2)
  145. Info.text = "左键单击屏幕 右上角";
  146. else if (pointManual.Count == 3)
  147. Info.text = "左键单击屏幕 左上角";
  148. else if (pointManual.Count == 4)
  149. {
  150. screenIdentification.LocateScreenManual(new Quadrilateral(pointManual[0].o0Vector(), pointManual[1].o0Vector(), pointManual[3].o0Vector(), pointManual[2].o0Vector()));
  151. pointManual.Clear();
  152. ShowScreen(screenIdentification.Screen.Quad);
  153. foreach (Transform i in FullScreenImage.transform)
  154. Destroy(i.gameObject);
  155. ToMode(Mode.InfraredLocate);
  156. }
  157. }
  158. return;
  159. }
  160. /* New*/
  161. if (texture.didUpdateThisFrame) // 检查camera是否更新帧
  162. {
  163. if (!screenIdentification.Update(WebCamera.webCamTexture))
  164. {
  165. var pixels = texture.GetPixels(); // 从左往右、从下往上
  166. if (mode == Mode.InfraredLocate)
  167. {
  168. if (!screenIdentification.Screen.Active)
  169. {
  170. var newTex = pixels.zimAutoLightSimple(texture.width, texture.height);
  171. DebugTexture(1, newTex);
  172. }
  173. else
  174. {
  175. //var newTex = pixels.zimAutoLightSimple(texture.width, texture.height);
  176. //DebugTexture(1, newTex);
  177. // 成功定位屏幕后才做红外识别
  178. InfraredSpots = infraredLocate.Update(pixels);
  179. for (int i = 0; i < InfraredSpots.Length; i++)
  180. {
  181. if (InfraredSpots[i].CameraLocation != null)
  182. {
  183. // 检测到光点
  184. var posInCanvas = InfraredSpots[i].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(WebCamera.Size, rawImage.rectTransform.rect);
  185. CrosshairInCamera[i].gameObject.SetActive(true);
  186. CrosshairInCamera[i].anchoredPosition = posInCanvas;
  187. }
  188. else
  189. CrosshairInCamera[i].gameObject.SetActive(false);
  190. }
  191. }
  192. }
  193. else if (mode == Mode.ScreenMap)
  194. {
  195. InfraredSpots = infraredLocate.Update(pixels);
  196. for (int i = 0; i < InfraredSpots.Length; i++)
  197. {
  198. if (InfraredSpots[i].ScreenUV != null)
  199. {
  200. // 检测到光点
  201. var posInCanvas = InfraredSpots[i].ScreenUV.Value.pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), canvas.rect);
  202. CrosshairInScreen[i].gameObject.SetActive(true);
  203. CrosshairInScreen[i].anchoredPosition = posInCanvas;
  204. }
  205. else
  206. CrosshairInScreen[i].gameObject.SetActive(false);
  207. }
  208. }
  209. }
  210. }
  211. /**/
  212. if (Input.GetKeyDown(KeyCode.Alpha1))
  213. BtnScreenLocate();
  214. if (Input.GetKeyDown(KeyCode.Alpha2))
  215. SetScreen(Color.white);
  216. if (Input.GetKeyDown(KeyCode.Alpha3))
  217. SetScreen(Color.black);
  218. if (Input.GetKeyDown(KeyCode.Alpha4))
  219. SetScreen(null);
  220. //if (Input.GetKeyDown(KeyCode.Alpha5))
  221. // DebugScreen(new Quadrilateral(Vector2.zero, new Vector2(600,300), new Vector2(0, 0), new Vector2(1000, 700)));
  222. if (Input.GetKeyDown(KeyCode.Escape))
  223. ToMode(Mode.InfraredLocate);
  224. if (Input.GetKeyDown(KeyCode.Alpha8))
  225. DebugBackQuad();
  226. if (Input.GetKeyDown(KeyCode.Alpha9))
  227. DebugBackQuad(new Rect(0, 0, 1, 1));
  228. if (Input.GetKeyDown(KeyCode.Alpha0))
  229. ShowScreenQuad = true;
  230. /* Old
  231. if (texture.didUpdateThisFrame) // 检查camera是否更新帧
  232. {
  233. if (!o0InfraredIdentification2.Update(WebCamera.webCamTexture))
  234. {
  235. var pixels = texture.GetPixels(); // 从左往右、从下往上
  236. var infraredLocation = infraredLocate.Get(pixels);
  237. if (mode == Mode.InfraredLocate)
  238. {
  239. //if (rawImage1.IsActive())
  240. //{
  241. //var newTex = WebCamera.webCamTexture.zimAutoLight();
  242. //DebugTexture(1, newTex);
  243. //}
  244. int i = 0;
  245. if (infraredLocation != null)
  246. {
  247. for (; i < infraredLocation.Count; i++)
  248. {
  249. // 检测到光点
  250. var posInCanvas = infraredLocation[i].pixelToLocalPosition_AnchorCenter(WebCamera.Size, rawImage.rectTransform.rect);
  251. CrosshairInCamera[i].gameObject.SetActive(true);
  252. CrosshairInCamera[i].anchoredPosition = posInCanvas;
  253. //Debug.Log("location: " + posInCanvas);
  254. }
  255. }
  256. for (; i < CrosshairInCamera.Count; i++)
  257. {
  258. CrosshairInCamera[i].gameObject.SetActive(false);
  259. }
  260. }
  261. else if (mode == Mode.ScreenMap)
  262. {
  263. int i = 0;
  264. if (infraredLocation != null)
  265. {
  266. for (; i < infraredLocation.Count; i++)
  267. {
  268. var uv = o0InfraredIdentification2.Quad.InterpolationFactors(infraredLocation[i].o0Vector());
  269. if (uv.x > 0 && uv.x < 1 && uv.y > 0 && uv.y < 1)
  270. {
  271. var posInScreen = uv.UnityVector().pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), canvas.rect);
  272. CrosshairInScreen[i].anchoredPosition = posInScreen;
  273. }
  274. CrosshairInScreen[i].gameObject.SetActive(true);
  275. }
  276. }
  277. for (; i < CrosshairInScreen.Count; i++)
  278. {
  279. CrosshairInScreen[i].gameObject.SetActive(false);
  280. }
  281. }
  282. }
  283. }
  284. /**/
  285. }
  286. public void BtnScreenLocate()
  287. {
  288. if (WebCamera?.webCamTexture)
  289. screenIdentification.LocateScreen();
  290. }
  291. public void BtnScreenMap()
  292. {
  293. if (WebCamera?.webCamTexture)
  294. ToMode(Mode.ScreenMap);
  295. }
  296. public void BtnScreenLocateManual()
  297. {
  298. if (WebCamera?.webCamTexture)
  299. ToMode(Mode.ScreenLocateManual);
  300. }
  301. // 标记屏幕的四个角
  302. public void ShowScreen(Quadrilateral quad)
  303. {
  304. if (quad == null)
  305. {
  306. Info.text = "识别屏幕失败";
  307. return;
  308. }
  309. Info.text = "已识别到屏幕";
  310. if (ShowScreenQuad)
  311. {
  312. ScreenQuad.gameObject.SetActive(true);
  313. for (int i = 0; i < 4; i++)
  314. {
  315. RectTransform t = ScreenQuad.GetChild(i) as RectTransform;
  316. t.anchoredPosition = quad[i].UnityVector().pixelToLocalPosition_AnchorCenter(WebCamera.Size, ScreenQuad.rect);
  317. }
  318. }
  319. }
  320. void ToMode(Mode mode)
  321. {
  322. if (this.mode == mode)
  323. return;
  324. if (mode == Mode.ScreenMap)
  325. {
  326. if (!screenIdentification.Screen.Active)
  327. {
  328. Info.text = "先定位屏幕";
  329. return;
  330. }
  331. Info.text = "按ESC退出";
  332. SetScreen(Color.black);
  333. Info.transform.SetAsLastSibling();
  334. this.mode = Mode.ScreenMap;
  335. }
  336. else if (mode == Mode.InfraredLocate)
  337. {
  338. Info.text = screenIdentification.Screen.Active ? "已定位屏幕" : "定位屏幕失败";
  339. //Info.text = "已识别到屏幕";
  340. SetScreen(null);
  341. foreach (var i in CrosshairInScreen)
  342. i.gameObject.SetActive(false);
  343. FullScreenImage.gameObject.SetActive(false);
  344. Info.transform.SetSiblingIndex(transform.childCount - 4);
  345. this.mode = Mode.InfraredLocate;
  346. DebugTexture(1, null);
  347. }
  348. else if (mode == Mode.ScreenLocateManual)
  349. {
  350. Info.text = "左键单击屏幕 左下角";
  351. FullScreenImage.gameObject.SetActive(true);
  352. Info.transform.SetSiblingIndex(transform.childCount - 1);
  353. //var newTex = WebCamera.webCamTexture.AutoLight(10);
  354. var newTex = WebCamera.webCamTexture.zimAutoLightSimple();
  355. DebugTexture(6, newTex);
  356. this.mode = Mode.ScreenLocateManual;
  357. }
  358. }
  359. }