InfraredScreenPositioningView.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LineUI;
  5. using UnityEngine.UI;
  6. using System.Linq;
  7. using ZIM;
  8. using o0;
  9. using Color = UnityEngine.Color;
  10. using InfraredManager;
  11. using Org.BouncyCastle.Asn1.Crmf;
  12. using ZIM.Unity;
  13. using o0.Project;
  14. public class LinePosition
  15. {
  16. public int index;
  17. public List<Vector3> pos;
  18. }
  19. public class InfraredScreenPositioningView : JCUnityLib.ViewBase
  20. {
  21. [SerializeField]
  22. RectTransform canvasRectTransform;
  23. [SerializeField]
  24. RectTransform draggableParent;
  25. [SerializeField]
  26. GameObject mask;
  27. [SerializeField]
  28. GameObject cameraLight;
  29. [SerializeField]
  30. RectTransform pointsParent;
  31. [SerializeField]
  32. RectTransform pos1;
  33. [SerializeField]
  34. RectTransform pos2;
  35. [SerializeField]
  36. RectTransform pos3;
  37. [SerializeField]
  38. RectTransform pos4;
  39. //画线时候的点偏移量
  40. float offset = 10;
  41. [SerializeField]
  42. Line line;
  43. List<LinePosition> oldLinePosition;
  44. Vector3 beginPos;
  45. Vector3 endPos;
  46. //相机感光部分
  47. [SerializeField]
  48. Slider slider;
  49. [SerializeField]
  50. RawImage rawImage;
  51. [SerializeField]
  52. RectTransform crosshair;
  53. [SerializeField]
  54. GameObject textTip1;
  55. [SerializeField]
  56. GameObject textTip2;
  57. [SerializeField]
  58. Button btnAuto;
  59. [SerializeField]
  60. Button btnHandMovement;
  61. [SerializeField]
  62. BtnRecordInfrared btnRecordInfrared;
  63. bool bAuto = true;
  64. [SerializeField] Color normalColor = Color.white;
  65. [SerializeField] Color highlightedColor = Color.green;
  66. [SerializeField] Color normalTextColor = Color.black;
  67. [SerializeField] Color highlightedTextColor = Color.white;
  68. [SerializeField] TextAutoLanguage2 markerTextAutoLanguage2;
  69. [SerializeField]
  70. GameObject LayoutStart;
  71. [SerializeField]
  72. GameObject LayoutMarker;
  73. [SerializeField]
  74. GameObject LayoutSuccessful;
  75. [Tooltip("选择框的Line")]
  76. [SerializeField]
  77. Sprite[] ResultLines;// 0: 选中状态, 1: 未选中状态
  78. [Tooltip("选择框图片")]
  79. [SerializeField]
  80. Image[] ResultImages;
  81. [Tooltip("选择框按钮")]
  82. [SerializeField]
  83. Button[] ResultImagesButtons;
  84. [SerializeField]
  85. Button[] ResultButtons; // 结果选择按钮
  86. [SerializeField]
  87. RawImage CameraFirstImage; //第一个结果屏幕
  88. [SerializeField]
  89. RawImage CameraSecondImage; //第二个结果屏幕
  90. [SerializeField] LineGenerator FirstUILineGenerator;//第一个结果屏幕线条
  91. [SerializeField] LineGenerator SecondUILineGenerator;//第二个结果屏幕线条
  92. bool doLocateAuto;
  93. int DefaultResolutionIndex;
  94. private void Awake()
  95. {
  96. offset = line.MyThickness;
  97. }
  98. void Start()
  99. {
  100. doLocateAuto = false;
  101. textTip1.SetActive(true);
  102. //设置btnAuto 高亮
  103. OnButtonClick(btnAuto);
  104. textTip2.SetActive(false);
  105. //动态设置marker的提示语
  106. if (BluetoothAim.ins!=null && markerTextAutoLanguage2 != null)
  107. {
  108. string name = "";
  109. if (BluetoothAim.ins.isMainConnectToHOUYIPRO())
  110. {
  111. name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-HOUYIPro");
  112. }
  113. else if (BluetoothAim.ins.isMainConnectToARTEMISPRO())
  114. {
  115. name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-ArtemisPro");
  116. }
  117. else if (BluetoothAim.ins.isMainConnectToGun())
  118. {
  119. name = TextAutoLanguage2.GetTextByKey("TitleTipMarker-M9");
  120. }
  121. markerTextAutoLanguage2.textFormatArgs = new object[] { name };
  122. }
  123. //手动不高亮
  124. //ResetButton(btnHandMovement);
  125. //quadUnityVectorList.Clear();
  126. //quadUnityVectorList.Add(new Vector2(16.39f, 35.91f));
  127. //quadUnityVectorList.Add(new Vector2(233.35f, 51.08f));
  128. //quadUnityVectorList.Add(new Vector2(94.15f, 219.11f));
  129. //quadUnityVectorList.Add(new Vector2(243.12f, 237.40f));
  130. Debug.Log("quadUnityVectorList count:" + ScreenLocate.quadUnityVectorList.Count);
  131. if (ScreenLocate.quadUnityVectorList.Count == 0)
  132. {
  133. Debug.Log("GetLocalPos");
  134. GetLocalPos();
  135. }
  136. else
  137. {
  138. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  139. Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
  140. //自动识别时候四个点
  141. SyncQuadUnityVectorListToPos();
  142. //pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  143. //pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  144. //pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  145. //pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  146. }
  147. //记录操作的位置信息
  148. oldLinePosition = new List<LinePosition>();
  149. SetLinePos();
  150. //相机感光度
  151. if (InfraredDemo._ins)
  152. {
  153. //重置偏移量
  154. InfraredDemo._ins.ResetCenterOffset();
  155. //重置识别点
  156. ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
  157. //清除一下记录的点
  158. ScreenLocate.quadUnityVectorList.Clear();
  159. slider.onValueChanged.AddListener((value) =>
  160. {
  161. InfraredDemo._ins.onSliderEvent(value);
  162. });
  163. InfraredDemo._ins.onSetSliderValue(slider);
  164. offset = line.MyThickness = InfraredDemo._ins.lineWidth.Get();
  165. }
  166. else {
  167. //编辑器使用
  168. if (Application.isEditor || Application.platform == RuntimePlatform.WindowsPlayer) {
  169. slider.onValueChanged.AddListener((value) =>
  170. {
  171. double originalMin = 0.0;
  172. double originalMax = 10.0;
  173. double targetMin = -1;
  174. double targetMax = 1;
  175. // 计算转换后的值
  176. double result = InfraredDemo.MapValue(value, originalMin, originalMax, targetMin, targetMax);
  177. //int _current = (int)(result);
  178. Debug.Log("_current:" + value + " , result:" + result);
  179. ScreenLocate.Main.pcContrast = (float)result;
  180. });
  181. float v = ScreenLocate.Main.pcContrast;
  182. // 目标区间 [0, 10] 的边界值
  183. double targetMin = 0.0;
  184. double targetMax = 10.0;
  185. double originalMin = -1;
  186. double originalMax = 1;
  187. // 计算转换后的值
  188. double v2 = InfraredDemo.MapValue(v, originalMin, originalMax, targetMin, targetMax);
  189. Debug.Log("PC获取相机的感光度:" + v + " = " + v2);
  190. slider.SetValueWithoutNotify((float)v2);
  191. }
  192. }
  193. initSelectInfo();
  194. //if (ScreenLocate.Main.getUVCTexture)
  195. //{
  196. // //渲染相机画面
  197. // rawImage.texture = ScreenLocate.Main.getUVCTexture;
  198. // //Debug.Log("rawImage.texture:" + rawImage.texture.name);
  199. //}
  200. //slider.onValueChanged.AddListener((value) => {
  201. // //onSliderEvent(value);
  202. // InfraredDemo._ins.SetBrightness(value);
  203. //});
  204. //slider.value = InfraredDemo._ins.brightness.Get();
  205. //修改分辨率。是否清晰一点?
  206. //DefaultResolutionIndex = InfraredDemo._ins?.ResolutionIndex ?? 0;
  207. //Debug.Log("[InfraredScreenPositioningView]开始记录进入时候的分辨率:" + DefaultResolutionIndex);
  208. //InfraredDemo._ins?.SetResolutionNew(ScreenLocate.Main.HighScreenLocateResolutionIndex);
  209. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
  210. {
  211. SB_EventSystem.ins.AwakenSimulateMouse();
  212. // Debug.Log("simulate-mouse-close");
  213. }
  214. }
  215. private void OnDestroy()
  216. {
  217. //修改回进入手动调节页面时候的分辨率
  218. //InfraredDemo._ins?.SetResolutionNew(DefaultResolutionIndex);
  219. if (SB_EventSystem.ins && !SB_EventSystem.ins.simulateMouseIsAwaked)
  220. {
  221. SB_EventSystem.ins.AwakenSimulateMouse();
  222. }
  223. }
  224. public RawImage Bg => rawImage;
  225. void Update()
  226. {
  227. if (enterFromZimWebCamera && ScreenLocate.Main.DebugOnZIMDemo) {
  228. // ZimWebCamera场景测试
  229. rawImage.texture = ScreenLocate.Main.outputRawImages[7].texture;
  230. if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
  231. {
  232. // 检测到光点
  233. var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
  234. crosshair.gameObject.SetActive(true);
  235. crosshair.anchoredPosition = posInCanvas;
  236. }
  237. else
  238. crosshair.gameObject.SetActive(false);
  239. //渲染相机画面
  240. CameraSecondImage.texture = rawImage.texture;
  241. CameraFirstImage.texture = rawImage.texture;
  242. }
  243. else if (InfraredDemo.running)
  244. {
  245. //渲染相机画面
  246. Texture texture = InfraredDemo.infraredCameraHelper.GetCameraTexture();
  247. if (rawImage.texture == null || texture.GetNativeTexturePtr() != rawImage.texture.GetNativeTexturePtr())
  248. {
  249. rawImage.texture = texture;
  250. //渲染相机画面
  251. CameraSecondImage.texture = rawImage.texture;
  252. CameraFirstImage.texture = rawImage.texture;
  253. }
  254. //rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
  255. }
  256. }
  257. public void OnClick_Back()
  258. {
  259. AudioMgr.ins.PlayBtn();
  260. ViewMgr.Instance.DestroyView<InfraredScreenPositioningView>();
  261. }
  262. /// <summary>
  263. /// 进入屏幕标记
  264. /// </summary>
  265. public void OnClick_EnterMarker() {
  266. LayoutStart.SetActive(false);
  267. LayoutMarker.SetActive(true);
  268. }
  269. /// <summary>
  270. /// 自动识别
  271. /// </summary>
  272. public void OnClick_Auto()
  273. {
  274. bAuto = true;
  275. doLocateAuto = true;
  276. textTip1.SetActive(false);
  277. textTip2.SetActive(true);
  278. ResetButton(btnAuto);
  279. if (enterFromZimWebCamera)
  280. {
  281. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  282. _screenLocate.EnterScreenLocateManualAuto();
  283. return;
  284. }
  285. if (InfraredDemo.running)
  286. {
  287. InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
  288. }
  289. }
  290. /// <summary>
  291. /// 切换成手动方式
  292. /// </summary>
  293. public void onHandMovement()
  294. {
  295. bAuto = false;
  296. doLocateAuto = false;
  297. draggableParent.gameObject.SetActive(true);
  298. pointsParent.gameObject.SetActive(false);
  299. mask.SetActive(false);
  300. cameraLight.SetActive(true);
  301. }
  302. /// <summary>
  303. /// 自动识别后同步
  304. /// </summary>
  305. public void SyncScreenPosition()
  306. {
  307. //Debug.Log("quadUnityVectorList[i]:" + quadUnityVectorList[i]);
  308. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  309. Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
  310. //自动识别时候四个点
  311. //pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  312. //pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  313. //pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  314. //pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  315. SyncQuadUnityVectorListToPos();
  316. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  317. //自动识别后隐藏手动按钮
  318. draggableParent.gameObject.SetActive(false);
  319. pointsParent.gameObject.SetActive(true);
  320. if (ScreenLocate.Main) {
  321. ZIM.Unity.QuadrilateralInCamera screen = ScreenLocate.Main.ScreenIdentification.Screen.QuadInCamera;
  322. //设置points点
  323. for (int i = 0; i < 4; i++)
  324. {
  325. RectTransform t = pointsParent.GetChild(i) as RectTransform;
  326. t.anchoredPosition = screen.Quad[i].pixelToLocalPosition_AnchorCenter(screen.CameraSize, pointsParent.rect);
  327. }
  328. }
  329. //显示最后结果
  330. LayoutMarker.SetActive(false);
  331. LayoutSuccessful.SetActive(true);
  332. //设置两个线段
  333. QuadrilateralInCamera screenAuto = ScreenLocate.Main.ScreenIdentification.QuadAuto;
  334. QuadrilateralInCamera screenSemiAuto = ScreenLocate.Main.ScreenIdentification.QuadSemiAuto;
  335. var lo = new Vector2(-0.5f, -0.5f);
  336. if (screenAuto != null)
  337. {
  338. FirstUILineGenerator.Points = new Vector2[4] {
  339. 2 * (new Vector2(screenAuto.Quad[0].x/texSize.x,screenAuto.Quad[0].y/texSize.y) + lo),
  340. 2 * (new Vector2(screenAuto.Quad[1].x/texSize.x,screenAuto.Quad[1].y/texSize.y) + lo),
  341. 2 * (new Vector2(screenAuto.Quad[3].x/texSize.x,screenAuto.Quad[3].y/texSize.y) + lo),
  342. 2 * (new Vector2(screenAuto.Quad[2].x/texSize.x,screenAuto.Quad[2].y/texSize.y) + lo) };
  343. // 打印 Points 的值,合并为一个字符串
  344. string pointsOutput = "1 Points: ";
  345. for (int i = 0; i < FirstUILineGenerator.Points.Length; i++)
  346. {
  347. pointsOutput += "[" + FirstUILineGenerator.Points[i].x + ", " + FirstUILineGenerator.Points[i].y + "]";
  348. if (i < FirstUILineGenerator.Points.Length - 1)
  349. {
  350. pointsOutput += ", "; // 添加逗号分隔符,最后一个元素后不加
  351. }
  352. }
  353. Debug.Log(pointsOutput);
  354. // 打印 Quad 的值,合并为一个字符串
  355. int[] customOrder = new int[] { 0, 1, 3, 2 };
  356. string quadOutput = "1 Quad Points: ";
  357. for (int i = 0; i < customOrder.Length; i++)
  358. {
  359. quadOutput += "[" + screenAuto.Quad[customOrder[i]].x + ", " + screenAuto.Quad[customOrder[i]].y + "]";
  360. if (i < customOrder.Length - 1)
  361. {
  362. quadOutput += ", "; // 添加逗号分隔符,最后一个元素后不加
  363. }
  364. }
  365. Debug.Log(quadOutput);
  366. }
  367. else {
  368. Debug.LogError("screenAuto 不存在!");
  369. }
  370. if (screenSemiAuto != null)
  371. {
  372. SecondUILineGenerator.Points = new Vector2[4] {
  373. 2 * (new Vector2(screenSemiAuto.Quad[0].x/texSize.x,screenSemiAuto.Quad[0].y/texSize.y) + lo),
  374. 2 * (new Vector2(screenSemiAuto.Quad[1].x/texSize.x,screenSemiAuto.Quad[1].y/texSize.y) + lo),
  375. 2 * (new Vector2(screenSemiAuto.Quad[3].x/texSize.x,screenSemiAuto.Quad[3].y/texSize.y) + lo),
  376. 2 * (new Vector2(screenSemiAuto.Quad[2].x/texSize.x,screenSemiAuto.Quad[2].y/texSize.y) + lo) };
  377. // 打印 Points 的值,合并为一个字符串
  378. string pointsOutput = "2 Points: ";
  379. for (int i = 0; i < SecondUILineGenerator.Points.Length; i++)
  380. {
  381. pointsOutput += "[" + SecondUILineGenerator.Points[i].x + ", " + SecondUILineGenerator.Points[i].y + "]";
  382. if (i < SecondUILineGenerator.Points.Length - 1)
  383. {
  384. pointsOutput += ", "; // 添加逗号分隔符,最后一个元素后不加
  385. }
  386. }
  387. Debug.Log(pointsOutput);
  388. // 打印 Quad 的值,合并为一个字符串
  389. int[] customOrder = new int[] { 0, 1, 3, 2 };
  390. string quadOutput = "2 Quad Points: ";
  391. for (int i = 0; i < customOrder.Length; i++)
  392. {
  393. quadOutput += "[" + screenSemiAuto.Quad[customOrder[i]].x + ", " + screenSemiAuto.Quad[customOrder[i]].y + "]";
  394. if (i < customOrder.Length - 1)
  395. {
  396. quadOutput += ", "; // 添加逗号分隔符,最后一个元素后不加
  397. }
  398. }
  399. Debug.Log(quadOutput);
  400. }
  401. else {
  402. Debug.LogError("screenSemiAuto 不存在!");
  403. }
  404. }
  405. #region 绘制线段部分
  406. //点击拖拽的开始位置
  407. public void onBeginPos(int index, Vector3 pos)
  408. {
  409. Debug.Log("pos begin: " + pos);
  410. beginPos = pos;
  411. }
  412. public void onDragPos(int index, Vector3 pos)
  413. {
  414. //设置线段的点
  415. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  416. }
  417. //点击拖拽的结束位置
  418. public void onEndPos(int index, Vector3 pos)
  419. {
  420. Debug.Log("pos end: " + pos);
  421. endPos = pos;
  422. if (beginPos == endPos) return;
  423. //Debug.Log(index+",最后的点:" + pos);
  424. //再记录一次最后的点
  425. SetLinePos();
  426. }
  427. //同步设置图片对应的位置到line
  428. public void SetLinePos()
  429. {
  430. //记录一个操作点的操作位置
  431. AddOldLinePosition();
  432. //设置线段的点
  433. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  434. }
  435. void AddOldLinePosition()
  436. {
  437. Vector3[] v = new Vector3[4];
  438. pos1.GetWorldCorners(v);
  439. Vector3[] v1 = new Vector3[4];
  440. pos1.GetLocalCorners(v1);
  441. //for (int i = 0; i < 4; i++)
  442. //{
  443. // Debug.Log(i + " =1= " + v[i]);
  444. // Debug.Log(i + " =2= " + v1[i]);
  445. //}
  446. //记录一个操作点的操作位置
  447. List<Vector3> screenPositions = new List<Vector3>();
  448. screenPositions.Add(pos1.localPosition);
  449. screenPositions.Add(pos2.localPosition);
  450. screenPositions.Add(pos3.localPosition);
  451. screenPositions.Add(pos4.localPosition);
  452. LinePosition linePosition = new LinePosition();
  453. linePosition.index = oldLinePosition.Count;
  454. linePosition.pos = screenPositions;
  455. oldLinePosition.Add(linePosition);
  456. }
  457. //转换绘制线段的点
  458. List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
  459. {
  460. List<Vector2> _screenPositions = new List<Vector2>();
  461. //点为负数的增大 offset,正数减少 offset
  462. //Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.5f + offset, _pos1.y - pos1.rect.height * 0.5f + offset);
  463. //Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.5f - offset, _pos2.y - pos2.rect.height * 0.5f + offset);
  464. //Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.5f - offset, _pos3.y + pos3.rect.height * 0.5f - offset);
  465. //Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.5f + offset, _pos4.y + pos4.rect.height * 0.5f - offset);
  466. Vector2 startPos1 = new Vector2(_pos1.x + offset, _pos1.y + offset);
  467. Vector2 startPos2 = new Vector2(_pos2.x - offset, _pos2.y + offset);
  468. Vector2 startPos3 = new Vector2(_pos3.x - offset, _pos3.y - offset);
  469. Vector2 startPos4 = new Vector2(_pos4.x + offset, _pos4.y - offset);
  470. _screenPositions.Add(startPos1);
  471. _screenPositions.Add(startPos2);
  472. _screenPositions.Add(startPos3);
  473. _screenPositions.Add(startPos4);
  474. return _screenPositions;
  475. }
  476. void SetRectanglePoints(List<Vector2> screenPositions)
  477. {
  478. line.SetLine(screenPositions);
  479. }
  480. //撤回上一个元素
  481. public void onRecall()
  482. {
  483. // 获取并删除最后一个元素,并且保留一个元素
  484. if (oldLinePosition.Count > 1) // 确保列表不为空
  485. {
  486. // 获取回退的那个元素点
  487. LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
  488. // 获取最后一个元素
  489. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  490. //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
  491. oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
  492. pos1.localPosition = lastElement_second.pos[0];
  493. pos2.localPosition = lastElement_second.pos[1];
  494. pos3.localPosition = lastElement_second.pos[2];
  495. pos4.localPosition = lastElement_second.pos[3];
  496. //设置线段的点
  497. SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3]));
  498. }
  499. }
  500. //是不是从Demo界面进入该页面的
  501. public bool enterFromInfraredDemo { get; set; } = false;
  502. //是否从测试场景进入
  503. public bool enterFromZimWebCamera { get; set; } = false;
  504. //确认修改
  505. public void onConfirmation()
  506. {
  507. if (enterFromZimWebCamera)
  508. {
  509. ConfirmScreenLocateManualTest();
  510. if (oldLinePosition.Count > 1) // 确保列表不为空
  511. {
  512. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  513. oldLinePosition.Clear();
  514. oldLinePosition.Add(lastElement);
  515. }
  516. SaveLocalPos();
  517. AudioMgr.ins.PlayBtn();
  518. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  519. return;
  520. }
  521. if (!ConfirmScreenLocateManual()) return;
  522. if (oldLinePosition.Count > 1) // 确保列表不为空
  523. {
  524. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  525. oldLinePosition.Clear();
  526. oldLinePosition.Add(lastElement);
  527. }
  528. if (InfraredDemo.running)
  529. {
  530. //跳转入界面
  531. AudioMgr.ins.PlayBtn();
  532. if (!enterFromInfraredDemo)
  533. {
  534. //每次初始化重置一下引导
  535. InfraredDemo._ins.resetInfraredPlayerPrefs();
  536. //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  537. //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  538. if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
  539. {
  540. //如果是红外连接成功,记录一个tag
  541. PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
  542. }
  543. onEnterInfrared();
  544. }
  545. else {
  546. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  547. }
  548. }
  549. //存储一次节点
  550. SaveLocalPos();
  551. }
  552. //重置位置
  553. public void onReset()
  554. {
  555. oldLinePosition.Clear();
  556. // 获取屏幕的四个角的像素坐标
  557. Vector2 bottomLeft = new Vector2(0, 0);
  558. // 将屏幕像素坐标转换为 Canvas 的局部坐标
  559. Vector2 localBottomLeft;
  560. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, bottomLeft, null, out localBottomLeft);
  561. // 打印结果
  562. Debug.Log("Local Bottom Left: " + localBottomLeft);
  563. //int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x) - 120), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y) - 77);
  564. int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x)), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y));
  565. pos1.anchoredPosition = new Vector3(-_x, -_y, 0); //Bottom Left
  566. pos2.anchoredPosition = new Vector3(_x, -_y, 0); //Bottom Right
  567. pos3.anchoredPosition = new Vector3(_x, _y, 0); //Top Right
  568. pos4.anchoredPosition = new Vector3(-_x, _y, 0); //Top Left
  569. //pos1.anchoredPosition = new Vector3(_x, _y, 0);
  570. //pos2.anchoredPosition = new Vector3(-_x, _y, 0);
  571. //pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
  572. //pos4.anchoredPosition = new Vector3(_x, -_y, 0);
  573. //设置一次位置
  574. SetLinePos();
  575. btnRecordInfrared.Reset();
  576. ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
  577. if (enterFromZimWebCamera)
  578. {
  579. ScreenLocate.Main.ScreenQuad.gameObject.SetActive(false);
  580. ScreenLocate.Main.UILineGenerator.Points = new Vector2[0];
  581. }
  582. }
  583. /// <summary>
  584. /// 标准四个点
  585. /// </summary>
  586. /// <param name="index"></param>
  587. /// <param name="pos"></param>
  588. public void onManualNewPos(int index, Vector3 pos)
  589. {
  590. Debug.Log("pos end: " + pos);
  591. if (index == 0)
  592. pos1.localPosition = pos;
  593. else if (index == 1)
  594. pos2.localPosition = pos;
  595. else if (index == 2)
  596. pos3.localPosition = pos;
  597. else if (index == 3)
  598. pos4.localPosition = pos;
  599. //再记录一次最后的点
  600. SetLinePos();
  601. }
  602. /// <summary>
  603. /// 处理新流程,先记录手动,然后到自动识别处理
  604. /// </summary>
  605. public void onManualToAutomatic() {
  606. if (enterFromZimWebCamera)
  607. {
  608. ConfirmScreenLocateManualTest();
  609. if (oldLinePosition.Count > 1) // 确保列表不为空
  610. {
  611. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  612. oldLinePosition.Clear();
  613. oldLinePosition.Add(lastElement);
  614. }
  615. SaveLocalPos();
  616. //到自动
  617. OnClick_Auto();
  618. return;
  619. }
  620. if (!ConfirmScreenLocateManual()) {
  621. Debug.LogError("ConfirmScreenLocateManual is false!");
  622. return;
  623. }
  624. if (oldLinePosition.Count > 1) // 确保列表不为空
  625. {
  626. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  627. oldLinePosition.Clear();
  628. oldLinePosition.Add(lastElement);
  629. }
  630. //存储一次节点
  631. SaveLocalPos();
  632. bAuto = true;
  633. doLocateAuto = true;
  634. //自动校准
  635. InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
  636. }
  637. #endregion
  638. List<Vector2> _locatePointList = new();
  639. float _texWidth;
  640. float _texHeight;
  641. void RecordLocatePoint(RectTransform p, Vector2 pivot)
  642. {
  643. Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
  644. pos.x = Mathf.Clamp01(pos.x / Screen.width);
  645. pos.y = Mathf.Clamp01(pos.y / Screen.height);
  646. _locatePointList.Add(pos);
  647. }
  648. bool ConfirmScreenLocateManual()
  649. {
  650. if (InfraredDemo.running)
  651. {
  652. //渲染截图
  653. Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual();
  654. if (texture2D == null)
  655. {
  656. Debug.Log("EnterScreenLocateManual = null");
  657. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null);
  658. return false;
  659. }
  660. _locatePointList.Clear();
  661. _texWidth = texture2D.width;
  662. _texHeight = texture2D.height;
  663. RecordLocatePoint(pos1, new Vector2(0, 0));
  664. RecordLocatePoint(pos2, new Vector2(1, 0));
  665. RecordLocatePoint(pos3, new Vector2(1, 1));
  666. RecordLocatePoint(pos4, new Vector2(0, 1));
  667. //RecordLocatePoint(pos1, new Vector2(0.5f, 0.5f));
  668. //RecordLocatePoint(pos2, new Vector2(0.5f, 0.5f));
  669. //RecordLocatePoint(pos3, new Vector2(0.5f, 0.5f));
  670. //RecordLocatePoint(pos4, new Vector2(0.5f, 0.5f));
  671. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
  672. FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, 1, 1);
  673. //同步数据
  674. ScreenLocate.quadUnityVectorList.Clear();
  675. ScreenLocate.quadUnityVectorList.Add(_locatePointList[0]);
  676. ScreenLocate.quadUnityVectorList.Add(_locatePointList[1]);
  677. //两个点切换,顺序不一样
  678. ScreenLocate.quadUnityVectorList.Add(_locatePointList[3]);
  679. ScreenLocate.quadUnityVectorList.Add(_locatePointList[2]);
  680. ScreenLocate.SaveScreenLocateVectorList();
  681. return true;
  682. }
  683. return false;
  684. }
  685. void ConfirmScreenLocateManualTest()
  686. {
  687. //渲染截图
  688. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  689. Texture2D texture2D = _screenLocate.EnterScreenLocateManual();
  690. if (texture2D == null)
  691. {
  692. _screenLocate.QuitScreenLocateManual(null);
  693. return;
  694. }
  695. _locatePointList.Clear();
  696. _texWidth = texture2D.width;
  697. _texHeight = texture2D.height;
  698. RecordLocatePoint(pos1, new Vector2(0, 0));
  699. RecordLocatePoint(pos2, new Vector2(1, 0));
  700. RecordLocatePoint(pos3, new Vector2(1, 1));
  701. RecordLocatePoint(pos4, new Vector2(0, 1));
  702. _screenLocate.QuitScreenLocateManual(_locatePointList);
  703. //FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
  704. setPointsLocation(_locatePointList,pointsParent.gameObject,!bAuto);
  705. if (!doLocateAuto) // 设置手动定位数据
  706. setPointsManual(_locatePointList, GameObject.Find("WebCameraView/CameraImage0/ScreenQuad"));
  707. //同步数据
  708. ScreenLocate.quadUnityVectorList.Clear();
  709. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[0].x, _locatePointList[0].y));
  710. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[1].x, _locatePointList[1].y));
  711. //两个点切换,顺序不一样
  712. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[3].x, _locatePointList[3].y));
  713. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[2].x, _locatePointList[2].y));
  714. ScreenLocate.SaveScreenLocateVectorList();
  715. }
  716. void setPointsLocation(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
  717. {
  718. //GameObject pointsTF2 = GameObject.Find("WebCameraView/CameraImage0/ScreenQuad");
  719. pointsTF2.SetActive(active);
  720. if (pointsTF2.transform.childCount == targetList.Count)
  721. {
  722. for (int i = 0; i < pointsTF2.transform.childCount; i++)
  723. {
  724. Transform pointTF = pointsTF2.transform.GetChild(i);
  725. Vector2 pos = targetList[i];
  726. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(Vector2.one, pointsTF2.GetComponent<RectTransform>().rect);
  727. pointTF.gameObject.SetActive(true);
  728. }
  729. }
  730. }
  731. void setPointsManual(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
  732. {
  733. setPointsLocation(targetList, pointsTF2, active);
  734. var lo = new Vector2(-0.5f, -0.5f);
  735. ScreenLocate.Main.UILineGenerator.Points = new Vector2[4] {
  736. 2 * (targetList[0] + lo),
  737. 2 * (targetList[1] + lo),
  738. 2 * (targetList[2] + lo),
  739. 2 * (targetList[3] + lo) };
  740. // 记录手动数据
  741. //var size = ScreenLocate.Main.getUVCCameraInfoSize;
  742. //var quad = new QuadrilateralInCamera(
  743. // new o0.Geometry2D.Float.Vector(targetList[0].x * size.x, targetList[0].y * size.y),
  744. // new o0.Geometry2D.Float.Vector(targetList[1].x * size.x, targetList[1].y * size.y),
  745. // new o0.Geometry2D.Float.Vector(targetList[3].x * size.x, targetList[3].y * size.y),
  746. // new o0.Geometry2D.Float.Vector(targetList[2].x * size.x, targetList[2].y * size.y),
  747. // size.o0Vector()
  748. // );
  749. //ScreenLocate.Main.ScreenIdentification.QuadManual = quad;
  750. //Debug.Log("<color=aqua>[ScreenLocate] 记录手动数据</color>");
  751. }
  752. void SaveLocalPos()
  753. {
  754. List<Vector3> screenPositions = new List<Vector3>();
  755. screenPositions.Add(pos1.anchoredPosition);
  756. screenPositions.Add(pos2.anchoredPosition);
  757. screenPositions.Add(pos3.anchoredPosition);
  758. screenPositions.Add(pos4.anchoredPosition);
  759. string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y}")); //,{v.z}
  760. Debug.Log("Local UI Position: " + saveStr);
  761. PlayerPrefs.SetString("ScreenPositioningView", saveStr);
  762. }
  763. void GetLocalPos()
  764. {
  765. string posListStr = PlayerPrefs.GetString("ScreenPositioningView", "");
  766. if (!string.IsNullOrWhiteSpace(posListStr))
  767. {
  768. List<Vector2> posList = posListStr.Split(';')
  769. .Select(s =>
  770. {
  771. string[] parts = s.Split(',');
  772. return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
  773. })
  774. .ToList();
  775. pos1.anchoredPosition = posList[0];
  776. pos2.anchoredPosition = posList[1];
  777. pos3.anchoredPosition = posList[2];
  778. pos4.anchoredPosition = posList[3];
  779. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  780. }
  781. }
  782. #region 按钮颜色切换
  783. private void OnButtonClick(Button button)
  784. {
  785. // 切换按钮颜色
  786. ColorBlock colors = button.colors;
  787. colors.normalColor = highlightedColor;
  788. colors.highlightedColor = highlightedColor;
  789. button.colors = colors;
  790. // 切换字体颜色
  791. Text buttonText = button.GetComponentInChildren<Text>();
  792. buttonText.color = highlightedTextColor;
  793. }
  794. private void ResetButton(Button button)
  795. {
  796. // 重置按钮颜色
  797. ColorBlock colors = button.colors;
  798. colors.normalColor = normalColor;
  799. colors.highlightedColor = normalColor;
  800. button.colors = colors;
  801. // 重置字体颜色
  802. Text buttonText = button.GetComponentInChildren<Text>();
  803. buttonText.color = normalTextColor;
  804. }
  805. #endregion
  806. #region 标定完成之后进入游戏界面
  807. void onEnterInfrared()
  808. {
  809. //添加进入射箭场景
  810. if (PlayerPrefs.GetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 0) == 0)
  811. {
  812. Debug.Log("-----进入射箭场景!");
  813. NewUserGuiderManager newUserGuiderManager = FindObjectOfType<NewUserGuiderManager>();
  814. newUserGuiderManager.curConfigKey = "开始-红外调整";
  815. newUserGuiderManager.isNewModule = AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.HOUYIPRO || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.Gun || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.ARTEMISPRO;
  816. //进入射箭场景
  817. GlobalData.pkMatchType = PKMatchType.None;
  818. GameMgr.gameType = 1;
  819. //射一箭回到连接页面,Device.view
  820. GameMgr.bNavBack = true;
  821. GameMgr.bShowDistance = false;
  822. AimHandler.ins.bInitOne = false;//true
  823. //关闭计时器
  824. GameMgr.turnOffTimer = true;
  825. //关闭左边靶子和底部速度栏
  826. GameMgr.HideTargetView = true;
  827. GameMgr.HideBillboard = true;
  828. GameMgr.ButtonCount = 0;
  829. UnityEngine.SceneManagement.SceneManager.LoadScene(
  830. "Game", UnityEngine.SceneManagement.LoadSceneMode.Single);
  831. PlayerPrefs.SetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 1);
  832. }
  833. else
  834. {
  835. Debug.Log("-----跳转回连接界面!");
  836. //如不是第一次,则应该跳转回连接界面
  837. ViewMgr.Instance.DestroyView<SmartArcheryView>();
  838. ViewManager2.HideView(ViewManager2.Path_ConnectGuidanceView);
  839. }
  840. }
  841. #endregion
  842. #region 最后选择算法处理UI
  843. void initSelectInfo() {
  844. for (int i = 0; i < 2; i++)
  845. {
  846. int currentIndex = i; // 将 i 赋值给局部变量
  847. ResultImagesButtons[currentIndex].onClick.AddListener(() => SelectImage(currentIndex));
  848. ResultButtons[currentIndex].onClick.AddListener(() =>
  849. {
  850. if (currentIndex == 0)
  851. {
  852. OnFirstResult();
  853. }
  854. else if (currentIndex == 1)
  855. {
  856. OnSecondResult();
  857. }
  858. });
  859. }
  860. //默认选择1 自动
  861. SelectImage(0);
  862. }
  863. // 添加一个选择函数
  864. void SelectImage(int selectedIndex)
  865. {
  866. // 遍历所有图片
  867. for (int i = 0; i < ResultImages.Length; i++)
  868. {
  869. // 如果是选中的 index,赋值为 ResultLines[0],否则赋值为 ResultLines[1]
  870. if (i == selectedIndex)
  871. {
  872. ResultImages[i].sprite = ResultLines[0];
  873. ResultButtons[i].interactable = true;
  874. }
  875. else
  876. {
  877. ResultImages[i].sprite = ResultLines[1];
  878. ResultButtons[i].interactable = false;
  879. }
  880. }
  881. }
  882. //选择全自动结果后进入游戏
  883. void OnFirstResult()
  884. {
  885. ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.Auto);
  886. UpdateQuadUnityVectorList();
  887. onCompelete();
  888. }
  889. //选择半自动结果
  890. void OnSecondResult()
  891. {
  892. ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.SemiAuto);
  893. UpdateQuadUnityVectorList();
  894. onCompelete();
  895. }
  896. public void onCompelete()
  897. {
  898. if (enterFromZimWebCamera)
  899. {
  900. SaveLocalPos();
  901. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  902. return;
  903. }
  904. if (InfraredDemo.running)
  905. {
  906. //跳转入界面
  907. AudioMgr.ins.PlayBtn();
  908. if (!enterFromInfraredDemo)
  909. {
  910. //每次初始化重置一下引导
  911. InfraredDemo._ins.resetInfraredPlayerPrefs();
  912. //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  913. //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  914. if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
  915. {
  916. //如果是红外连接成功,记录一个tag
  917. PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
  918. }
  919. onEnterInfrared();
  920. }
  921. else
  922. {
  923. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  924. }
  925. }
  926. //存储一次节点
  927. SaveLocalPos();
  928. }
  929. /// <summary>
  930. /// 选择模式后更新 quadUnityVectorList
  931. /// </summary>
  932. void UpdateQuadUnityVectorList() {
  933. ScreenLocate.Main.UpdateQuadUnityVectorList();
  934. SyncQuadUnityVectorListToPos();
  935. InfraredDemo._ins?.SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList, 1, 1);
  936. _locatePointList.Clear();
  937. }
  938. void SyncQuadUnityVectorListToPos() {
  939. Debug.Log("[ScreenLocate] SyncQuadUnityVectorListToPos quadUnityVectorList:" + ScreenLocate.Main.PrintVector2List(ScreenLocate.quadUnityVectorList));
  940. pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  941. pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  942. pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  943. pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  944. }
  945. #endregion
  946. }