ScreenLocate.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. #define ENABLE_LOG
  2. using Serenegiant.UVC;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using ZIM;
  9. using static Serenegiant.UVC.UVCManager;
  10. using Color = UnityEngine.Color;
  11. [RequireComponent(typeof(Canvas))]
  12. public class ScreenLocate : MonoBehaviour
  13. {
  14. private const string TAG = "ScreenLocate#";
  15. enum Mode
  16. {
  17. InfraredLocate,
  18. ScreenMap,
  19. ScreenLocateManual
  20. }
  21. enum InfraredCount
  22. {
  23. Single,
  24. Double
  25. }
  26. enum Platform
  27. {
  28. Window,
  29. Android
  30. }
  31. Platform mPlatform = Platform.Android;
  32. // 2个灯,顺序根据红外灯的大小 由大到小, 坐标通过 InfraredSpot.ScreenUV 和 InfraredSpot.CameraLocation 获得
  33. public InfraredSpot[] InfraredSpots
  34. {
  35. get
  36. {
  37. infraredCount = InfraredCount.Double;
  38. return infraredSpotBuffer;
  39. }
  40. }
  41. // 1个灯, 坐标通过 InfraredSpot.ScreenUV 和 InfraredSpot.CameraLocation 获得
  42. public InfraredSpot InfraredSpotSingle
  43. {
  44. get
  45. {
  46. infraredCount = InfraredCount.Single;
  47. return infraredSpotBuffer[0];
  48. }
  49. }
  50. InfraredSpot[] infraredSpotBuffer;
  51. InfraredCount infraredCount;
  52. #region UVC 处理的对象
  53. public UVCManager mUVCManager;
  54. public UVCDrawer mUVCDrawer;
  55. public CameraInfo mUVCCameraInfo;
  56. private Texture mUVCTexture;
  57. private Texture2D mUVCTexture2D;
  58. // [SerializeField] Texture2DArray mUVCOutArray;
  59. #endregion
  60. public Text Info;
  61. public List<RectTransform> CrosshairInCamera;
  62. public List<RectTransform> CrosshairInScreen;
  63. public RectTransform ScreenQuad;
  64. public Toggle SaveToggle;
  65. public bool ShowScreenQuad = false;
  66. public RawImage rawImage;
  67. public RawImage rawImage1;
  68. public RawImage rawImage2;
  69. public RawImage rawImage3;
  70. public RawImage rawImage4;
  71. public RawImage rawImage5;
  72. public RawImage FullScreenImage;
  73. public InfraredSpotSettings InfraredSpotSettings;
  74. public Texture2D DebugScreenImage;
  75. public Button startUVCBtn;
  76. public Button manualDebugBtn;
  77. public InfraredManager.UIManagerSingle mUIManagerSingle;
  78. // private SynchronizationContext mainContext;
  79. //是否单点显示
  80. bool bSinglePoint = true;//默认单点识别
  81. public Toggle SinglePointToggle;
  82. bool bIdentifyRed = true;//默认设备红色
  83. public Toggle SinglePointToggleColor;
  84. bool bIdentifyGreen = true;
  85. public Toggle SinglePointToggleColorGreen;
  86. #region 性能检测相关
  87. public Text m_UITime;
  88. const float m_UIUpdateInterval = 0.1f;
  89. float m_UIUpdateTimer = 0.0f;
  90. List<float> m_History = new List<float>(100);
  91. int m_ValidHistoryFrames = 0;
  92. float m_AverageTime = float.NaN;
  93. float m_MedianTime = float.NaN;
  94. float m_MinTime = float.NaN;
  95. float m_MaxTime = float.NaN;
  96. public float updateInterval = 0.5F;
  97. private double lastInterval;
  98. private int frames = 0;
  99. private float fps;
  100. public Text m_FPS;
  101. #endregion
  102. InfraredLocate infraredLocate;
  103. RectTransform canvas;
  104. Mode mode;
  105. List<Vector2> pointManual = new List<Vector2>();
  106. //o0.Project.WebCam o0WebCam = null;
  107. o0.Project.ScreenIdentification screenIdentification;
  108. static public ScreenLocate Main;
  109. static public List<RawImage> DebugImage = new List<RawImage>();
  110. static public RectTransform BackQuad = null;
  111. static public void DebugTexture(int index, Texture texture)
  112. {
  113. Destroy(DebugImage[index].texture);
  114. DebugImage[index].texture = texture;
  115. }
  116. static public void SetScreen(UnityEngine.Color? color = null)
  117. {
  118. if (BackQuad == null)
  119. {
  120. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  121. var background = canvas.Find("Background");
  122. BackQuad = background.GetChild(0).GetComponent<RectTransform>();
  123. }
  124. BackQuad.parent.gameObject.SetActive(color != null);
  125. BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
  126. }
  127. static public void SetScreen(Rect rect, UnityEngine.Color? color = null)
  128. {
  129. if (BackQuad == null)
  130. {
  131. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  132. var background = canvas.Find("Background");
  133. BackQuad = background.GetChild(0).GetComponent<RectTransform>();
  134. }
  135. BackQuad.parent.gameObject.SetActive(color != null);
  136. BackQuad.anchorMin = rect.min;
  137. BackQuad.anchorMax = rect.max;
  138. BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
  139. }
  140. static void DebugBackQuad(Rect? rect = null)
  141. {
  142. if (BackQuad)
  143. {
  144. BackQuad.parent.GetComponent<RawImage>().enabled = false;
  145. BackQuad.GetComponent<RawImage>().color = Color.white;
  146. BackQuad.parent.gameObject.SetActive(!BackQuad.parent.gameObject.activeSelf);
  147. if (rect.HasValue)
  148. {
  149. BackQuad.anchorMin = rect.Value.min;
  150. BackQuad.anchorMax = rect.Value.max;
  151. }
  152. }
  153. }
  154. void Awake()
  155. {
  156. Main = this;
  157. if (mUVCDrawer)
  158. mUVCDrawer.StartPreviewAction += UVCIsReady;
  159. }
  160. void OnDestroy()
  161. {
  162. if (mUVCDrawer)
  163. mUVCDrawer.StartPreviewAction -= UVCIsReady;
  164. }
  165. void Start()
  166. {
  167. //mainContext = SynchronizationContext.Current;
  168. DebugImage.Add(rawImage);
  169. DebugImage.Add(rawImage1);
  170. DebugImage.Add(rawImage2);
  171. DebugImage.Add(rawImage3);
  172. DebugImage.Add(rawImage4);
  173. DebugImage.Add(rawImage5);
  174. DebugImage.Add(FullScreenImage);
  175. canvas = transform.GetComponent<RectTransform>();
  176. mode = Mode.InfraredLocate;
  177. //if (DebugScreenImage)
  178. //{
  179. // screenIdentification = new o0.Project.ScreenIdentification(new o0.Geometry2D.Vector<int>(DebugScreenImage.width, DebugScreenImage.height));
  180. // WebCamIsReady(DebugScreenImage);
  181. // screenIdentification.LocateScreen();
  182. //}
  183. infraredCount = InfraredCount.Single;
  184. if (SinglePointToggle)
  185. {
  186. //如果是单点显示
  187. bSinglePoint = PlayerPrefs.GetInt("bSinglePoint", 1) == 1;
  188. SinglePointToggle.isOn = bSinglePoint;
  189. }
  190. if (SinglePointToggleColor)
  191. {
  192. bIdentifyRed = PlayerPrefs.GetInt("bIdentifyRed", 1) == 1;
  193. SinglePointToggleColor.isOn = bIdentifyRed;
  194. }
  195. if (SinglePointToggleColorGreen) {
  196. bIdentifyGreen = PlayerPrefs.GetInt("bIdentifyGreen", 1) == 1;
  197. SinglePointToggleColorGreen.isOn = bIdentifyGreen;
  198. }
  199. #region 性能检测相关
  200. for (var i = 0; i < m_History.Capacity; ++i)
  201. {
  202. m_History.Add(0.0f);
  203. }
  204. lastInterval = Time.realtimeSinceStartup;
  205. frames = 0;
  206. #endregion
  207. }
  208. //ZIMWebCamera场景使用
  209. public void WebCamIsReady(Texture texture)
  210. {
  211. mPlatform = Platform.Window;
  212. mUVCTexture = texture;
  213. mUVCCameraInfo = new CameraInfo(mUVCTexture);
  214. brightness = 0;
  215. }
  216. //手机端UVCCamra使用
  217. public void UVCIsReady(Texture texture)
  218. {
  219. mPlatform = Platform.Android;
  220. //this.startUVCBtn.interactable = true;
  221. mUVCTexture = texture;
  222. //ARGB32
  223. //mUVCTexture2D = Texture2D.CreateExternalTexture(texture.width, texture.height, TextureFormat.ARGB32, false, true, texture.GetNativeTexturePtr()); //TextureToTexture2D(texture);
  224. //Debug.Log("mUVCTexture2D isReable:" + mUVCTexture2D.isReadable);
  225. //Debug.Log("mUVCTexture2D mipmapCount:" + (mUVCTexture2D.mipmapCount > 1));
  226. List<CameraInfo> cameraInfos = mUVCManager.GetAttachedDevices();
  227. mUVCCameraInfo = cameraInfos[cameraInfos.Count - 1];
  228. manualDebugBtn.interactable = true;
  229. }
  230. public void OnChangeSinglePoint()
  231. {
  232. bSinglePoint = SinglePointToggle.isOn;
  233. PlayerPrefs.SetInt("bSinglePoint", bSinglePoint ? 1 : 0);
  234. }
  235. public void OnChangeSinglePointColor()
  236. {
  237. bIdentifyRed = SinglePointToggleColor.isOn;
  238. PlayerPrefs.SetInt("bIdentifyRed", bIdentifyRed ? 1 : 0);
  239. }
  240. public void OnChangeSinglePointColorGreen() {
  241. bIdentifyGreen = SinglePointToggleColorGreen.isOn;
  242. PlayerPrefs.SetInt("bIdentifyGreen", bIdentifyGreen ? 1 : 0);
  243. }
  244. public void startUVC()
  245. {
  246. }
  247. int brightness = 0;
  248. //public Text brightnessText;
  249. //public void SliderBrightness(Slider slider)
  250. //{
  251. // var _value = slider.value;
  252. // brightness = (int)_value;
  253. // brightnessText.text = (2 + brightness) + "";
  254. //}
  255. public void SetInfraredLocateBrightnessThreshold(float value) {
  256. if (infraredLocate != null)
  257. {
  258. if(value>=0 && value <=1)
  259. infraredLocate.SetBrightnessThreshold(value); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
  260. }
  261. }
  262. void Update()
  263. {
  264. ++frames;
  265. float timeNow = Time.realtimeSinceStartup;
  266. if (timeNow > lastInterval + updateInterval)
  267. {
  268. fps = (float)(frames / (timeNow - lastInterval));
  269. frames = 0;
  270. lastInterval = timeNow;
  271. }
  272. if (m_FPS != null)
  273. m_FPS.text = "FPS:" + fps.ToString("f2");
  274. if (mUVCCameraInfo == null) return;
  275. if (screenIdentification == null)
  276. {
  277. screenIdentification = new o0.Project.ScreenIdentification(mUVCTexture);
  278. }
  279. if (infraredLocate == null)
  280. {
  281. infraredLocate = new InfraredLocate(mUVCCameraInfo, screenIdentification, InfraredSpotSettings);
  282. float redfilterValue = PlayerPrefs.GetFloat("Init redFilterSliderValue", 0.8f);
  283. Debug.Log("Init Red filterValue:" + redfilterValue);
  284. infraredLocate.SetBrightnessThreshold(redfilterValue); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
  285. }
  286. //screenIdentification.Update(DebugScreenImage);
  287. if (mode == Mode.ScreenLocateManual)
  288. {
  289. if (Input.GetMouseButtonDown(0))
  290. {
  291. var mouse = Input.mousePosition;
  292. var u = mouse.x / Screen.width;
  293. var v = mouse.y / Screen.height;
  294. u = Math.Clamp(u, 0, 1);
  295. v = Math.Clamp(v, 0, 1);
  296. pointManual.Add(new Vector2(u * mUVCTexture.width, v * mUVCTexture.height));
  297. var obj = Instantiate(Resources.Load("Point")) as GameObject;
  298. obj.transform.SetParent(FullScreenImage.transform);
  299. obj.transform.localPosition = new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), FullScreenImage.rectTransform.rect);
  300. if (pointManual.Count == 1)
  301. Info.text = "左键单击屏幕 右下角";
  302. else if (pointManual.Count == 2)
  303. Info.text = "左键单击屏幕 右上角";
  304. else if (pointManual.Count == 3)
  305. Info.text = "左键单击屏幕 左上角";
  306. else if (pointManual.Count == 4)
  307. {
  308. screenIdentification.LocateScreenManual(new OrdinalQuadrilateral(pointManual[0].o0Vector(), pointManual[1].o0Vector(), pointManual[3].o0Vector(), pointManual[2].o0Vector()));
  309. pointManual.Clear();
  310. ShowScreen(screenIdentification.Screen.Quad);
  311. foreach (Transform i in FullScreenImage.transform)
  312. Destroy(i.gameObject);
  313. ToMode(Mode.InfraredLocate);
  314. }
  315. }
  316. return;
  317. }
  318. var t0 = Time.realtimeSinceStartup;
  319. /* New*/
  320. //Debug.Log((mUVCCameraInfo != null) +" = "+ mUVCCameraInfo.IsPreviewing + " = "+ screenIdentification.Screen.Active);
  321. if (mUVCCameraInfo != null && mUVCCameraInfo.IsPreviewing) // 成功定位屏幕后才做红外识别
  322. {
  323. CreateUVCTexture2DIfNeeded();
  324. if (!screenIdentification.Update(mUVCTexture2D) )
  325. {
  326. if (!screenIdentification.Screen.Active) {
  327. DebugTexture(1, mUVCTexture2D.zimAutoLightSimple());
  328. return;
  329. }
  330. if (mode == Mode.InfraredLocate)
  331. {
  332. //0,0, cameraTexture2D.width, cameraTexture2D.height,0
  333. var pixels = mUVCTexture2D.GetPixels(); // 从左往右、从下往上
  334. //InfraredSpots = infraredLocate.Update(pixels);
  335. if (bSinglePoint)
  336. infraredSpotBuffer = infraredLocate.UpdateSingle(pixels);
  337. else
  338. infraredSpotBuffer = infraredLocate.Update(pixels);
  339. if (mPlatform == Platform.Window || mUIManagerSingle.bGetPanelActive) //渲染ui上面的点。进入游戏可以隐藏
  340. {
  341. for (int i = 0; i < infraredSpotBuffer.Length; i++)
  342. {
  343. if (infraredSpotBuffer[i].CameraLocation != null)
  344. {
  345. // 检测到光点
  346. var posInCanvas = infraredSpotBuffer[i].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(mUVCCameraInfo.Size, rawImage.rectTransform.rect);
  347. CrosshairInCamera[i].gameObject.SetActive(true);
  348. CrosshairInCamera[i].anchoredPosition = posInCanvas;
  349. }
  350. else
  351. CrosshairInCamera[i].gameObject.SetActive(false);
  352. }
  353. }
  354. //手机端使用
  355. if (mPlatform == Platform.Android && infraredSpotBuffer.Length > 0)
  356. {
  357. int redIndex = 0;
  358. int greenIndex = 1;
  359. //仅仅第一个点显示(如果最大点出界了会闪烁)
  360. if (bSinglePoint)
  361. {
  362. redIndex = 0; //单点识别是,可以选择切换颜色
  363. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  364. {
  365. string str = "Single:";
  366. Info.text = str + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  367. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  368. onFilterPos(infraredSpotBuffer[redIndex].ScreenUV.Value);
  369. }
  370. }
  371. else
  372. {
  373. //雙點模式下選擇第一個點
  374. if (bIdentifyRed && !bIdentifyGreen)
  375. {
  376. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  377. {
  378. Info.text = "Red" + redIndex + ":" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  379. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  380. onFilterPos(infraredSpotBuffer[redIndex].ScreenUV.Value);
  381. }
  382. else
  383. {
  384. Info.text = "未检测到红色最大点!";
  385. }
  386. }
  387. else if (!bIdentifyRed && bIdentifyGreen) {
  388. if (infraredSpotBuffer[greenIndex].ScreenUV != null)
  389. {
  390. Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
  391. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
  392. onFilterPos(infraredSpotBuffer[greenIndex].ScreenUV.Value);
  393. }
  394. else
  395. {
  396. Info.text = "未检测到绿色点!";
  397. }
  398. }
  399. else
  400. {
  401. //两个不选择和两个全选都跑识别两个点
  402. //自動切換 检测到光点
  403. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  404. {
  405. Info.text = "Red:" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  406. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  407. onFilterPos(infraredSpotBuffer[redIndex].ScreenUV.Value);
  408. }
  409. else if (infraredSpotBuffer[greenIndex].ScreenUV != null)
  410. {
  411. Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
  412. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
  413. onFilterPos(infraredSpotBuffer[greenIndex].ScreenUV.Value);
  414. }
  415. else
  416. {
  417. Info.text = "未检测到点!";
  418. }
  419. }
  420. }
  421. }
  422. }
  423. }
  424. }
  425. var t1 = Time.realtimeSinceStartup;
  426. var dt = t1 - t0;
  427. m_History[m_ValidHistoryFrames % m_History.Count] = dt;
  428. ++m_ValidHistoryFrames;
  429. m_UIUpdateTimer += Time.deltaTime;
  430. if (m_UIUpdateTimer >= m_UIUpdateInterval)
  431. {
  432. m_UIUpdateTimer = 0.0f;
  433. if (m_ValidHistoryFrames >= m_History.Count)
  434. {
  435. m_ValidHistoryFrames = 0;
  436. m_AverageTime = 0.0f;
  437. m_MinTime = float.PositiveInfinity;
  438. m_MaxTime = float.NegativeInfinity;
  439. {
  440. for (var i = 0; i < m_History.Count; i++)
  441. {
  442. var time = m_History[i];
  443. m_AverageTime += time;
  444. m_MinTime = Mathf.Min(m_MinTime, time);
  445. m_MaxTime = Mathf.Max(m_MaxTime, time);
  446. }
  447. m_AverageTime /= m_History.Count;
  448. }
  449. {
  450. m_History.Sort();
  451. // Odd-length history?
  452. if ((m_History.Count & 1) != 0)
  453. {
  454. m_MedianTime = m_History[m_History.Count / 2];
  455. }
  456. else
  457. {
  458. m_MedianTime = (m_History[m_History.Count / 2] + m_History[m_History.Count / 2 - 1]) / 2.0f;
  459. }
  460. }
  461. }
  462. var statistics = $"{m_History.Count} 帧样本:\naverage: {m_AverageTime * 1000.0f:F2}ms\nmedian: {m_MedianTime * 1000.0f:F2}ms\nmin: {m_MinTime * 1000.0f:F2}ms\nmax: {m_MaxTime * 1000.0f:F2}ms\n";
  463. //Method: {m_Method} {UnityEngine.SceneManagement.SceneManager.GetActiveScene().name} |
  464. if (m_UITime != null)
  465. m_UITime.text = $"Cam: {mUVCCameraInfo.CurrentWidth}x{mUVCCameraInfo.CurrentHeight}{(mUVCTexture2D? ",T2D:" : "")}{(mUVCTexture2D? mUVCTexture2D.width+ "x" : "")}{(mUVCTexture2D ? mUVCTexture2D.height:"")} \nLast Frame: {dt * 1000.0f:F2}ms \n{statistics}";
  466. }
  467. UpdateInputs();
  468. }
  469. Vector2 targetPos = Vector2.zero;
  470. Vector2 movePos = Vector2.zero;
  471. int moveSpeed = 20;
  472. public float filterDis = 3.0f;
  473. void onFilterPos(Vector2 _vector2Pos) {
  474. Vector2 np = new Vector2(_vector2Pos.x * Screen.width, _vector2Pos.y * Screen.height); //_vector2Pos.pixelToLocalPosition_AnchorCenter(Vector2.one, (transform as RectTransform).rect);
  475. if (Vector2.Distance(np, targetPos) >= filterDis)
  476. {
  477. targetPos = np;
  478. InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(targetPos.x, targetPos.y, 0));
  479. }
  480. //movePos = Vector3.Lerp(movePos, targetPos, Time.deltaTime * moveSpeed);
  481. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(movePos.x, movePos.y, 0));
  482. }
  483. public void BtnScreenLocate()
  484. {
  485. if (DebugScreenImage)
  486. {
  487. screenIdentification = new o0.Project.ScreenIdentification(new o0.Geometry2D.Vector<int>(DebugScreenImage.width, DebugScreenImage.height));
  488. WebCamIsReady(DebugScreenImage);
  489. screenIdentification.LocateScreen();
  490. }
  491. screenIdentification.LocateScreen();
  492. }
  493. public void BtnScreenMap()
  494. {
  495. ToMode(Mode.ScreenMap);
  496. }
  497. //进入手动定位屏幕
  498. public void BtnScreenLocateManual()
  499. {
  500. ToMode(Mode.ScreenLocateManual);
  501. }
  502. // 标记屏幕的四个角
  503. public void ShowScreen(OrdinalQuadrilateral quad)
  504. {
  505. if (quad == null)
  506. {
  507. Info.text = "识别屏幕失败";
  508. return;
  509. }
  510. Info.text = "已识别到屏幕";
  511. if (ShowScreenQuad)
  512. {
  513. ScreenQuad.gameObject.SetActive(true);
  514. for (int i = 0; i < 4; i++)
  515. {
  516. RectTransform t = ScreenQuad.GetChild(i) as RectTransform;
  517. //mUVCCameraInfo.Size
  518. t.anchoredPosition = quad[i].UnityVector().pixelToLocalPosition_AnchorCenter(mUVCCameraInfo.Size, ScreenQuad.rect);
  519. }
  520. }
  521. }
  522. void ToMode(Mode mode)
  523. {
  524. if (this.mode == mode)
  525. return;
  526. if (mode == Mode.ScreenMap)
  527. {
  528. if (!screenIdentification.Screen.Active)
  529. {
  530. Info.text = "先定位屏幕";
  531. return;
  532. }
  533. Info.text = "按ESC退出";
  534. SetScreen(Color.black);
  535. Info.transform.SetAsLastSibling();
  536. this.mode = Mode.ScreenMap;
  537. }
  538. else if (mode == Mode.InfraredLocate)
  539. {
  540. Info.text = screenIdentification.Screen.Active ? "已定位屏幕" : "定位屏幕失败";
  541. //Info.text = "已识别到屏幕";
  542. SetScreen(null);
  543. foreach (var i in CrosshairInScreen)
  544. i.gameObject.SetActive(false);
  545. FullScreenImage.gameObject.SetActive(false);
  546. Info.transform.SetSiblingIndex(transform.childCount - 4);
  547. this.mode = Mode.InfraredLocate;
  548. DebugTexture(6, null);
  549. //DebugTexture(1, null); //null
  550. // rawImage1.texture = null;
  551. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  552. Console.WriteLine($"{TAG} Mode.InfraredLocate:已识别到屏幕:{ screenIdentification.Screen.Active}");
  553. #endif
  554. }
  555. else if (mode == Mode.ScreenLocateManual)
  556. {
  557. Info.text = "左键单击屏幕 左下角";
  558. FullScreenImage.gameObject.SetActive(true);
  559. Info.transform.SetSiblingIndex(transform.childCount - 1);
  560. // var newTex = WebCamera.webCamTexture.AutoLight(10);
  561. //DebugTexture(1, TextureToTexture2D(rawImage.texture));
  562. CreateUVCTexture2DIfNeeded();
  563. DebugTexture(6, mUVCTexture2D.zimAutoLight(brightness));
  564. //mUVCTexture2DTemp = TextureToTexture2D(mUVCCameraInfo.previewTexture);
  565. //DebugTexture(6, mUVCTexture2DTemp.zimAutoLight(brightness));
  566. this.mode = Mode.ScreenLocateManual;
  567. }
  568. }
  569. private Texture2D TextureToTexture2D(Texture texture)
  570. {
  571. Texture2D _texture2D = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false, true);
  572. RenderTexture currentRT = RenderTexture.active;
  573. RenderTexture renderTexture = RenderTexture.GetTemporary(
  574. texture.width,
  575. texture.height,
  576. 0,
  577. RenderTextureFormat.ARGB32,
  578. RenderTextureReadWrite.Linear);
  579. Graphics.Blit(texture, renderTexture);
  580. RenderTexture.active = renderTexture;
  581. _texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  582. _texture2D.Apply();
  583. RenderTexture.active = currentRT;
  584. RenderTexture.ReleaseTemporary(renderTexture);
  585. return _texture2D;
  586. }
  587. private void CreateUVCTexture2DIfNeeded()
  588. {
  589. if (mUVCTexture2D != null)
  590. Destroy(mUVCTexture2D);
  591. mUVCTexture2D = TextureToTexture2D(mUVCTexture);
  592. }
  593. #region DoubleButton
  594. private DateTime m_firstTime;
  595. private DateTime m_secondTime;
  596. private void Press()
  597. {
  598. Debug.Log("进入手动定位");
  599. BtnScreenLocateManual();
  600. resetTime();
  601. }
  602. public void OnDoubleClick()
  603. {
  604. //超时重置
  605. if (!m_firstTime.Equals(default(DateTime)))
  606. {
  607. var intervalTime = DateTime.Now - m_firstTime;
  608. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  609. if (milliSeconds >= 400)
  610. resetTime();
  611. }
  612. // 按下按钮时对两次的时间进行记录
  613. if (m_firstTime.Equals(default(DateTime)))
  614. m_firstTime = DateTime.Now;
  615. else
  616. m_secondTime = DateTime.Now;
  617. // 在第二次点击触发,时差小于400ms触发
  618. if (!m_firstTime.Equals(default(DateTime)) && !m_secondTime.Equals(default(DateTime)))
  619. {
  620. var intervalTime = m_secondTime - m_firstTime;
  621. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  622. if (milliSeconds < 400)
  623. Press();
  624. else
  625. resetTime();
  626. }
  627. }
  628. private void resetTime()
  629. {
  630. m_firstTime = default(DateTime);
  631. m_secondTime = default(DateTime);
  632. }
  633. #endregion
  634. #region 性能检测相关
  635. void InvalidateTimings()
  636. {
  637. m_ValidHistoryFrames = 0;
  638. m_AverageTime = float.NaN;
  639. m_MedianTime = float.NaN;
  640. m_MinTime = float.NaN;
  641. m_MaxTime = float.NaN;
  642. }
  643. void UpdateInputs()
  644. {
  645. //重置
  646. if (Input.GetKeyDown(KeyCode.UpArrow))
  647. {
  648. InvalidateTimings();
  649. }
  650. }
  651. #endregion
  652. }