ScreenLocate.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. DebugOnEditorWin = 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. public void SetInfraredLocateBrightnessThreshold(float value)
  260. {
  261. if (infraredLocate != null)
  262. {
  263. if (value >= 0 && value <= 1)
  264. infraredLocate.SetBrightnessThreshold(value); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
  265. }
  266. }
  267. void Update()
  268. {
  269. ++frames;
  270. float timeNow = Time.realtimeSinceStartup;
  271. if (timeNow > lastInterval + updateInterval)
  272. {
  273. fps = (float)(frames / (timeNow - lastInterval));
  274. frames = 0;
  275. lastInterval = timeNow;
  276. }
  277. if (m_FPS != null)
  278. m_FPS.text = "FPS:" + fps.ToString("f2");
  279. if (mUVCCameraInfo == null) return;
  280. if (screenIdentification == null)
  281. {
  282. screenIdentification = new o0.Project.ScreenIdentification();
  283. screenIdentification.OnLocateScreenEnter += OnLocateScreenEnter;
  284. screenIdentification.OnLocateScreenEnd += OnLocateScreenEnd;
  285. }
  286. if (infraredLocate == null)
  287. {
  288. infraredLocate = new InfraredLocate(mUVCCameraInfo, screenIdentification, InfraredSpotSettings);
  289. float redfilterValue = PlayerPrefs.GetFloat("Init redFilterSliderValue", 0.8f);
  290. Debug.Log("Init Red filterValue:" + redfilterValue);
  291. infraredLocate.SetBrightnessThreshold(redfilterValue); // 参数是 红外灯的亮度阈值,阈值越小能够检测到的亮度就越低,默认值是0.93
  292. }
  293. screenIdentification.Screen.CurCameraSize = getUVCCameraInfoSize; // 同步分辨率
  294. if (mode == Mode.ScreenLocateManual)
  295. {
  296. //if (Input.GetMouseButtonDown(0))
  297. //{
  298. // var mouse = Input.mousePosition;
  299. // var u = mouse.x / Screen.width;
  300. // var v = mouse.y / Screen.height;
  301. // u = Math.Clamp(u, 0, 1);
  302. // v = Math.Clamp(v, 0, 1);
  303. // pointManual.Add(new Vector2(u * mUVCTexture.width, v * mUVCTexture.height));
  304. // var obj = Instantiate(Resources.Load("Point")) as GameObject;
  305. // obj.transform.SetParent(FullScreenImage.transform);
  306. // obj.transform.localPosition = new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), FullScreenImage.rectTransform.rect);
  307. // if (pointManual.Count == 1)
  308. // Info.text = "左键单击屏幕 右下角";
  309. // else if (pointManual.Count == 2)
  310. // Info.text = "左键单击屏幕 右上角";
  311. // else if (pointManual.Count == 3)
  312. // Info.text = "左键单击屏幕 左上角";
  313. // else if (pointManual.Count == 4)
  314. // {
  315. // screenIdentification.LocateScreenManual(new OrdinalQuadrilateral(pointManual[0].o0Vector(), pointManual[1].o0Vector(), pointManual[3].o0Vector(), pointManual[2].o0Vector()));
  316. // pointManual.Clear();
  317. // ShowScreen(screenIdentification.Screen.Quad);
  318. // foreach (Transform i in FullScreenImage.transform)
  319. // Destroy(i.gameObject);
  320. // ToMode(Mode.InfraredLocate);
  321. // }
  322. //}
  323. return;
  324. }
  325. //var t0 = Time.realtimeSinceStartup;
  326. /* New*/
  327. //Debug.Log((mUVCCameraInfo != null) +" = "+ mUVCCameraInfo.IsPreviewing + " = "+ screenIdentification.Screen.Active);
  328. if (mUVCCameraInfo != null && mUVCCameraInfo.IsPreviewing) // 成功定位屏幕后才做红外识别
  329. {
  330. CreateUVCTexture2DIfNeeded();
  331. if (!screenIdentification.Update(mUVCTexture2D))
  332. {
  333. if (!screenIdentification.Screen.Active)
  334. {
  335. //DebugTexture(1, mUVCTexture2D.zimAutoLightSimple());
  336. return;
  337. }
  338. //if (mUVCCameraInfo.Size != ScreenLocateCameraSize) // 摄像机分辨率发生改变时执行
  339. //{
  340. // RefreshScreenQuad(mUVCCameraInfo.Size);
  341. // return;
  342. //}
  343. if (mode == Mode.InfraredLocate)
  344. {
  345. //0,0, cameraTexture2D.width, cameraTexture2D.height,0
  346. var pixels = mUVCTexture2D.GetPixels(); // 从左往右、从下往上
  347. //InfraredSpots = infraredLocate.Update(pixels);
  348. if (bSinglePoint)
  349. infraredSpotBuffer = infraredLocate.UpdateSingle(pixels);
  350. else
  351. infraredSpotBuffer = infraredLocate.Update(pixels);
  352. if (mPlatform == Platform.Window) //渲染ui上面的点。进入游戏可以隐藏
  353. {
  354. for (int i = 0; i < infraredSpotBuffer.Length; i++)
  355. {
  356. if (infraredSpotBuffer[i].CameraLocation != null)
  357. {
  358. // 检测到光点
  359. var posInCanvas = infraredSpotBuffer[i].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(mUVCCameraInfo.Size, rawImage.rectTransform.rect);
  360. CrosshairInCamera[i].gameObject.SetActive(true);
  361. CrosshairInCamera[i].anchoredPosition = posInCanvas;
  362. }
  363. else
  364. CrosshairInCamera[i].gameObject.SetActive(false);
  365. }
  366. }
  367. //手机端使用
  368. if (mPlatform == Platform.Android && infraredSpotBuffer.Length > 0)
  369. {
  370. int redIndex = 0;
  371. int greenIndex = 1;
  372. //仅仅第一个点显示(如果最大点出界了会闪烁)
  373. if (bSinglePoint)
  374. {
  375. redIndex = 0; //单点识别是,可以选择切换颜色
  376. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  377. {
  378. string str = "Single:";
  379. Info.text = str + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  380. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  381. onFilterPos(infraredSpotBuffer[redIndex].ScreenUV.Value);
  382. }
  383. }
  384. else
  385. {
  386. //雙點模式下選擇第一個點
  387. if (bIdentifyRed && !bIdentifyGreen)
  388. {
  389. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  390. {
  391. Info.text = "Red" + redIndex + ":" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  392. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  393. onFilterPos2(infraredSpotBuffer[redIndex].ScreenUV.Value, redIndex);
  394. }
  395. else
  396. {
  397. Info.text = "未检测到红色最大点!";
  398. }
  399. }
  400. else if (!bIdentifyRed && bIdentifyGreen)
  401. {
  402. if (infraredSpotBuffer[greenIndex].ScreenUV != null)
  403. {
  404. Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
  405. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
  406. onFilterPos2(infraredSpotBuffer[greenIndex].ScreenUV.Value, greenIndex);
  407. }
  408. else
  409. {
  410. Info.text = "未检测到绿色点!";
  411. }
  412. }
  413. else
  414. {
  415. //两个不选择和两个全选都跑识别两个点
  416. //自動切換 检测到光点
  417. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  418. {
  419. Info.text = "Red:" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  420. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  421. onFilterPos2(infraredSpotBuffer[redIndex].ScreenUV.Value, redIndex);
  422. }
  423. else if (infraredSpotBuffer[greenIndex].ScreenUV != null)
  424. {
  425. Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
  426. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
  427. onFilterPos2(infraredSpotBuffer[greenIndex].ScreenUV.Value, greenIndex);
  428. }
  429. else
  430. {
  431. Info.text = "未检测到点!";
  432. }
  433. }
  434. }
  435. }
  436. }
  437. else if (mode == Mode.ScreenMap && DebugOnEditorWin)
  438. {
  439. var pixels = mUVCTexture2D.GetPixels();
  440. if (infraredCount == InfraredCount.Single)
  441. infraredSpotBuffer = infraredLocate.UpdateSingle(pixels);
  442. else if (infraredCount == InfraredCount.Double)
  443. infraredSpotBuffer = infraredLocate.Update(pixels);
  444. for (int i = 0; i < infraredSpotBuffer.Length; i++)
  445. {
  446. if (infraredSpotBuffer[i].ScreenUV != null)
  447. {
  448. // 检测到光点
  449. var posInCanvas = infraredSpotBuffer[i].ScreenUV.Value.pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), canvas.rect);
  450. CrosshairInScreen[i].gameObject.SetActive(true);
  451. CrosshairInScreen[i].anchoredPosition = posInCanvas;
  452. }
  453. else
  454. CrosshairInScreen[i].gameObject.SetActive(false);
  455. }
  456. if (Input.GetKeyDown(KeyCode.Escape))
  457. ToMode(Mode.InfraredLocate);
  458. }
  459. }
  460. }
  461. //var t1 = Time.realtimeSinceStartup;
  462. //var dt = t1 - t0;
  463. //m_History[m_ValidHistoryFrames % m_History.Count] = dt;
  464. //++m_ValidHistoryFrames;
  465. //m_UIUpdateTimer += Time.deltaTime;
  466. //if (m_UIUpdateTimer >= m_UIUpdateInterval)
  467. //{
  468. // m_UIUpdateTimer = 0.0f;
  469. // if (m_ValidHistoryFrames >= m_History.Count)
  470. // {
  471. // m_ValidHistoryFrames = 0;
  472. // m_AverageTime = 0.0f;
  473. // m_MinTime = float.PositiveInfinity;
  474. // m_MaxTime = float.NegativeInfinity;
  475. // {
  476. // for (var i = 0; i < m_History.Count; i++)
  477. // {
  478. // var time = m_History[i];
  479. // m_AverageTime += time;
  480. // m_MinTime = Mathf.Min(m_MinTime, time);
  481. // m_MaxTime = Mathf.Max(m_MaxTime, time);
  482. // }
  483. // m_AverageTime /= m_History.Count;
  484. // }
  485. // {
  486. // m_History.Sort();
  487. // // Odd-length history?
  488. // if ((m_History.Count & 1) != 0)
  489. // {
  490. // m_MedianTime = m_History[m_History.Count / 2];
  491. // }
  492. // else
  493. // {
  494. // m_MedianTime = (m_History[m_History.Count / 2] + m_History[m_History.Count / 2 - 1]) / 2.0f;
  495. // }
  496. // }
  497. // }
  498. // 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";
  499. // //Method: {m_Method} {UnityEngine.SceneManagement.SceneManager.GetActiveScene().name} |
  500. // if (m_UITime != null)
  501. // 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}";
  502. //}
  503. //UpdateInputs();
  504. }
  505. Vector2 targetPos = Vector2.zero;
  506. Vector2 movePos = Vector2.zero;
  507. int moveSpeed = 20;
  508. public float filterDis = 3.0f;
  509. void onFilterPos(Vector2 _vector2Pos)
  510. {
  511. Vector2 np = new Vector2(_vector2Pos.x * Screen.width, _vector2Pos.y * Screen.height); //_vector2Pos.pixelToLocalPosition_AnchorCenter(Vector2.one, (transform as RectTransform).rect);
  512. if (Vector2.Distance(np, targetPos) >= filterDis)
  513. {
  514. targetPos = np;
  515. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(targetPos.x, targetPos.y, 0));
  516. InfraredCameraHelper.InvokeOnPositionUpdate(targetPos);
  517. }
  518. //movePos = Vector3.Lerp(movePos, targetPos, Time.deltaTime * moveSpeed);
  519. //InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(movePos.x, movePos.y, 0));
  520. }
  521. Vector2[] _targetPoints2 = new Vector2[] { Vector2.zero, Vector2.zero };
  522. void onFilterPos2(Vector2 _vector2Pos, int index)
  523. {
  524. Vector2 np = new Vector2(_vector2Pos.x * Screen.width, _vector2Pos.y * Screen.height);
  525. if (Vector2.Distance(np, _targetPoints2[index]) >= filterDis)
  526. {
  527. _targetPoints2[index] = np;
  528. InfraredCameraHelper.InvokeOnPositionUpdate2(_targetPoints2[index], index);
  529. }
  530. }
  531. #region 自动识别
  532. int Capture = 30;
  533. int Delay = 30;
  534. int DefaultResolutionIndex;
  535. readonly int HighScreenLocateResolutionIndex = 2; // 自动识别时,摄像机分辨率固定为1280 * 720 ( 对应索引是2 ),数值可以根据需要修改
  536. public void BtnScreenLocate()
  537. {
  538. if (DebugScreenImage)
  539. {
  540. screenIdentification = new o0.Project.ScreenIdentification();
  541. WebCamIsReady(DebugScreenImage);
  542. CreateUVCTexture2DIfNeeded();
  543. }
  544. Debug.Log("BtnScreenLocate Capture:" + Capture + " ,Delay: " + Delay);
  545. screenIdentification.LocateScreen(Capture, Delay);
  546. }
  547. public void OnLocateScreenEnter()
  548. {
  549. DefaultResolutionIndex = InfraredDemoMain?.ResolutionIndex ?? 0; // 记录一下进入前的分辨率(游戏场景的分辨率,比识别时更低)
  550. InfraredDemoMain?.SetResolutionNew(HighScreenLocateResolutionIndex);
  551. //CreateUVCTexture2DIfNeeded();
  552. }
  553. public void OnLocateScreenEnd()
  554. {
  555. Debug.Log("结束捕获,当前摄像机分辨率为: " + mUVCCameraInfo.Size);
  556. //ScreenLocateCameraSize = mUVCCameraInfo.Size; // 记录本次屏幕识别的分辨率(目前采用高分辨率做识别,识别结束后调回低分辨率)
  557. InfraredDemoMain?.SetResolutionNew(DefaultResolutionIndex);
  558. }
  559. /**
  560. * 与UVC设备协商时
  561. * H.264是否优先协商
  562. * 仅安卓实机有效
  563. * true: H.264 > MJPEG > YUV
  564. * false: MJPEG > H.264 > YUV
  565. */
  566. public bool PreferH264 = false;
  567. private const int FRAME_TYPE_MJPEG = 0x000007;
  568. private const int FRAME_TYPE_H264 = 0x000014;
  569. public void Resize(int width, int height)
  570. {
  571. if (mUVCCameraInfo == null) return;
  572. bool PreferH264 = mUVCManager.PreferH264;
  573. int[] frameTypes = {
  574. PreferH264 ? FRAME_TYPE_H264 : FRAME_TYPE_MJPEG,
  575. PreferH264 ? FRAME_TYPE_MJPEG : FRAME_TYPE_H264,
  576. };
  577. foreach (var frameType in frameTypes)
  578. {
  579. Debug.Log("Resize frameType:" + frameType + " = " + width + " : " + height);
  580. if (UVCManager.onResize(mUVCCameraInfo.device.id, frameType, width, height) == 0)
  581. {
  582. Debug.Log("frameType:" + frameType);
  583. break;
  584. }
  585. }
  586. ReSizeTexture(width, height);
  587. mUVCCameraInfo.SetSize(width, height); // 手动记录分辨率,这里可能会有问题 width和height是期望的分辨率而不是当前摄像机实际分辨率
  588. }
  589. #endregion
  590. public void BtnScreenMap()
  591. {
  592. ToMode(Mode.ScreenMap);
  593. }
  594. //进入手动定位屏幕
  595. public void BtnScreenLocateManual()
  596. {
  597. ToMode(Mode.ScreenLocateManual);
  598. }
  599. public static List<Vector2> quadUnityVectorList = new();
  600. // 标记屏幕的四个角
  601. public void ShowScreen(QuadrilateralInCamera screen)
  602. {
  603. if (screen == null)
  604. {
  605. Info.text = "识别屏幕失败";
  606. return;
  607. }
  608. Info.text = "已识别到屏幕";
  609. if (ShowScreenQuad)
  610. {
  611. ScreenQuad.gameObject.SetActive(true);
  612. //quadUnityVectorList.Clear();
  613. for (int i = 0; i < 4; i++)
  614. {
  615. if (DebugOnEditorWin)
  616. {
  617. RectTransform t = ScreenQuad.GetChild(i) as RectTransform;
  618. t.anchoredPosition = screen.Quad[i].UnityVector().pixelToLocalPosition_AnchorCenter(screen.CameraSize, ScreenQuad.rect);
  619. }
  620. //mUVCCameraInfo.Size
  621. //自动识别时候,记录四个点
  622. //quadUnityVectorList.Add(quad[i].UnityVector());
  623. }
  624. quadUnityVectorList = screen.GetVertexList(); // 记录四个点
  625. SaveScreenLocateVectorList();
  626. SyncInfraredDemo();
  627. SyncInfraredScreenPositioningView();
  628. }
  629. }
  630. static public void SaveScreenLocateVectorList()
  631. {
  632. string saveStr = string.Join(";", quadUnityVectorList.Select(v => $"{v.x},{v.y}")); //,{v.z}
  633. Debug.Log("SaveScreenLocateVectorList: " + saveStr);
  634. PlayerPrefs.SetString("ScreenLocateVectorList", saveStr);
  635. }
  636. static public void GetScreenLocateVectorList()
  637. {
  638. string posListStr = PlayerPrefs.GetString("ScreenLocateVectorList", "");
  639. if (!string.IsNullOrWhiteSpace(posListStr))
  640. {
  641. quadUnityVectorList.Clear();
  642. quadUnityVectorList = posListStr.Split(';')
  643. .Select(s =>
  644. {
  645. string[] parts = s.Split(',');
  646. return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
  647. })
  648. .ToList();
  649. }
  650. }
  651. public void SyncInfraredDemo()
  652. {
  653. if (quadUnityVectorList.Count == 0) return;
  654. Vector2 texSize = getUVCCameraInfoSize;
  655. Debug.Log("quadUnityVectorList" + quadUnityVectorList.Count);
  656. //同步到infaredDemo
  657. FindObjectOfType<InfraredDemo>()?.SetLocatePointsToCameraRender(
  658. quadUnityVectorList,
  659. 1,
  660. 1);
  661. }
  662. public void SyncInfraredScreenPositioningView()
  663. {
  664. if (quadUnityVectorList.Count == 0) return;
  665. //同步位置
  666. FindObjectOfType<InfraredScreenPositioningView>()?.SyncScreenPosition();
  667. }
  668. void ToMode(Mode mode)
  669. {
  670. if (this.mode == mode)
  671. return;
  672. if (mode == Mode.ScreenMap)
  673. {
  674. if (!screenIdentification.Screen.Active)
  675. {
  676. Info.text = "先定位屏幕";
  677. return;
  678. }
  679. Info.text = "按ESC退出";
  680. SetScreen(Color.black);
  681. Info.transform.SetAsLastSibling();
  682. this.mode = Mode.ScreenMap;
  683. }
  684. else if (mode == Mode.InfraredLocate)
  685. {
  686. Info.text = screenIdentification.Screen.Active ? "已定位屏幕" : "定位屏幕失败";
  687. //Info.text = "已识别到屏幕";
  688. SetScreen(null);
  689. foreach (var i in CrosshairInScreen)
  690. i.gameObject.SetActive(false);
  691. FullScreenImage.gameObject.SetActive(false);
  692. Info.transform.SetSiblingIndex(transform.childCount - 4);
  693. this.mode = Mode.InfraredLocate;
  694. DebugTexture(6, null);
  695. //DebugTexture(1, null); //null
  696. // rawImage1.texture = null;
  697. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  698. Console.WriteLine($"{TAG} Mode.InfraredLocate:已识别到屏幕:{screenIdentification.Screen.Active}");
  699. #endif
  700. }
  701. else if (mode == Mode.ScreenLocateManual)
  702. {
  703. Info.text = "左键单击屏幕 左下角";
  704. FullScreenImage.gameObject.SetActive(true);
  705. Info.transform.SetSiblingIndex(transform.childCount - 1);
  706. // var newTex = WebCamera.webCamTexture.AutoLight(10);
  707. //DebugTexture(1, TextureToTexture2D(rawImage.texture));
  708. CreateUVCTexture2DIfNeeded();
  709. DebugTexture(6, mUVCTexture2D.zimAutoLight(brightness));
  710. //mUVCTexture2DTemp = TextureToTexture2D(mUVCCameraInfo.previewTexture);
  711. //DebugTexture(6, mUVCTexture2DTemp.zimAutoLight(brightness));
  712. this.mode = Mode.ScreenLocateManual;
  713. }
  714. }
  715. private Texture2D TextureToTexture2D(Texture texture, int width = 0, int height = 0)
  716. {
  717. if (width == 0)
  718. width = texture.width;
  719. if (height == 0)
  720. height = texture.height;
  721. Texture2D _texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
  722. RenderTexture currentRT = RenderTexture.active;
  723. RenderTexture renderTexture = RenderTexture.GetTemporary(
  724. width,
  725. height,
  726. 0,
  727. RenderTextureFormat.ARGB32,
  728. RenderTextureReadWrite.Linear);
  729. Graphics.Blit(texture, renderTexture);
  730. RenderTexture.active = renderTexture;
  731. _texture2D.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  732. _texture2D.Apply();
  733. RenderTexture.active = currentRT;
  734. RenderTexture.ReleaseTemporary(renderTexture);
  735. return _texture2D;
  736. }
  737. //public void CreateUVCTexture2DFocusSizeIfNeeded(int width, int height)
  738. //{
  739. // if (mUVCTexture2D != null)
  740. // Destroy(mUVCTexture2D);
  741. // mUVCTexture2D = TextureToTexture2D(mUVCTexture, width, height);
  742. //}
  743. private void CreateUVCTexture2DIfNeeded()
  744. {
  745. if (mUVCTexture2D != null)
  746. Destroy(mUVCTexture2D);
  747. mUVCTexture2D = TextureToTexture2D(mUVCTexture);
  748. }
  749. #region DoubleButton
  750. private DateTime m_firstTime;
  751. private DateTime m_secondTime;
  752. private void Press()
  753. {
  754. Debug.Log("进入手动定位");
  755. BtnScreenLocateManual();
  756. resetTime();
  757. }
  758. public void OnDoubleClick()
  759. {
  760. //超时重置
  761. if (!m_firstTime.Equals(default(DateTime)))
  762. {
  763. var intervalTime = DateTime.Now - m_firstTime;
  764. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  765. if (milliSeconds >= 400)
  766. resetTime();
  767. }
  768. // 按下按钮时对两次的时间进行记录
  769. if (m_firstTime.Equals(default(DateTime)))
  770. m_firstTime = DateTime.Now;
  771. else
  772. m_secondTime = DateTime.Now;
  773. // 在第二次点击触发,时差小于400ms触发
  774. if (!m_firstTime.Equals(default(DateTime)) && !m_secondTime.Equals(default(DateTime)))
  775. {
  776. var intervalTime = m_secondTime - m_firstTime;
  777. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  778. if (milliSeconds < 400)
  779. Press();
  780. else
  781. resetTime();
  782. }
  783. }
  784. private void resetTime()
  785. {
  786. m_firstTime = default(DateTime);
  787. m_secondTime = default(DateTime);
  788. }
  789. #endregion
  790. #region 性能检测相关
  791. void InvalidateTimings()
  792. {
  793. m_ValidHistoryFrames = 0;
  794. m_AverageTime = float.NaN;
  795. m_MedianTime = float.NaN;
  796. m_MinTime = float.NaN;
  797. m_MaxTime = float.NaN;
  798. }
  799. void UpdateInputs()
  800. {
  801. //重置
  802. if (Input.GetKeyDown(KeyCode.UpArrow))
  803. {
  804. InvalidateTimings();
  805. }
  806. }
  807. #endregion
  808. }