ScreenLocate.cs 26 KB

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