ScreenLocate.cs 24 KB

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