InfraredScreenPositioningView.cs 39 KB

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