InfraredScreenPositioningView.cs 32 KB

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