ScreenLocate.cs 33 KB

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