InfraredCameraHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using SLAMUVC;
  6. using UnityEngine.UI;
  7. using SmartBowSDK;
  8. using o0InfraredLocate.ZIM;
  9. using static ScreenLocate;
  10. namespace InfraredManager
  11. {
  12. public class InfraredCameraHelper
  13. {
  14. #region 单例
  15. private static InfraredCameraHelper _Instance;
  16. public static InfraredCameraHelper GetInstance()
  17. {
  18. if (_Instance == null) _Instance = new();
  19. return _Instance;
  20. }
  21. #endregion
  22. #region 创建销毁
  23. private GameObject _managerObject;
  24. private ScreenLocate _screenLocate;
  25. private UVCManager _uvcManager;
  26. public static List<RawImage> InfraredCameraHelperRawImageList = new();
  27. public void Create(int defWidth = 320, int defHeight = 240, CameraMirror cameraMirror = CameraMirror.NONE)
  28. {
  29. if (_managerObject) return;
  30. string prefabName = "WebCameraView";
  31. var o = UnityEngine.Object.Instantiate(Resources.Load<GameObject>(prefabName));
  32. UnityEngine.Object.DontDestroyOnLoad(o);
  33. o.name = prefabName;
  34. _screenLocate = o.GetComponent<ScreenLocate>();
  35. _screenLocate.InfraredCameraHelper = this;
  36. _uvcManager = o.GetComponent<UVCManager>();
  37. _uvcManager.startUVCManager.AddListener(_screenLocate.UVCIsReady);
  38. _uvcManager.updateUVCManager.AddListener(_screenLocate.UVCUpdate);
  39. _uvcManager.DefaultWidth = defWidth;
  40. _uvcManager.DefaultHeight = defHeight;
  41. _uvcManager.DefaultMirror = cameraMirror;
  42. Debug.Log("InitUVCManagerCamera 初始化相机,size:[" + _uvcManager.DefaultWidth + "," + _uvcManager.DefaultHeight + "]" + ",Mirror:"+ _uvcManager.DefaultMirror);
  43. _managerObject = o;
  44. if (Application.platform == RuntimePlatform.Android) Application.targetFrameRate = 60;
  45. }
  46. /// <summary>
  47. /// 在pc里面创建此预制
  48. /// </summary>
  49. public void CreateToPc()
  50. {
  51. if (_managerObject) return;
  52. string prefabName = "WebCameraView_Pc";
  53. var o = UnityEngine.Object.Instantiate(Resources.Load<GameObject>(prefabName));
  54. UnityEngine.Object.DontDestroyOnLoad(o);
  55. o.name = "WebCameraView";
  56. _screenLocate = o.GetComponent<ScreenLocate>();
  57. _screenLocate.InfraredCameraHelper = this;
  58. _uvcManager = o.GetComponent<UVCManager>();
  59. _managerObject = o;
  60. }
  61. public void Dispose()
  62. {
  63. if (_managerObject) UnityEngine.Object.DestroyImmediate(_managerObject);
  64. if (_Instance == this) _Instance = null;
  65. }
  66. #endregion
  67. #region 画面接口
  68. public void onStartPreview()
  69. {
  70. _uvcManager.onStartPreview();
  71. }
  72. public void onStopPreview()
  73. {
  74. _uvcManager.onStopPreview();
  75. }
  76. /**
  77. * 水平翻转
  78. */
  79. public void onFlipHorizontally()
  80. {
  81. _uvcManager.FlipHorizontally();
  82. }
  83. /**
  84. * 垂直翻转
  85. */
  86. public void onFlipVertically()
  87. {
  88. _uvcManager.FlipVertically();
  89. }
  90. public void onDoubleFlip() {
  91. _uvcManager.DoubleFlip();
  92. }
  93. /**
  94. * 原来的设置
  95. */
  96. public void onFlipDefault() {
  97. _uvcManager.FlipDefault();
  98. }
  99. /// <summary>
  100. /// 获取摄像机对象CameraInfo
  101. /// </summary>
  102. public UVCManager.CameraInfo GetUVCCameraInfo()
  103. {
  104. return _screenLocate.mUVCCameraInfo;
  105. }
  106. /// <summary>
  107. /// 获取相机画面贴图
  108. /// </summary>
  109. public Texture GetCameraTexture()
  110. {
  111. return _screenLocate.getUVCTexture; //_screenLocate.rawImage.texture;
  112. }
  113. /// <summary>
  114. /// 获取相机画面材质
  115. /// </summary>
  116. public Material GetCameraMaterial()
  117. {
  118. return null;//_screenLocate.rawImage.material;
  119. }
  120. /// <summary>
  121. /// 设置亮度(-1~1)
  122. /// </summary>
  123. public void SetBrightness(float value)
  124. {
  125. //_screenLocate.rawImage.material.SetFloat("_Brightness", value);
  126. _screenLocate.pcBrightness = value;
  127. }
  128. /// <summary>
  129. /// 获取亮度(-1~1)
  130. /// </summary>
  131. public float GetBrightness()
  132. {
  133. return _screenLocate.pcBrightness;// _screenLocate.rawImage.material.GetFloat("_Brightness");
  134. }
  135. /// <summary>
  136. /// 设置对比度(-1~1)
  137. /// </summary>
  138. public void SetContrast(float value)
  139. {
  140. // _screenLocate.rawImage.material.SetFloat("_Contrast", value);
  141. _screenLocate.pcContrast = value;
  142. }
  143. /// <summary>
  144. /// 获取对比度(-1~1)
  145. /// </summary>
  146. public float GetContrast()
  147. {
  148. return _screenLocate.pcContrast;//_screenLocate.rawImage.material.GetFloat("_Contrast");
  149. }
  150. /// <summary>
  151. /// 设置相机画面分辨率
  152. /// </summary>
  153. public void SetCameraResolution(int width, int height)
  154. {
  155. if (_uvcManager)
  156. {
  157. _uvcManager.DefaultWidth = width;
  158. _uvcManager.DefaultHeight = height;
  159. }
  160. Debug.Log("[InfraredCameraHelper]设置分辨率 SetCameraResolution,width:" + width + " ,height:" + height);
  161. }
  162. /// <summary>
  163. /// 设置相机画面分辨率
  164. /// </summary>
  165. public void SetCameraResolutionNew(int width, int height)
  166. {
  167. Debug.Log("[InfraredCameraHelper]设置分辨率 SetCameraResolutionNew,width:" + width + " ,height:" + height);
  168. _screenLocate.Resize(width, height);
  169. }
  170. /// <summary>
  171. /// 设置default分辨率
  172. /// </summary>
  173. public void SetHighCameraResolution()
  174. {
  175. Vector2 _highResolution = _screenLocate.mUVCCameraInfo.CurrentHighResolution;
  176. _screenLocate.Resize((int)_highResolution.x, (int)_highResolution.y);
  177. }
  178. /// <summary>
  179. /// 设置Low分辨率
  180. /// </summary>
  181. public void SetLowCameraResolution()
  182. {
  183. Vector2 _lowResolution = _screenLocate.mUVCCameraInfo.CurrentLowResolution;
  184. _screenLocate.Resize((int)_lowResolution.x, (int)_lowResolution.y);
  185. }
  186. #endregion
  187. #region 操作接口
  188. /// <summary>
  189. /// 获取screenLocate 对象
  190. /// </summary>
  191. /// <returns>ScreenLocate</returns>
  192. public ScreenLocate getScreenLocate()
  193. {
  194. return _screenLocate;
  195. }
  196. /// <summary>
  197. /// 单点模式开关
  198. /// </summary>
  199. public void SetSinglePoint(bool value) => _screenLocate.SetSinglePoint(value);
  200. /// <summary>
  201. /// 是否为单点模式
  202. /// </summary>
  203. public bool IsSinglePoint() => _screenLocate.IsSinglePoint();
  204. /// <summary>
  205. /// 设置抖动过滤值
  206. /// </summary>
  207. public void SetShakeFilterValue(float value)
  208. {
  209. _screenLocate.filterDis = value;
  210. }
  211. /// <summary>
  212. /// 获取抖动过滤值
  213. /// </summary>
  214. public float GetShakeFilterValue()
  215. {
  216. return _screenLocate.filterDis;
  217. }
  218. /// <summary>
  219. /// 设置亮度过滤阈值
  220. /// </summary>
  221. public void SetInfraredLocateBrightnessThreshold(float value)
  222. {
  223. _screenLocate.SetInfraredLocateBrightnessThreshold(value);
  224. }
  225. /// <summary>
  226. /// 是否已经定位
  227. /// </summary>
  228. public bool IsScreenLoateOK()
  229. {
  230. return _screenLocate.IsScreenLoateOK();
  231. }
  232. /// <summary>
  233. /// 是否正在手动定位
  234. /// </summary>
  235. public bool IsScreenLocateManualDoing()
  236. {
  237. return _screenLocate.IsScreenLocateManualDoing();
  238. }
  239. /// <summary>
  240. /// 进入手动定位
  241. /// </summary>
  242. /// <returns>相机画面截图</returns>
  243. public Texture2D EnterScreenLocateManual()
  244. {
  245. return _screenLocate.EnterScreenLocateManual();
  246. }
  247. /// <summary>
  248. /// 退出/完成手动定位
  249. /// </summary>
  250. /// <param name="points">定位点(左下、右下、右上、左上)</param>
  251. public void QuitScreenLocateManual(List<Vector2> points)
  252. {
  253. _screenLocate.QuitScreenLocateManual(points);
  254. }
  255. /// <summary>
  256. /// 初始化中心点偏移量,用于有记录数据时候初始化
  257. /// </summary>
  258. /// <param name="inputPoint"></param>
  259. /// <param name="type"> CameraLocation , ScreenUV</param>
  260. /// <returns></returns>
  261. public Vector2 SetCenterOffset(Vector2 inputPointOffset, string type = "CameraLocation")
  262. {
  263. // 获取偏移量
  264. return _screenLocate.SetPointsOffset(inputPointOffset, type);
  265. }
  266. /// <summary>
  267. /// 获取中心点偏移量,双点情况下不会自动存储。需要再调用 SetCenterOffsets
  268. /// </summary>
  269. /// <param name="inputPoint"></param>
  270. /// <param name="type"> CameraLocation , ScreenUV</param>
  271. /// <returns></returns>
  272. public Vector2 GetCenterOffset(Vector2 inputPoint, string type = "CameraLocation")
  273. {
  274. // 获取偏移量
  275. return _screenLocate.AdjustPointsOffset(inputPoint, type);
  276. }
  277. /// <summary>
  278. /// 重置中心点的偏移量
  279. /// </summary>
  280. public void ResetCenterOffset()
  281. {
  282. _screenLocate.ResetPointsOffest();
  283. }
  284. /// <summary>
  285. /// 撤销回退中心点的偏移量操作。
  286. /// </summary>
  287. public void RevokeCenterOffset()
  288. {
  289. _screenLocate.RevokePointsOffest();
  290. }
  291. /// <summary>
  292. /// 获取中心点偏移量,双点情况下不会自动存储。需要再调用 SetCenterOffsets
  293. /// 左右屏时候调用
  294. /// </summary>
  295. /// <param name="inputPoint"></param>
  296. /// <param name="type"> CameraLocation , ScreenUV</param>
  297. /// <returns></returns>
  298. public Vector2 GetCenterOffset(Vector2 inputPoint, bool isLeft, string type = "CameraLocation")
  299. {
  300. // 获取偏移量
  301. return _screenLocate.AdjustPointsOffset(inputPoint, type, isLeft);
  302. }
  303. /// <summary>
  304. /// 设置偏移量--双点
  305. /// </summary>
  306. /// <param name="playerType">玩家类型</param>
  307. /// <param name="inputPointOffset">偏移点</param>
  308. /// <param name="type">CameraLocation 或者 UV</param>
  309. /// <returns></returns>
  310. public Vector2[] SetCenterOffsets(PlayerType playerType, Vector2 inputPointOffset, string type = "CameraLocation")
  311. {
  312. // 获取偏移量
  313. return _screenLocate.SetPointsOffsets(playerType, inputPointOffset, type);
  314. }
  315. /// <summary>
  316. /// 重置偏移点--双点
  317. /// </summary>
  318. public void RevokeCenterOffsets()
  319. {
  320. _screenLocate.RevokePointsOffests();
  321. }
  322. /// <summary>
  323. /// 重置中心点的偏移量--双点
  324. /// </summary>
  325. public void ResetCenterOffsets()
  326. {
  327. _screenLocate.ResetPointsOffests();
  328. }
  329. /// <summary>
  330. /// 清除定位数据
  331. /// </summary>
  332. public void ClearInfraredPositioningData(bool bSyncLocal = false)
  333. {
  334. //重置识别点
  335. ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
  336. //清除一下记录的点
  337. ScreenLocate.quadUnityVectorList.Clear();
  338. //是否需要保存清除后的数据
  339. if (bSyncLocal) ScreenLocate.SaveScreenLocateVectorList();
  340. }
  341. /// <summary>
  342. /// 初始化一次本地的记录点
  343. /// </summary>
  344. public void InitScreenLocateManual()
  345. {
  346. //获取本地记录的点
  347. if (ScreenLocate.GetScreenLocateVectorList())
  348. {
  349. Debug.Log("[初始化本地坐标]:" + _screenLocate.PrintVector2List(ScreenLocate.quadUnityVectorList));
  350. //使用算法顺序点来设置屏幕四边形,定位点(左下,右下,左上,右上)
  351. _screenLocate.QuadUnityVectorListToScreenQuad(ScreenLocate.quadUnityVectorList);
  352. // _screenLocate.SyncInfraredDemo();
  353. InvokeOnUVCPosUpdate(ScreenLocate.quadUnityVectorList);
  354. }
  355. }
  356. /// <summary>
  357. /// 进入自动定位
  358. /// </summary>
  359. public void EnterScreenLocateManualAuto()
  360. {
  361. _screenLocate.EnterScreenLocateManualAuto();
  362. }
  363. /// <summary>
  364. /// 修改 Capture ,建议值30
  365. /// </summary>
  366. public void SetCapture(int value)
  367. {
  368. _screenLocate.SetCapture(value);
  369. }
  370. /// <summary>
  371. /// 修改delay,建议值30
  372. /// </summary>
  373. public void SetDelay(int value)
  374. {
  375. _screenLocate.SetDelay(value);
  376. }
  377. #endregion
  378. #region 监听事件
  379. /// <summary>
  380. /// ScreenLocate准备事件
  381. /// 初始化 screenIdentification 和 infraredLocate后回调
  382. /// </summary>
  383. public OnScreenLocateIsReadyEvent OnScreenLocateIsReady;
  384. public delegate void OnScreenLocateIsReadyEvent();
  385. public void InvokeOnScreenLocateIsReady()
  386. {
  387. try
  388. {
  389. OnScreenLocateIsReady?.Invoke();
  390. }
  391. catch (Exception e)
  392. {
  393. Debug.LogError(e);
  394. }
  395. }
  396. /// <summary>
  397. /// UVC 相机准备事件
  398. /// </summary>
  399. public OnUVCIsReadyEvent OnUVCIsReady;
  400. public delegate void OnUVCIsReadyEvent(UVCManager.CameraInfo camera);
  401. public void InvokeOnUVCIsReady(UVCManager.CameraInfo camera)
  402. {
  403. try
  404. {
  405. OnUVCIsReady?.Invoke(camera);
  406. }
  407. catch (Exception e)
  408. {
  409. Debug.LogError(e);
  410. }
  411. }
  412. /// <summary>
  413. /// UVC 相机更新事件
  414. /// </summary>
  415. public OnUVCIsUpdateEvent OnUVCIsUpdate;
  416. public delegate void OnUVCIsUpdateEvent();
  417. public void InvokeOnUVCIsUpdate()
  418. {
  419. try
  420. {
  421. OnUVCIsUpdate?.Invoke();
  422. }
  423. catch (Exception e)
  424. {
  425. Debug.LogError(e);
  426. }
  427. }
  428. /// <summary>
  429. /// UVC 定位点更新事件
  430. /// </summary>
  431. public OnUVCPosUpdateEvent OnUVCPosUpdate;
  432. public delegate void OnUVCPosUpdateEvent(List<Vector2> list);
  433. public void InvokeOnUVCPosUpdate(List<Vector2> list)
  434. {
  435. try
  436. {
  437. OnUVCPosUpdate?.Invoke(list);
  438. }
  439. catch (Exception e)
  440. {
  441. Debug.LogError(e);
  442. }
  443. }
  444. /// <summary>
  445. /// 单点更新事件
  446. /// </summary>
  447. public OnPositionUpdateEvent OnPositionUpdate;
  448. public delegate void OnPositionUpdateEvent(Vector2 position, Vector2 cameraLocation);
  449. public void InvokeOnPositionUpdate(Vector2 position, Vector2 cameraLocation)
  450. {
  451. try
  452. {
  453. OnPositionUpdate?.Invoke(position, cameraLocation);
  454. }
  455. catch (Exception e)
  456. {
  457. Debug.LogError(e);
  458. }
  459. }
  460. /// <summary>
  461. /// 双点更新事件
  462. /// </summary>
  463. public OnPositionUpdate2Event OnPositionUpdate2;
  464. public delegate void OnPositionUpdate2Event(Vector2[] positions, Vector2[] cameraLocation);
  465. public void InvokeOnPositionUpdate2(Vector2[] positions, Vector2[] cameraLocation)
  466. {
  467. try
  468. {
  469. OnPositionUpdate2?.Invoke(positions, cameraLocation);
  470. }
  471. catch (Exception e)
  472. {
  473. Debug.LogError(e);
  474. }
  475. }
  476. #endregion
  477. }
  478. }