InfraredScreenPositioningView.cs 36 KB

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