ScreenLocate.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. #define ENABLE_LOG
  2. using InfraredManager;
  3. using o0;
  4. using Serenegiant.UVC;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using ZIM;
  11. using static Serenegiant.UVC.UVCManager;
  12. using Color = UnityEngine.Color;
  13. [RequireComponent(typeof(Canvas))]
  14. public partial class ScreenLocate : MonoBehaviour
  15. {
  16. public InfraredCameraHelper InfraredCameraHelper;
  17. private const string TAG = "ScreenLocate#";
  18. enum Mode
  19. {
  20. InfraredLocate,
  21. ScreenMap,
  22. ScreenLocateManual
  23. }
  24. enum InfraredCount
  25. {
  26. Single,
  27. Double
  28. }
  29. enum Platform
  30. {
  31. Window,
  32. Android
  33. }
  34. Platform mPlatform = Platform.Android;
  35. // 2个灯,顺序根据红外灯的大小 由大到小, 坐标通过 InfraredSpot.ScreenUV 和 InfraredSpot.CameraLocation 获得
  36. public InfraredSpot[] InfraredSpots
  37. {
  38. get
  39. {
  40. infraredCount = InfraredCount.Double;
  41. return infraredSpotBuffer;
  42. }
  43. }
  44. // 1个灯, 坐标通过 InfraredSpot.ScreenUV 和 InfraredSpot.CameraLocation 获得
  45. public InfraredSpot InfraredSpotSingle
  46. {
  47. get
  48. {
  49. infraredCount = InfraredCount.Single;
  50. return infraredSpotBuffer[0];
  51. }
  52. }
  53. public InfraredSpot[] infraredSpotBuffer;
  54. InfraredCount infraredCount;
  55. public string GetInfraredCount() { return infraredCount.ToString(); }
  56. public InfraredDemo InfraredDemoMain => FindObjectOfType<InfraredDemo>();
  57. #region UVC 处理的对象
  58. public UVCManager mUVCManager;
  59. public UVCDrawer mUVCDrawer;
  60. public CameraInfo mUVCCameraInfo;
  61. public bool getUVCCameraInfo => mUVCCameraInfo != null ? true : false;
  62. public Vector2 getUVCCameraInfoSize => getUVCCameraInfo ? mUVCCameraInfo.Size : new Vector2(256, 256);
  63. private Texture mUVCTexture;
  64. public Texture getUVCTexture => mUVCTexture;
  65. private Texture2D mUVCTexture2D;
  66. // [SerializeField] Texture2DArray mUVCOutArray;
  67. #endregion
  68. public Text Info;
  69. public List<RectTransform> CrosshairInCamera;
  70. public List<RectTransform> CrosshairInScreen;
  71. public RectTransform ScreenQuad;
  72. public Toggle SaveToggle;
  73. //public Vector2 ScreenLocateCameraSize; // 屏幕识别需要的目标分辨率,摄像机分辨率变化时该分辨率也会跟着调整
  74. public bool ShowScreenQuad = false;
  75. public RawImage rawImage;
  76. public RawImage rawImage1;
  77. public RawImage rawImage2;
  78. public RawImage rawImage3;
  79. public RawImage rawImage4;
  80. public RawImage rawImage5;
  81. public RawImage FullScreenImage;
  82. public InfraredSpotSettings InfraredSpotSettings;
  83. //public ZIMWebCamera zimWebCamera => GetComponent<ZIMWebCamera>();
  84. public Texture2D DebugScreenImage;
  85. public bool DebugOnEditorWin = false;
  86. // private SynchronizationContext mainContext;
  87. //是否单点显示
  88. public bool bSinglePoint = true;//默认单点识别
  89. bool bIdentifyRed = true;//默认设备红色
  90. bool bIdentifyGreen = true;
  91. #region 性能检测相关
  92. public Text m_UITime;
  93. const float m_UIUpdateInterval = 0.1f;
  94. float m_UIUpdateTimer = 0.0f;
  95. List<float> m_History = new List<float>(100);
  96. int m_ValidHistoryFrames = 0;
  97. float m_AverageTime = float.NaN;
  98. float m_MedianTime = float.NaN;
  99. float m_MinTime = float.NaN;
  100. float m_MaxTime = float.NaN;
  101. public float updateInterval = 0.5F;
  102. private double lastInterval;
  103. private int frames = 0;
  104. private float fps;
  105. public Text m_FPS;
  106. #endregion
  107. InfraredLocate infraredLocate;
  108. RectTransform canvas;
  109. Mode mode;
  110. List<Vector2> pointManual = new List<Vector2>();
  111. //o0.Project.WebCam o0WebCam = null;
  112. o0.Project.ScreenIdentification screenIdentification;
  113. public o0.Project.ScreenIdentification getScreenIdentification => screenIdentification;
  114. static public ScreenLocate Main;
  115. static public List<RawImage> DebugImage = new List<RawImage>();
  116. static public RectTransform BackQuad = null;
  117. static public void DebugTexture(int index, Texture texture)
  118. {
  119. Destroy(DebugImage[index].texture);
  120. DebugImage[index].texture = texture;
  121. }
  122. static public void SetScreen(UnityEngine.Color? color = null)
  123. {
  124. if (BackQuad == null)
  125. {
  126. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  127. var background = canvas.Find("Background");
  128. BackQuad = background.GetChild(0).GetComponent<RectTransform>();
  129. }
  130. BackQuad.parent.gameObject.SetActive(color != null);
  131. BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
  132. Debug.Log("Set Screen " + color.GetColorName());
  133. }
  134. static public void SetScreen(Rect rect, UnityEngine.Color? color = null)
  135. {
  136. if (BackQuad == null)
  137. {
  138. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  139. var background = canvas.Find("Background");
  140. BackQuad = background.GetChild(0).GetComponent<RectTransform>();
  141. }
  142. BackQuad.parent.gameObject.SetActive(color != null);
  143. BackQuad.anchorMin = rect.min;
  144. BackQuad.anchorMax = rect.max;
  145. BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
  146. Debug.Log("Set Screen " + color.GetColorName());
  147. }
  148. static void DebugBackQuad(Rect? rect = null)
  149. {
  150. if (BackQuad)
  151. {
  152. BackQuad.parent.GetComponent<RawImage>().enabled = false;
  153. BackQuad.GetComponent<RawImage>().color = Color.white;
  154. BackQuad.parent.gameObject.SetActive(!BackQuad.parent.gameObject.activeSelf);
  155. if (rect.HasValue)
  156. {
  157. BackQuad.anchorMin = rect.Value.min;
  158. BackQuad.anchorMax = rect.Value.max;
  159. }
  160. }
  161. }
  162. public void ReSizeTexture(int width, int height)
  163. {
  164. Debug.Log("Cur mUVCTexture Size: " + mUVCTexture);
  165. return;
  166. if (mUVCTexture.width < width || mUVCTexture.height < height) // 如果当前分辨率太小,则重新new一个texture
  167. {
  168. Texture2D tex = new Texture2D(
  169. width, height,
  170. TextureFormat.ARGB32,
  171. false, /* mipmap */
  172. true /* linear */);
  173. tex.filterMode = FilterMode.Point;
  174. tex.Apply();
  175. mUVCTexture = tex;
  176. mUVCCameraInfo.previewTexture = tex;
  177. var nativeTexPtr = mUVCCameraInfo.previewTexture.GetNativeTexturePtr();
  178. }
  179. }
  180. void Awake()
  181. {
  182. Main = this;
  183. //#if !UNITY_EDITOR_WIN
  184. // DebugOnWin = false;
  185. //#endif
  186. //if (mUVCDrawer)
  187. // mUVCDrawer.StartPreviewAction += UVCIsReady;
  188. }
  189. void OnDestroy()
  190. {
  191. //if (mUVCDrawer)
  192. // mUVCDrawer.StartPreviewAction -= UVCIsReady;
  193. }
  194. void Start()
  195. {
  196. //mainContext = SynchronizationContext.Current;
  197. DebugImage.Add(rawImage);
  198. DebugImage.Add(rawImage1);
  199. DebugImage.Add(rawImage2);
  200. DebugImage.Add(rawImage3);
  201. DebugImage.Add(rawImage4);
  202. DebugImage.Add(rawImage5);
  203. DebugImage.Add(FullScreenImage);
  204. canvas = transform.GetComponent<RectTransform>();
  205. mode = Mode.InfraredLocate;
  206. //if (DebugScreenImage)
  207. //{
  208. // screenIdentification = new o0.Project.ScreenIdentification(new o0.Geometry2D.Vector<int>(DebugScreenImage.width, DebugScreenImage.height));
  209. // WebCamIsReady(DebugScreenImage);
  210. // screenIdentification.LocateScreen();
  211. //}
  212. infraredCount = InfraredCount.Single;
  213. #region 性能检测相关
  214. for (var i = 0; i < m_History.Capacity; ++i)
  215. {
  216. m_History.Add(0.0f);
  217. }
  218. lastInterval = Time.realtimeSinceStartup;
  219. frames = 0;
  220. #endregion
  221. }
  222. //ZIMWebCamera场景使用
  223. public void WebCamIsReady(Texture texture)
  224. {
  225. mPlatform = Platform.Window;
  226. mUVCTexture = texture;
  227. mUVCCameraInfo = new CameraInfo(mUVCTexture);
  228. brightness = 0;
  229. //UVC准备好
  230. InfraredCameraHelper?.InvokeOnUVCIsReady(mUVCCameraInfo);
  231. }
  232. //手机端UVCCamra使用
  233. public void UVCIsReady(Texture texture)
  234. {
  235. mPlatform = Platform.Android;
  236. //this.startUVCBtn.interactable = true;
  237. mUVCTexture = texture;
  238. //ARGB32
  239. //mUVCTexture2D = Texture2D.CreateExternalTexture(texture.width, texture.height, TextureFormat.ARGB32, false, true, texture.GetNativeTexturePtr()); //TextureToTexture2D(texture);
  240. //Debug.Log("mUVCTexture2D isReable:" + mUVCTexture2D.isReadable);
  241. //Debug.Log("mUVCTexture2D mipmapCount:" + (mUVCTexture2D.mipmapCount > 1));
  242. List<CameraInfo> cameraInfos = mUVCManager.GetAttachedDevices();
  243. mUVCCameraInfo = cameraInfos[cameraInfos.Count - 1];
  244. Debug.Log("mUVCCameraInfo InvokeOnUVCIsReady:" + mUVCCameraInfo);
  245. //UVC准备好
  246. InfraredCameraHelper?.InvokeOnUVCIsReady(mUVCCameraInfo);
  247. }
  248. public void startUVC()
  249. {
  250. }
  251. int brightness = 0;
  252. //public Text brightnessText;
  253. //public void SliderBrightness(Slider slider)
  254. //{
  255. // var _value = slider.value;
  256. // brightness = (int)_value;
  257. // brightnessText.text = (2 + brightness) + "";
  258. //}
  259. float redfilterValue = 0.8f;
  260. public void SetInfraredLocateBrightnessThreshold(float value)
  261. {
  262. if (value >= 0 && value <= 1) {
  263. redfilterValue = value;
  264. if (infraredLocate != null)
  265. {
  266. infraredLocate.SetBrightnessThreshold(value); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
  267. }
  268. }
  269. }
  270. void Update()
  271. {
  272. ++frames;
  273. float timeNow = Time.realtimeSinceStartup;
  274. if (timeNow > lastInterval + updateInterval)
  275. {
  276. fps = (float)(frames / (timeNow - lastInterval));
  277. frames = 0;
  278. lastInterval = timeNow;
  279. }
  280. if (m_FPS != null)
  281. m_FPS.text = "FPS:" + fps.ToString("f2");
  282. if (mUVCCameraInfo == null) return;
  283. if (screenIdentification == null)
  284. {
  285. screenIdentification = new o0.Project.ScreenIdentification();
  286. //screenIdentification.OnLocateScreenEnter += OnLocateScreenEnter;
  287. //screenIdentification.OnLocateScreenEnd += OnLocateScreenEnd;
  288. }
  289. if (infraredLocate == null)
  290. {
  291. infraredLocate = new InfraredLocate(mUVCCameraInfo, screenIdentification, InfraredSpotSettings);
  292. //float redfilterValue = PlayerPrefs.GetFloat("Init redFilterSliderValue", 0.8f);
  293. Debug.Log("Init Red filterValue:" + redfilterValue);
  294. infraredLocate.SetBrightnessThreshold(redfilterValue); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
  295. }
  296. screenIdentification.Screen.CurCameraSize = getUVCCameraInfoSize; // 同步分辨率
  297. if (mode == Mode.ScreenLocateManual)
  298. {
  299. //if (Input.GetMouseButtonDown(0))
  300. //{
  301. // var mouse = Input.mousePosition;
  302. // var u = mouse.x / Screen.width;
  303. // var v = mouse.y / Screen.height;
  304. // u = Math.Clamp(u, 0, 1);
  305. // v = Math.Clamp(v, 0, 1);
  306. // pointManual.Add(new Vector2(u * mUVCTexture.width, v * mUVCTexture.height));
  307. // var obj = Instantiate(Resources.Load("Point")) as GameObject;
  308. // obj.transform.SetParent(FullScreenImage.transform);
  309. // obj.transform.localPosition = new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), FullScreenImage.rectTransform.rect);
  310. // if (pointManual.Count == 1)
  311. // Info.text = "左键单击屏幕 右下角";
  312. // else if (pointManual.Count == 2)
  313. // Info.text = "左键单击屏幕 右上角";
  314. // else if (pointManual.Count == 3)
  315. // Info.text = "左键单击屏幕 左上角";
  316. // else if (pointManual.Count == 4)
  317. // {
  318. // screenIdentification.LocateScreenManual(new OrdinalQuadrilateral(pointManual[0].o0Vector(), pointManual[1].o0Vector(), pointManual[3].o0Vector(), pointManual[2].o0Vector()));
  319. // pointManual.Clear();
  320. // ShowScreen(screenIdentification.Screen.Quad);
  321. // foreach (Transform i in FullScreenImage.transform)
  322. // Destroy(i.gameObject);
  323. // ToMode(Mode.InfraredLocate);
  324. // }
  325. //}
  326. return;
  327. }
  328. //var t0 = Time.realtimeSinceStartup;
  329. /* New*/
  330. //Debug.Log((mUVCCameraInfo != null) +" = "+ mUVCCameraInfo.IsPreviewing + " = "+ screenIdentification.Screen.Active);
  331. if (mUVCCameraInfo != null && mUVCCameraInfo.IsPreviewing) // 成功定位屏幕后才做红外识别
  332. {
  333. CreateUVCTexture2DIfNeeded();
  334. if (!screenIdentification.Update(mUVCTexture2D))
  335. {
  336. if (!screenIdentification.Screen.Active)
  337. {
  338. //DebugTexture(1, mUVCTexture2D.zimAutoLightSimple());
  339. return;
  340. }
  341. //if (mUVCCameraInfo.Size != ScreenLocateCameraSize) // 摄像机分辨率发生改变时执行
  342. //{
  343. // RefreshScreenQuad(mUVCCameraInfo.Size);
  344. // return;
  345. //}
  346. if (mode == Mode.InfraredLocate)
  347. {
  348. //0,0, cameraTexture2D.width, cameraTexture2D.height,0
  349. var pixels = mUVCTexture2D.GetPixels(); // 从左往右、从下往上
  350. //InfraredSpots = infraredLocate.Update(pixels);
  351. if (bSinglePoint)
  352. infraredSpotBuffer = infraredLocate.UpdateSingle(pixels);
  353. else
  354. infraredSpotBuffer = infraredLocate.Update(pixels);
  355. if (mPlatform == Platform.Window) //渲染ui上面的点。进入游戏可以隐藏
  356. {
  357. for (int i = 0; i < infraredSpotBuffer.Length; i++)
  358. {
  359. if (infraredSpotBuffer[i].CameraLocation != null)
  360. {
  361. // 检测到光点
  362. var posInCanvas = infraredSpotBuffer[i].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(mUVCCameraInfo.Size, rawImage.rectTransform.rect);
  363. CrosshairInCamera[i].gameObject.SetActive(true);
  364. CrosshairInCamera[i].anchoredPosition = posInCanvas;
  365. }
  366. else
  367. CrosshairInCamera[i].gameObject.SetActive(false);
  368. }
  369. }
  370. //手机端使用
  371. if (mPlatform == Platform.Android && infraredSpotBuffer.Length > 0)
  372. {
  373. int redIndex = 0;
  374. int greenIndex = 1;
  375. //仅仅第一个点显示(如果最大点出界了会闪烁)
  376. if (bSinglePoint)
  377. {
  378. redIndex = 0; //单点识别是,可以选择切换颜色
  379. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  380. {
  381. string str = "Single:";
  382. Info.text = str + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  383. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  384. onFilterPos(infraredSpotBuffer[redIndex].ScreenUV.Value);
  385. }
  386. }
  387. else
  388. {
  389. //雙點模式下選擇第一個點
  390. if (bIdentifyRed && !bIdentifyGreen)
  391. {
  392. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  393. {
  394. Info.text = "Red" + redIndex + ":" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  395. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  396. onFilterPos2(infraredSpotBuffer[redIndex].ScreenUV.Value, redIndex);
  397. }
  398. else
  399. {
  400. Info.text = "未检测到红色最大点!";
  401. }
  402. }
  403. else if (!bIdentifyRed && bIdentifyGreen)
  404. {
  405. if (infraredSpotBuffer[greenIndex].ScreenUV != null)
  406. {
  407. Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
  408. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
  409. onFilterPos2(infraredSpotBuffer[greenIndex].ScreenUV.Value, greenIndex);
  410. }
  411. else
  412. {
  413. Info.text = "未检测到绿色点!";
  414. }
  415. }
  416. else
  417. {
  418. //两个不选择和两个全选都跑识别两个点
  419. //自動切換 检测到光点
  420. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  421. {
  422. Info.text = "Red:" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  423. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  424. onFilterPos2(infraredSpotBuffer[redIndex].ScreenUV.Value, redIndex);
  425. }
  426. else if (infraredSpotBuffer[greenIndex].ScreenUV != null)
  427. {
  428. Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
  429. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
  430. onFilterPos2(infraredSpotBuffer[greenIndex].ScreenUV.Value, greenIndex);
  431. }
  432. else
  433. {
  434. Info.text = "未检测到点!";
  435. }
  436. }
  437. }
  438. }
  439. }
  440. else if (mode == Mode.ScreenMap && DebugOnEditorWin)
  441. {
  442. var pixels = mUVCTexture2D.GetPixels();
  443. if (infraredCount == InfraredCount.Single)
  444. infraredSpotBuffer = infraredLocate.UpdateSingle(pixels);
  445. else if (infraredCount == InfraredCount.Double)
  446. infraredSpotBuffer = infraredLocate.Update(pixels);
  447. for (int i = 0; i < infraredSpotBuffer.Length; i++)
  448. {
  449. if (infraredSpotBuffer[i].ScreenUV != null)
  450. {
  451. // 检测到光点
  452. var posInCanvas = infraredSpotBuffer[i].ScreenUV.Value.pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), canvas.rect);
  453. CrosshairInScreen[i].gameObject.SetActive(true);
  454. CrosshairInScreen[i].anchoredPosition = posInCanvas;
  455. }
  456. else
  457. CrosshairInScreen[i].gameObject.SetActive(false);
  458. }
  459. if (Input.GetKeyDown(KeyCode.Escape))
  460. ToMode(Mode.InfraredLocate);
  461. }
  462. }
  463. }
  464. //var t1 = Time.realtimeSinceStartup;
  465. //var dt = t1 - t0;
  466. //m_History[m_ValidHistoryFrames % m_History.Count] = dt;
  467. //++m_ValidHistoryFrames;
  468. //m_UIUpdateTimer += Time.deltaTime;
  469. //if (m_UIUpdateTimer >= m_UIUpdateInterval)
  470. //{
  471. // m_UIUpdateTimer = 0.0f;
  472. // if (m_ValidHistoryFrames >= m_History.Count)
  473. // {
  474. // m_ValidHistoryFrames = 0;
  475. // m_AverageTime = 0.0f;
  476. // m_MinTime = float.PositiveInfinity;
  477. // m_MaxTime = float.NegativeInfinity;
  478. // {
  479. // for (var i = 0; i < m_History.Count; i++)
  480. // {
  481. // var time = m_History[i];
  482. // m_AverageTime += time;
  483. // m_MinTime = Mathf.Min(m_MinTime, time);
  484. // m_MaxTime = Mathf.Max(m_MaxTime, time);
  485. // }
  486. // m_AverageTime /= m_History.Count;
  487. // }
  488. // {
  489. // m_History.Sort();
  490. // // Odd-length history?
  491. // if ((m_History.Count & 1) != 0)
  492. // {
  493. // m_MedianTime = m_History[m_History.Count / 2];
  494. // }
  495. // else
  496. // {
  497. // m_MedianTime = (m_History[m_History.Count / 2] + m_History[m_History.Count / 2 - 1]) / 2.0f;
  498. // }
  499. // }
  500. // }
  501. // 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";
  502. // //Method: {m_Method} {UnityEngine.SceneManagement.SceneManager.GetActiveScene().name} |
  503. // if (m_UITime != null)
  504. // 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}";
  505. //}
  506. //UpdateInputs();
  507. }
  508. Vector2 targetPos = Vector2.zero;
  509. Vector2 movePos = Vector2.zero;
  510. int moveSpeed = 20;
  511. public float filterDis = 3.0f;
  512. void onFilterPos(Vector2 _vector2Pos)
  513. {
  514. Vector2 np = new Vector2(_vector2Pos.x * Screen.width, _vector2Pos.y * Screen.height); //_vector2Pos.pixelToLocalPosition_AnchorCenter(Vector2.one, (transform as RectTransform).rect);
  515. if (Vector2.Distance(np, targetPos) >= filterDis)
  516. {
  517. targetPos = np;
  518. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(targetPos.x, targetPos.y, 0));
  519. InfraredCameraHelper.InvokeOnPositionUpdate(targetPos);
  520. }
  521. //movePos = Vector3.Lerp(movePos, targetPos, Time.deltaTime * moveSpeed);
  522. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(movePos.x, movePos.y, 0));
  523. }
  524. Vector2[] _targetPoints2 = new Vector2[] { Vector2.zero, Vector2.zero };
  525. void onFilterPos2(Vector2 _vector2Pos, int index)
  526. {
  527. Vector2 np = new Vector2(_vector2Pos.x * Screen.width, _vector2Pos.y * Screen.height);
  528. if (Vector2.Distance(np, _targetPoints2[index]) >= filterDis)
  529. {
  530. _targetPoints2[index] = np;
  531. InfraredCameraHelper.InvokeOnPositionUpdate2(_targetPoints2[index], index);
  532. }
  533. }
  534. #region 自动识别
  535. int Capture = 30;
  536. int Delay = 30;
  537. int DefaultResolutionIndex;
  538. readonly int HighScreenLocateResolutionIndex = 2; // 自动识别时,摄像机分辨率固定为1280 * 720 ( 对应索引是2 ),数值可以根据需要修改
  539. public void BtnScreenLocate()
  540. {
  541. if (DebugScreenImage)
  542. {
  543. screenIdentification = new o0.Project.ScreenIdentification();
  544. WebCamIsReady(DebugScreenImage);
  545. CreateUVCTexture2DIfNeeded();
  546. }
  547. Debug.Log("BtnScreenLocate Capture:" + Capture + " ,Delay: " + Delay);
  548. screenIdentification.LocateScreen(Capture, Delay);
  549. }
  550. public void OnLocateScreenEnter()
  551. {
  552. DefaultResolutionIndex = InfraredDemoMain?.ResolutionIndex ?? 0; // 记录一下进入前的分辨率(游戏场景的分辨率,比识别时更低)
  553. InfraredDemoMain?.SetResolutionNew(HighScreenLocateResolutionIndex);
  554. //CreateUVCTexture2DIfNeeded();
  555. }
  556. public void OnLocateScreenEnd()
  557. {
  558. Debug.Log("结束捕获,当前摄像机分辨率为: " + mUVCCameraInfo.Size);
  559. //ScreenLocateCameraSize = mUVCCameraInfo.Size; // 记录本次屏幕识别的分辨率(目前采用高分辨率做识别,识别结束后调回低分辨率)
  560. InfraredDemoMain?.SetResolutionNew(DefaultResolutionIndex);
  561. }
  562. /**
  563. * 与UVC设备协商时
  564. * H.264是否优先协商
  565. * 仅安卓实机有效
  566. * true: H.264 > MJPEG > YUV
  567. * false: MJPEG > H.264 > YUV
  568. */
  569. public bool PreferH264 = false;
  570. private const int FRAME_TYPE_MJPEG = 0x000007;
  571. private const int FRAME_TYPE_H264 = 0x000014;
  572. public void Resize(int width, int height)
  573. {
  574. if (mUVCCameraInfo == null) return;
  575. bool PreferH264 = mUVCManager.PreferH264;
  576. int[] frameTypes = {
  577. PreferH264 ? FRAME_TYPE_H264 : FRAME_TYPE_MJPEG,
  578. PreferH264 ? FRAME_TYPE_MJPEG : FRAME_TYPE_H264,
  579. };
  580. foreach (var frameType in frameTypes)
  581. {
  582. Debug.Log("Resize frameType:" + frameType + " = " + width + " : " + height);
  583. if (UVCManager.onResize(mUVCCameraInfo.device.id, frameType, width, height) == 0)
  584. {
  585. Debug.Log("frameType:" + frameType);
  586. break;
  587. }
  588. }
  589. ReSizeTexture(width, height);
  590. //mUVCCameraInfo.SetSize(width, height); // 手动记录分辨率,这里可能会有问题 width和height是期望的分辨率而不是当前摄像机实际分辨率
  591. }
  592. #endregion
  593. public void BtnScreenMap()
  594. {
  595. ToMode(Mode.ScreenMap);
  596. }
  597. //进入手动定位屏幕
  598. public void BtnScreenLocateManual()
  599. {
  600. ToMode(Mode.ScreenLocateManual);
  601. }
  602. public static List<Vector2> quadUnityVectorList = new();
  603. // 标记屏幕的四个角
  604. public void ShowScreen(QuadrilateralInCamera screen)
  605. {
  606. if (screen == null)
  607. {
  608. Info.text = "识别屏幕失败";
  609. return;
  610. }
  611. Info.text = "已识别到屏幕";
  612. if (ShowScreenQuad)
  613. {
  614. ScreenQuad.gameObject.SetActive(true);
  615. //quadUnityVectorList.Clear();
  616. for (int i = 0; i < 4; i++)
  617. {
  618. if (DebugOnEditorWin)
  619. {
  620. RectTransform t = ScreenQuad.GetChild(i) as RectTransform;
  621. t.anchoredPosition = screen.Quad[i].UnityVector().pixelToLocalPosition_AnchorCenter(screen.CameraSize, ScreenQuad.rect);
  622. }
  623. //mUVCCameraInfo.Size
  624. //自动识别时候,记录四个点
  625. //quadUnityVectorList.Add(quad[i].UnityVector());
  626. }
  627. quadUnityVectorList = screen.GetVertexList(); // 记录四个点
  628. SaveScreenLocateVectorList();
  629. SyncInfraredDemo();
  630. SyncInfraredScreenPositioningView();
  631. }
  632. }
  633. static public void SaveScreenLocateVectorList()
  634. {
  635. string saveStr = string.Join(";", quadUnityVectorList.Select(v => $"{v.x},{v.y}")); //,{v.z}
  636. Debug.Log("SaveScreenLocateVectorList: " + saveStr);
  637. PlayerPrefs.SetString("ScreenLocateVectorList", saveStr);
  638. }
  639. static public void GetScreenLocateVectorList()
  640. {
  641. string posListStr = PlayerPrefs.GetString("ScreenLocateVectorList", "");
  642. if (!string.IsNullOrWhiteSpace(posListStr))
  643. {
  644. quadUnityVectorList.Clear();
  645. quadUnityVectorList = posListStr.Split(';')
  646. .Select(s =>
  647. {
  648. string[] parts = s.Split(',');
  649. return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
  650. })
  651. .ToList();
  652. }
  653. }
  654. public void SyncInfraredDemo()
  655. {
  656. if (quadUnityVectorList.Count == 0) return;
  657. Vector2 texSize = getUVCCameraInfoSize;
  658. Debug.Log("quadUnityVectorList" + quadUnityVectorList.Count);
  659. //同步到infaredDemo
  660. FindObjectOfType<InfraredDemo>()?.SetLocatePointsToCameraRender(
  661. quadUnityVectorList,
  662. 1,
  663. 1);
  664. }
  665. public void SyncInfraredScreenPositioningView()
  666. {
  667. if (quadUnityVectorList.Count == 0) return;
  668. //同步位置
  669. FindObjectOfType<InfraredScreenPositioningView>()?.SyncScreenPosition();
  670. }
  671. void ToMode(Mode mode)
  672. {
  673. if (this.mode == mode)
  674. return;
  675. if (mode == Mode.ScreenMap)
  676. {
  677. if (!screenIdentification.Screen.Active)
  678. {
  679. Info.text = "先定位屏幕";
  680. return;
  681. }
  682. Info.text = "按ESC退出";
  683. SetScreen(Color.black);
  684. Info.transform.SetAsLastSibling();
  685. this.mode = Mode.ScreenMap;
  686. }
  687. else if (mode == Mode.InfraredLocate)
  688. {
  689. Info.text = screenIdentification.Screen.Active ? "已定位屏幕" : "定位屏幕失败";
  690. //Info.text = "已识别到屏幕";
  691. SetScreen(null);
  692. foreach (var i in CrosshairInScreen)
  693. i.gameObject.SetActive(false);
  694. FullScreenImage.gameObject.SetActive(false);
  695. Info.transform.SetSiblingIndex(transform.childCount - 4);
  696. this.mode = Mode.InfraredLocate;
  697. DebugTexture(6, null);
  698. //DebugTexture(1, null); //null
  699. // rawImage1.texture = null;
  700. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  701. Console.WriteLine($"{TAG} Mode.InfraredLocate:已识别到屏幕:{screenIdentification.Screen.Active}");
  702. #endif
  703. }
  704. else if (mode == Mode.ScreenLocateManual)
  705. {
  706. Info.text = "左键单击屏幕 左下角";
  707. FullScreenImage.gameObject.SetActive(true);
  708. Info.transform.SetSiblingIndex(transform.childCount - 1);
  709. // var newTex = WebCamera.webCamTexture.AutoLight(10);
  710. //DebugTexture(1, TextureToTexture2D(rawImage.texture));
  711. CreateUVCTexture2DIfNeeded();
  712. DebugTexture(6, mUVCTexture2D.zimAutoLight(brightness));
  713. //mUVCTexture2DTemp = TextureToTexture2D(mUVCCameraInfo.previewTexture);
  714. //DebugTexture(6, mUVCTexture2DTemp.zimAutoLight(brightness));
  715. this.mode = Mode.ScreenLocateManual;
  716. }
  717. }
  718. private Texture2D TextureToTexture2D(Texture texture, int width = 0, int height = 0)
  719. {
  720. if (width == 0)
  721. width = texture.width;
  722. if (height == 0)
  723. height = texture.height;
  724. Texture2D _texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
  725. RenderTexture currentRT = RenderTexture.active;
  726. RenderTexture renderTexture = RenderTexture.GetTemporary(
  727. width,
  728. height,
  729. 0,
  730. RenderTextureFormat.ARGB32,
  731. RenderTextureReadWrite.Linear);
  732. Graphics.Blit(texture, renderTexture);
  733. RenderTexture.active = renderTexture;
  734. _texture2D.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  735. _texture2D.Apply();
  736. RenderTexture.active = currentRT;
  737. RenderTexture.ReleaseTemporary(renderTexture);
  738. return _texture2D;
  739. }
  740. //public void CreateUVCTexture2DFocusSizeIfNeeded(int width, int height)
  741. //{
  742. // if (mUVCTexture2D != null)
  743. // Destroy(mUVCTexture2D);
  744. // mUVCTexture2D = TextureToTexture2D(mUVCTexture, width, height);
  745. //}
  746. private void CreateUVCTexture2DIfNeeded()
  747. {
  748. if (mUVCTexture2D != null)
  749. Destroy(mUVCTexture2D);
  750. mUVCTexture2D = TextureToTexture2D(mUVCTexture);
  751. }
  752. #region DoubleButton
  753. private DateTime m_firstTime;
  754. private DateTime m_secondTime;
  755. private void Press()
  756. {
  757. Debug.Log("进入手动定位");
  758. BtnScreenLocateManual();
  759. resetTime();
  760. }
  761. public void OnDoubleClick()
  762. {
  763. //超时重置
  764. if (!m_firstTime.Equals(default(DateTime)))
  765. {
  766. var intervalTime = DateTime.Now - m_firstTime;
  767. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  768. if (milliSeconds >= 400)
  769. resetTime();
  770. }
  771. // 按下按钮时对两次的时间进行记录
  772. if (m_firstTime.Equals(default(DateTime)))
  773. m_firstTime = DateTime.Now;
  774. else
  775. m_secondTime = DateTime.Now;
  776. // 在第二次点击触发,时差小于400ms触发
  777. if (!m_firstTime.Equals(default(DateTime)) && !m_secondTime.Equals(default(DateTime)))
  778. {
  779. var intervalTime = m_secondTime - m_firstTime;
  780. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  781. if (milliSeconds < 400)
  782. Press();
  783. else
  784. resetTime();
  785. }
  786. }
  787. private void resetTime()
  788. {
  789. m_firstTime = default(DateTime);
  790. m_secondTime = default(DateTime);
  791. }
  792. #endregion
  793. #region 性能检测相关
  794. void InvalidateTimings()
  795. {
  796. m_ValidHistoryFrames = 0;
  797. m_AverageTime = float.NaN;
  798. m_MedianTime = float.NaN;
  799. m_MinTime = float.NaN;
  800. m_MaxTime = float.NaN;
  801. }
  802. void UpdateInputs()
  803. {
  804. //重置
  805. if (Input.GetKeyDown(KeyCode.UpArrow))
  806. {
  807. InvalidateTimings();
  808. }
  809. }
  810. #endregion
  811. }