ScreenLocate.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. infraredCount = InfraredCount.Single;
  178. if (SinglePointToggle)
  179. {
  180. //如果是单点显示
  181. bSinglePoint = PlayerPrefs.GetInt("bSinglePoint", 1) == 1;
  182. SinglePointToggle.isOn = bSinglePoint;
  183. }
  184. if (SinglePointToggleColor)
  185. {
  186. bIdentifyRed = PlayerPrefs.GetInt("bIdentifyRed", 1) == 1;
  187. SinglePointToggleColor.isOn = bIdentifyRed;
  188. }
  189. if (SinglePointToggleColorGreen) {
  190. bIdentifyGreen = PlayerPrefs.GetInt("bIdentifyGreen", 1) == 1;
  191. SinglePointToggleColorGreen.isOn = bIdentifyGreen;
  192. }
  193. #region 性能检测相关
  194. for (var i = 0; i < m_History.Capacity; ++i)
  195. {
  196. m_History.Add(0.0f);
  197. }
  198. lastInterval = Time.realtimeSinceStartup;
  199. frames = 0;
  200. #endregion
  201. }
  202. //ZIMWebCamera场景使用
  203. public void WebCamIsReady(Texture texture)
  204. {
  205. mPlatform = Platform.Window;
  206. mUVCTexture = texture;
  207. mUVCCameraInfo = new CameraInfo(mUVCTexture);
  208. brightness = 0;
  209. }
  210. //手机端UVCCamra使用
  211. public void UVCIsReady(Texture texture)
  212. {
  213. mPlatform = Platform.Android;
  214. //this.startUVCBtn.interactable = true;
  215. mUVCTexture = texture;
  216. //ARGB32
  217. //mUVCTexture2D = Texture2D.CreateExternalTexture(texture.width, texture.height, TextureFormat.ARGB32, false, true, texture.GetNativeTexturePtr()); //TextureToTexture2D(texture);
  218. //Debug.Log("mUVCTexture2D isReable:" + mUVCTexture2D.isReadable);
  219. //Debug.Log("mUVCTexture2D mipmapCount:" + (mUVCTexture2D.mipmapCount > 1));
  220. List<CameraInfo> cameraInfos = mUVCManager.GetAttachedDevices();
  221. mUVCCameraInfo = cameraInfos[cameraInfos.Count - 1];
  222. manualDebugBtn.interactable = true;
  223. }
  224. public void OnChangeSinglePoint()
  225. {
  226. bSinglePoint = SinglePointToggle.isOn;
  227. PlayerPrefs.SetInt("bSinglePoint", bSinglePoint ? 1 : 0);
  228. }
  229. public void OnChangeSinglePointColor()
  230. {
  231. bIdentifyRed = SinglePointToggleColor.isOn;
  232. PlayerPrefs.SetInt("bIdentifyRed", bIdentifyRed ? 1 : 0);
  233. }
  234. public void OnChangeSinglePointColorGreen() {
  235. bIdentifyGreen = SinglePointToggleColorGreen.isOn;
  236. PlayerPrefs.SetInt("bIdentifyGreen", bIdentifyGreen ? 1 : 0);
  237. }
  238. public void startUVC()
  239. {
  240. }
  241. int brightness = 0;
  242. //public Text brightnessText;
  243. //public void SliderBrightness(Slider slider)
  244. //{
  245. // var _value = slider.value;
  246. // brightness = (int)_value;
  247. // brightnessText.text = (2 + brightness) + "";
  248. //}
  249. void Update()
  250. {
  251. ++frames;
  252. float timeNow = Time.realtimeSinceStartup;
  253. if (timeNow > lastInterval + updateInterval)
  254. {
  255. fps = (float)(frames / (timeNow - lastInterval));
  256. frames = 0;
  257. lastInterval = timeNow;
  258. }
  259. if (m_FPS != null)
  260. m_FPS.text = "FPS:" + fps.ToString("f2");
  261. if (mUVCCameraInfo == null) return;
  262. if (screenIdentification == null)
  263. {
  264. screenIdentification = new o0.Project.ScreenIdentification(mUVCTexture);
  265. }
  266. if (infraredLocate == null)
  267. {
  268. infraredLocate = new InfraredLocate(mUVCCameraInfo, screenIdentification, InfraredSpotSettings);
  269. infraredLocate.SetBrightnessThreshold(0.8f); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
  270. }
  271. if (mode == Mode.ScreenLocateManual)
  272. {
  273. if (Input.GetMouseButtonDown(0))
  274. {
  275. var mouse = Input.mousePosition;
  276. var u = mouse.x / Screen.width;
  277. var v = mouse.y / Screen.height;
  278. u = Math.Clamp(u, 0, 1);
  279. v = Math.Clamp(v, 0, 1);
  280. pointManual.Add(new Vector2(u * mUVCTexture.width, v * mUVCTexture.height));
  281. var obj = Instantiate(Resources.Load("Point")) as GameObject;
  282. obj.transform.SetParent(FullScreenImage.transform);
  283. obj.transform.localPosition = new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), FullScreenImage.rectTransform.rect);
  284. if (pointManual.Count == 1)
  285. Info.text = "左键单击屏幕 右下角";
  286. else if (pointManual.Count == 2)
  287. Info.text = "左键单击屏幕 右上角";
  288. else if (pointManual.Count == 3)
  289. Info.text = "左键单击屏幕 左上角";
  290. else if (pointManual.Count == 4)
  291. {
  292. screenIdentification.LocateScreenManual(new OrdinalQuadrilateral(pointManual[0].o0Vector(), pointManual[1].o0Vector(), pointManual[3].o0Vector(), pointManual[2].o0Vector()));
  293. pointManual.Clear();
  294. ShowScreen(screenIdentification.Screen.Quad);
  295. foreach (Transform i in FullScreenImage.transform)
  296. Destroy(i.gameObject);
  297. ToMode(Mode.InfraredLocate);
  298. }
  299. }
  300. return;
  301. }
  302. var t0 = Time.realtimeSinceStartup;
  303. /* New*/
  304. if (mUVCCameraInfo != null && mUVCCameraInfo.IsPreviewing && screenIdentification.Screen.Active) // 成功定位屏幕后才做红外识别
  305. {
  306. CreateUVCTexture2DIfNeeded();
  307. if (!screenIdentification.Update(mUVCTexture2D))
  308. {
  309. if (mode == Mode.InfraredLocate)
  310. {
  311. //0,0, cameraTexture2D.width, cameraTexture2D.height,0
  312. var pixels = mUVCTexture2D.GetPixels(); // 从左往右、从下往上
  313. //InfraredSpots = infraredLocate.Update(pixels);
  314. if (bSinglePoint)
  315. infraredSpotBuffer = infraredLocate.UpdateSingle(pixels);
  316. else
  317. infraredSpotBuffer = infraredLocate.Update(pixels);
  318. if (mPlatform == Platform.Window || mUIManagerSingle.bGetPanelActive) //渲染ui上面的点。进入游戏可以隐藏
  319. {
  320. for (int i = 0; i < infraredSpotBuffer.Length; i++)
  321. {
  322. if (infraredSpotBuffer[i].CameraLocation != null)
  323. {
  324. // 检测到光点
  325. var posInCanvas = infraredSpotBuffer[i].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(mUVCCameraInfo.Size, rawImage.rectTransform.rect);
  326. CrosshairInCamera[i].gameObject.SetActive(true);
  327. CrosshairInCamera[i].anchoredPosition = posInCanvas;
  328. }
  329. else
  330. CrosshairInCamera[i].gameObject.SetActive(false);
  331. }
  332. }
  333. //手机端使用
  334. if (mPlatform == Platform.Android && infraredSpotBuffer.Length > 0)
  335. {
  336. int redIndex = 0;
  337. int greenIndex = 1;
  338. //仅仅第一个点显示(如果最大点出界了会闪烁)
  339. if (bSinglePoint)
  340. {
  341. redIndex = 0; //单点识别是,可以选择切换颜色
  342. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  343. {
  344. string str = "Single:";
  345. Info.text = str + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  346. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  347. onFilterPos(infraredSpotBuffer[redIndex].ScreenUV.Value);
  348. }
  349. }
  350. else
  351. {
  352. //雙點模式下選擇第一個點
  353. if (bIdentifyRed && !bIdentifyGreen)
  354. {
  355. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  356. {
  357. Info.text = "Red" + redIndex + ":" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  358. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  359. onFilterPos(infraredSpotBuffer[redIndex].ScreenUV.Value);
  360. }
  361. else
  362. {
  363. Info.text = "未检测到红色最大点!";
  364. }
  365. }
  366. else if (!bIdentifyRed && bIdentifyGreen) {
  367. if (infraredSpotBuffer[greenIndex].ScreenUV != null)
  368. {
  369. Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
  370. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
  371. onFilterPos(infraredSpotBuffer[greenIndex].ScreenUV.Value);
  372. }
  373. else
  374. {
  375. Info.text = "未检测到绿色点!";
  376. }
  377. }
  378. else
  379. {
  380. //两个不选择和两个全选都跑识别两个点
  381. //自動切換 检测到光点
  382. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  383. {
  384. Info.text = "Red:" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  385. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  386. onFilterPos(infraredSpotBuffer[redIndex].ScreenUV.Value);
  387. }
  388. else 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. }
  400. }
  401. }
  402. }
  403. }
  404. var t1 = Time.realtimeSinceStartup;
  405. var dt = t1 - t0;
  406. m_History[m_ValidHistoryFrames % m_History.Count] = dt;
  407. ++m_ValidHistoryFrames;
  408. m_UIUpdateTimer += Time.deltaTime;
  409. if (m_UIUpdateTimer >= m_UIUpdateInterval)
  410. {
  411. m_UIUpdateTimer = 0.0f;
  412. if (m_ValidHistoryFrames >= m_History.Count)
  413. {
  414. m_ValidHistoryFrames = 0;
  415. m_AverageTime = 0.0f;
  416. m_MinTime = float.PositiveInfinity;
  417. m_MaxTime = float.NegativeInfinity;
  418. {
  419. for (var i = 0; i < m_History.Count; i++)
  420. {
  421. var time = m_History[i];
  422. m_AverageTime += time;
  423. m_MinTime = Mathf.Min(m_MinTime, time);
  424. m_MaxTime = Mathf.Max(m_MaxTime, time);
  425. }
  426. m_AverageTime /= m_History.Count;
  427. }
  428. {
  429. m_History.Sort();
  430. // Odd-length history?
  431. if ((m_History.Count & 1) != 0)
  432. {
  433. m_MedianTime = m_History[m_History.Count / 2];
  434. }
  435. else
  436. {
  437. m_MedianTime = (m_History[m_History.Count / 2] + m_History[m_History.Count / 2 - 1]) / 2.0f;
  438. }
  439. }
  440. }
  441. 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";
  442. //Method: {m_Method} {UnityEngine.SceneManagement.SceneManager.GetActiveScene().name} |
  443. if (m_UITime != null)
  444. 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}";
  445. }
  446. UpdateInputs();
  447. }
  448. Vector2 targetPos = Vector2.zero;
  449. Vector2 movePos = Vector2.zero;
  450. int moveSpeed = 20;
  451. public float filterDis = 3.0f;
  452. void onFilterPos(Vector2 _vector2Pos) {
  453. Vector2 np = new Vector2(_vector2Pos.x * Screen.width, _vector2Pos.y * Screen.height); //_vector2Pos.pixelToLocalPosition_AnchorCenter(Vector2.one, (transform as RectTransform).rect);
  454. if (Vector2.Distance(np, targetPos) >= filterDis)
  455. {
  456. targetPos = np;
  457. InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(targetPos.x, targetPos.y, 0));
  458. }
  459. //movePos = Vector3.Lerp(movePos, targetPos, Time.deltaTime * moveSpeed);
  460. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(movePos.x, movePos.y, 0));
  461. }
  462. public void BtnScreenLocate()
  463. {
  464. screenIdentification.LocateScreen();
  465. }
  466. public void BtnScreenMap()
  467. {
  468. ToMode(Mode.ScreenMap);
  469. }
  470. //进入手动定位屏幕
  471. public void BtnScreenLocateManual()
  472. {
  473. ToMode(Mode.ScreenLocateManual);
  474. }
  475. // 标记屏幕的四个角
  476. public void ShowScreen(OrdinalQuadrilateral quad)
  477. {
  478. if (quad == null)
  479. {
  480. Info.text = "识别屏幕失败";
  481. return;
  482. }
  483. Info.text = "已识别到屏幕";
  484. if (ShowScreenQuad)
  485. {
  486. ScreenQuad.gameObject.SetActive(true);
  487. for (int i = 0; i < 4; i++)
  488. {
  489. RectTransform t = ScreenQuad.GetChild(i) as RectTransform;
  490. //mUVCCameraInfo.Size
  491. t.anchoredPosition = quad[i].UnityVector().pixelToLocalPosition_AnchorCenter(mUVCCameraInfo.Size, ScreenQuad.rect);
  492. }
  493. }
  494. }
  495. void ToMode(Mode mode)
  496. {
  497. if (this.mode == mode)
  498. return;
  499. if (mode == Mode.ScreenMap)
  500. {
  501. if (!screenIdentification.Screen.Active)
  502. {
  503. Info.text = "先定位屏幕";
  504. return;
  505. }
  506. Info.text = "按ESC退出";
  507. SetScreen(Color.black);
  508. Info.transform.SetAsLastSibling();
  509. this.mode = Mode.ScreenMap;
  510. }
  511. else if (mode == Mode.InfraredLocate)
  512. {
  513. Info.text = screenIdentification.Screen.Active ? "已定位屏幕" : "定位屏幕失败";
  514. //Info.text = "已识别到屏幕";
  515. SetScreen(null);
  516. foreach (var i in CrosshairInScreen)
  517. i.gameObject.SetActive(false);
  518. FullScreenImage.gameObject.SetActive(false);
  519. Info.transform.SetSiblingIndex(transform.childCount - 4);
  520. this.mode = Mode.InfraredLocate;
  521. DebugTexture(6, null);
  522. //DebugTexture(1, null); //null
  523. // rawImage1.texture = null;
  524. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  525. Console.WriteLine($"{TAG} Mode.InfraredLocate:已识别到屏幕:{ screenIdentification.Screen.Active}");
  526. #endif
  527. }
  528. else if (mode == Mode.ScreenLocateManual)
  529. {
  530. Info.text = "左键单击屏幕 左下角";
  531. FullScreenImage.gameObject.SetActive(true);
  532. Info.transform.SetSiblingIndex(transform.childCount - 1);
  533. // var newTex = WebCamera.webCamTexture.AutoLight(10);
  534. //DebugTexture(1, TextureToTexture2D(rawImage.texture));
  535. CreateUVCTexture2DIfNeeded();
  536. DebugTexture(6, mUVCTexture2D.zimAutoLight(brightness));
  537. //mUVCTexture2DTemp = TextureToTexture2D(mUVCCameraInfo.previewTexture);
  538. //DebugTexture(6, mUVCTexture2DTemp.zimAutoLight(brightness));
  539. this.mode = Mode.ScreenLocateManual;
  540. }
  541. }
  542. private Texture2D TextureToTexture2D(Texture texture)
  543. {
  544. Texture2D _texture2D = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false, true);
  545. RenderTexture currentRT = RenderTexture.active;
  546. RenderTexture renderTexture = RenderTexture.GetTemporary(
  547. texture.width,
  548. texture.height,
  549. 0,
  550. RenderTextureFormat.ARGB32,
  551. RenderTextureReadWrite.Linear);
  552. Graphics.Blit(texture, renderTexture);
  553. RenderTexture.active = renderTexture;
  554. _texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  555. _texture2D.Apply();
  556. RenderTexture.active = currentRT;
  557. RenderTexture.ReleaseTemporary(renderTexture);
  558. return _texture2D;
  559. }
  560. private void CreateUVCTexture2DIfNeeded()
  561. {
  562. if (mUVCTexture2D != null)
  563. Destroy(mUVCTexture2D);
  564. mUVCTexture2D = TextureToTexture2D(mUVCTexture);
  565. }
  566. #region DoubleButton
  567. private DateTime m_firstTime;
  568. private DateTime m_secondTime;
  569. private void Press()
  570. {
  571. Debug.Log("进入手动定位");
  572. BtnScreenLocateManual();
  573. resetTime();
  574. }
  575. public void OnDoubleClick()
  576. {
  577. //超时重置
  578. if (!m_firstTime.Equals(default(DateTime)))
  579. {
  580. var intervalTime = DateTime.Now - m_firstTime;
  581. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  582. if (milliSeconds >= 400)
  583. resetTime();
  584. }
  585. // 按下按钮时对两次的时间进行记录
  586. if (m_firstTime.Equals(default(DateTime)))
  587. m_firstTime = DateTime.Now;
  588. else
  589. m_secondTime = DateTime.Now;
  590. // 在第二次点击触发,时差小于400ms触发
  591. if (!m_firstTime.Equals(default(DateTime)) && !m_secondTime.Equals(default(DateTime)))
  592. {
  593. var intervalTime = m_secondTime - m_firstTime;
  594. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  595. if (milliSeconds < 400)
  596. Press();
  597. else
  598. resetTime();
  599. }
  600. }
  601. private void resetTime()
  602. {
  603. m_firstTime = default(DateTime);
  604. m_secondTime = default(DateTime);
  605. }
  606. #endregion
  607. #region 性能检测相关
  608. void InvalidateTimings()
  609. {
  610. m_ValidHistoryFrames = 0;
  611. m_AverageTime = float.NaN;
  612. m_MedianTime = float.NaN;
  613. m_MinTime = float.NaN;
  614. m_MaxTime = float.NaN;
  615. }
  616. void UpdateInputs()
  617. {
  618. //重置
  619. if (Input.GetKeyDown(KeyCode.UpArrow))
  620. {
  621. InvalidateTimings();
  622. }
  623. }
  624. #endregion
  625. }