InfraredScreenPositioningView.cs 32 KB

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