InfraredScreenPositioningView.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. //设置points点
  263. for (int i = 0; i < 4; i++)
  264. {
  265. RectTransform t = pointsParent.GetChild(i) as RectTransform;
  266. t.anchoredPosition = screen.Quad[i].pixelToLocalPosition_AnchorCenter(screen.CameraSize, pointsParent.rect);
  267. }
  268. }
  269. }
  270. #region 绘制线段部分
  271. //点击拖拽的开始位置
  272. public void onBeginPos(int index, Vector3 pos)
  273. {
  274. Debug.Log("pos begin: " + pos);
  275. beginPos = pos;
  276. }
  277. public void onDragPos(int index, Vector3 pos)
  278. {
  279. //设置线段的点
  280. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  281. }
  282. //点击拖拽的结束位置
  283. public void onEndPos(int index, Vector3 pos)
  284. {
  285. Debug.Log("pos end: " + pos);
  286. endPos = pos;
  287. if (beginPos == endPos) return;
  288. //Debug.Log(index+",最后的点:" + pos);
  289. //再记录一次最后的点
  290. SetLinePos();
  291. }
  292. //同步设置图片对应的位置到line
  293. public void SetLinePos()
  294. {
  295. //记录一个操作点的操作位置
  296. AddOldLinePosition();
  297. //设置线段的点
  298. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  299. }
  300. void AddOldLinePosition()
  301. {
  302. Vector3[] v = new Vector3[4];
  303. pos1.GetWorldCorners(v);
  304. Vector3[] v1 = new Vector3[4];
  305. pos1.GetLocalCorners(v1);
  306. //for (int i = 0; i < 4; i++)
  307. //{
  308. // Debug.Log(i + " =1= " + v[i]);
  309. // Debug.Log(i + " =2= " + v1[i]);
  310. //}
  311. //记录一个操作点的操作位置
  312. List<Vector3> screenPositions = new List<Vector3>();
  313. screenPositions.Add(pos1.localPosition);
  314. screenPositions.Add(pos2.localPosition);
  315. screenPositions.Add(pos3.localPosition);
  316. screenPositions.Add(pos4.localPosition);
  317. LinePosition linePosition = new LinePosition();
  318. linePosition.index = oldLinePosition.Count;
  319. linePosition.pos = screenPositions;
  320. oldLinePosition.Add(linePosition);
  321. }
  322. //转换绘制线段的点
  323. List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
  324. {
  325. List<Vector2> _screenPositions = new List<Vector2>();
  326. //点为负数的增大 offset,正数减少 offset
  327. //Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.5f + offset, _pos1.y - pos1.rect.height * 0.5f + offset);
  328. //Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.5f - offset, _pos2.y - pos2.rect.height * 0.5f + offset);
  329. //Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.5f - offset, _pos3.y + pos3.rect.height * 0.5f - offset);
  330. //Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.5f + offset, _pos4.y + pos4.rect.height * 0.5f - offset);
  331. Vector2 startPos1 = new Vector2(_pos1.x + offset, _pos1.y + offset);
  332. Vector2 startPos2 = new Vector2(_pos2.x - offset, _pos2.y + offset);
  333. Vector2 startPos3 = new Vector2(_pos3.x - offset, _pos3.y - offset);
  334. Vector2 startPos4 = new Vector2(_pos4.x + offset, _pos4.y - offset);
  335. _screenPositions.Add(startPos1);
  336. _screenPositions.Add(startPos2);
  337. _screenPositions.Add(startPos3);
  338. _screenPositions.Add(startPos4);
  339. return _screenPositions;
  340. }
  341. void SetRectanglePoints(List<Vector2> screenPositions)
  342. {
  343. line.SetLine(screenPositions);
  344. }
  345. //撤回上一个元素
  346. public void onRecall()
  347. {
  348. // 获取并删除最后一个元素,并且保留一个元素
  349. if (oldLinePosition.Count > 1) // 确保列表不为空
  350. {
  351. // 获取回退的那个元素点
  352. LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
  353. // 获取最后一个元素
  354. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  355. //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
  356. oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
  357. pos1.localPosition = lastElement_second.pos[0];
  358. pos2.localPosition = lastElement_second.pos[1];
  359. pos3.localPosition = lastElement_second.pos[2];
  360. pos4.localPosition = lastElement_second.pos[3];
  361. //设置线段的点
  362. SetRectanglePoints(linePosConversion(lastElement_second.pos[0], lastElement_second.pos[1], lastElement_second.pos[2], lastElement_second.pos[3]));
  363. }
  364. }
  365. //是不是从Demo界面进入该页面的
  366. public bool enterFromInfraredDemo { get; set; } = false;
  367. //是否从测试场景进入
  368. public bool enterFromZimWebCamera { get; set; } = false;
  369. //确认修改
  370. public void onConfirmation()
  371. {
  372. if (enterFromZimWebCamera)
  373. {
  374. ConfirmScreenLocateManualTest();
  375. if (oldLinePosition.Count > 1) // 确保列表不为空
  376. {
  377. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  378. oldLinePosition.Clear();
  379. oldLinePosition.Add(lastElement);
  380. }
  381. SaveLocalPos();
  382. AudioMgr.ins.PlayBtn();
  383. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  384. return;
  385. }
  386. if (!ConfirmScreenLocateManual()) return;
  387. if (oldLinePosition.Count > 1) // 确保列表不为空
  388. {
  389. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  390. oldLinePosition.Clear();
  391. oldLinePosition.Add(lastElement);
  392. }
  393. if (InfraredDemo.running)
  394. {
  395. //跳转入界面
  396. AudioMgr.ins.PlayBtn();
  397. if (!enterFromInfraredDemo)
  398. {
  399. //每次初始化重置一下引导
  400. InfraredDemo._ins.resetInfraredPlayerPrefs();
  401. //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  402. //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  403. if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
  404. {
  405. //如果是红外连接成功,记录一个tag
  406. PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
  407. }
  408. onEnterInfrared();
  409. }
  410. else {
  411. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  412. }
  413. }
  414. //存储一次节点
  415. SaveLocalPos();
  416. }
  417. //重置位置
  418. public void onReset()
  419. {
  420. oldLinePosition.Clear();
  421. // 获取屏幕的四个角的像素坐标
  422. Vector2 bottomLeft = new Vector2(0, 0);
  423. // 将屏幕像素坐标转换为 Canvas 的局部坐标
  424. Vector2 localBottomLeft;
  425. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, bottomLeft, null, out localBottomLeft);
  426. // 打印结果
  427. Debug.Log("Local Bottom Left: " + localBottomLeft);
  428. //int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x) - 120), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y) - 77);
  429. int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x)), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y));
  430. pos1.anchoredPosition = new Vector3(-_x, -_y, 0); //Bottom Left
  431. pos2.anchoredPosition = new Vector3(_x, -_y, 0); //Bottom Right
  432. pos3.anchoredPosition = new Vector3(_x, _y, 0); //Top Right
  433. pos4.anchoredPosition = new Vector3(-_x, _y, 0); //Top Left
  434. //pos1.anchoredPosition = new Vector3(_x, _y, 0);
  435. //pos2.anchoredPosition = new Vector3(-_x, _y, 0);
  436. //pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
  437. //pos4.anchoredPosition = new Vector3(_x, -_y, 0);
  438. //设置一次位置
  439. SetLinePos();
  440. btnRecordInfrared.Reset();
  441. ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
  442. if (enterFromZimWebCamera)
  443. {
  444. ScreenLocate.Main.ScreenQuad.gameObject.SetActive(false);
  445. ScreenLocate.Main.UILineGenerator.Points = new Vector2[0];
  446. }
  447. }
  448. // by zim, 加载数据
  449. public void OnLoadQuadChanged(int value)
  450. {
  451. if (!doLocateAuto)
  452. {
  453. var quadTag = value switch
  454. {
  455. 0 => ScreenLocate.ScreenIdentificationTag.Manual,
  456. 1 => ScreenLocate.ScreenIdentificationTag.Auto,
  457. 2 => ScreenLocate.ScreenIdentificationTag.SemiAuto,
  458. _ => ScreenLocate.ScreenIdentificationTag.Manual,
  459. };
  460. var quad = ScreenLocate.Main.GetScreenAfterLocate(quadTag);
  461. if (quad != null)
  462. {
  463. AddOldLinePosition();
  464. GetQuadPos(quad);
  465. QuadInfo.text = "加载成功";
  466. }
  467. else
  468. QuadInfo.text = "加载失败";
  469. }
  470. }
  471. #endregion
  472. List<Vector2> _locatePointList = new();
  473. float _texWidth;
  474. float _texHeight;
  475. void RecordLocatePoint(RectTransform p, Vector2 pivot)
  476. {
  477. Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
  478. pos.x = Mathf.Clamp01(pos.x / Screen.width);
  479. pos.y = Mathf.Clamp01(pos.y / Screen.height);
  480. _locatePointList.Add(pos);
  481. }
  482. bool ConfirmScreenLocateManual()
  483. {
  484. if (InfraredDemo.running)
  485. {
  486. //渲染截图
  487. Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual();
  488. if (texture2D == null)
  489. {
  490. Debug.Log("EnterScreenLocateManual = null");
  491. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null);
  492. return false;
  493. }
  494. _locatePointList.Clear();
  495. _texWidth = texture2D.width;
  496. _texHeight = texture2D.height;
  497. RecordLocatePoint(pos1, new Vector2(0, 0));
  498. RecordLocatePoint(pos2, new Vector2(1, 0));
  499. RecordLocatePoint(pos3, new Vector2(1, 1));
  500. RecordLocatePoint(pos4, new Vector2(0, 1));
  501. //RecordLocatePoint(pos1, new Vector2(0.5f, 0.5f));
  502. //RecordLocatePoint(pos2, new Vector2(0.5f, 0.5f));
  503. //RecordLocatePoint(pos3, new Vector2(0.5f, 0.5f));
  504. //RecordLocatePoint(pos4, new Vector2(0.5f, 0.5f));
  505. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
  506. FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, 1, 1);
  507. //setPointsLocation(_locatePointList, pointsParent.gameObject, !bAuto);
  508. //同步数据
  509. ScreenLocate.quadUnityVectorList.Clear();
  510. ScreenLocate.quadUnityVectorList.Add(_locatePointList[0]);
  511. ScreenLocate.quadUnityVectorList.Add(_locatePointList[1]);
  512. //两个点切换,顺序不一样
  513. ScreenLocate.quadUnityVectorList.Add(_locatePointList[3]);
  514. ScreenLocate.quadUnityVectorList.Add(_locatePointList[2]);
  515. ScreenLocate.SaveScreenLocateVectorList();
  516. return true;
  517. }
  518. return false;
  519. }
  520. void ConfirmScreenLocateManualTest()
  521. {
  522. //渲染截图
  523. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  524. Texture2D texture2D = _screenLocate.EnterScreenLocateManual();
  525. if (texture2D == null)
  526. {
  527. _screenLocate.QuitScreenLocateManual(null);
  528. return;
  529. }
  530. _locatePointList.Clear();
  531. _texWidth = texture2D.width;
  532. _texHeight = texture2D.height;
  533. RecordLocatePoint(pos1, new Vector2(0, 0));
  534. RecordLocatePoint(pos2, new Vector2(1, 0));
  535. RecordLocatePoint(pos3, new Vector2(1, 1));
  536. RecordLocatePoint(pos4, new Vector2(0, 1));
  537. _screenLocate.QuitScreenLocateManual(_locatePointList);
  538. //FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
  539. setPointsLocation(_locatePointList,pointsParent.gameObject,!bAuto);
  540. if (!doLocateAuto) // 设置手动定位数据
  541. {
  542. setPointsManual(_locatePointList, GameObject.Find("WebCameraView/CameraImage0/ScreenQuad"));
  543. var size = ScreenLocate.Main.getUVCCameraInfoSize;
  544. var quad = new QuadrilateralInCamera(
  545. new o0.Geometry2D.Float.Vector(_locatePointList[0].x * size.x, _locatePointList[0].y * size.y),
  546. new o0.Geometry2D.Float.Vector(_locatePointList[1].x * size.x, _locatePointList[1].y * size.y),
  547. new o0.Geometry2D.Float.Vector(_locatePointList[3].x * size.x, _locatePointList[3].y * size.y),
  548. new o0.Geometry2D.Float.Vector(_locatePointList[2].x * size.x, _locatePointList[2].y * size.y),
  549. size.o0Vector());
  550. if (ScreenLocate.Main.ScreenIdentification!=null)
  551. {
  552. ScreenLocate.Main.ScreenIdentification.SetScreenQuad(quad);
  553. ScreenLocate.Main.ScreenIdentification.QuadManual = quad;
  554. Debug.Log($"[ScreenLocate] 记录手动数据: {quad}");
  555. }
  556. }
  557. //同步数据
  558. ScreenLocate.quadUnityVectorList.Clear();
  559. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[0].x, _locatePointList[0].y));
  560. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[1].x, _locatePointList[1].y));
  561. //两个点切换,顺序不一样
  562. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[3].x, _locatePointList[3].y));
  563. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[2].x, _locatePointList[2].y));
  564. ScreenLocate.SaveScreenLocateVectorList();
  565. }
  566. void setPointsLocation(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
  567. {
  568. //GameObject pointsTF2 = GameObject.Find("WebCameraView/CameraImage0/ScreenQuad");
  569. pointsTF2.SetActive(active);
  570. if (pointsTF2.transform.childCount == targetList.Count)
  571. {
  572. for (int i = 0; i < pointsTF2.transform.childCount; i++)
  573. {
  574. Transform pointTF = pointsTF2.transform.GetChild(i);
  575. Vector2 pos = targetList[i];
  576. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(Vector2.one, pointsTF2.GetComponent<RectTransform>().rect);
  577. pointTF.gameObject.SetActive(true);
  578. }
  579. }
  580. }
  581. void setPointsManual(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
  582. {
  583. setPointsLocation(targetList, pointsTF2, active);
  584. var lo = new Vector2(-0.5f, -0.5f);
  585. ScreenLocate.Main.UILineGenerator.Points = new Vector2[4] {
  586. 2 * (targetList[0] + lo),
  587. 2 * (targetList[1] + lo),
  588. 2 * (targetList[2] + lo),
  589. 2 * (targetList[3] + lo) };
  590. }
  591. void SaveLocalPos()
  592. {
  593. List<Vector3> screenPositions = new List<Vector3>();
  594. screenPositions.Add(pos1.anchoredPosition);
  595. screenPositions.Add(pos2.anchoredPosition);
  596. screenPositions.Add(pos3.anchoredPosition);
  597. screenPositions.Add(pos4.anchoredPosition);
  598. string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y}")); //,{v.z}
  599. Debug.Log("Local UI Position: " + saveStr);
  600. PlayerPrefs.SetString("ScreenPositioningView", saveStr);
  601. }
  602. // by zim
  603. void GetDefalutPos()
  604. {
  605. if (ScreenLocate.Main.DebugOnZIMDemo)
  606. {
  607. var quad = ScreenLocate.Main.GetScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.Manual);
  608. if (quad != null)
  609. GetQuadPos(quad);
  610. else
  611. GetLocalPos();
  612. }
  613. else
  614. GetLocalPos();
  615. }
  616. void GetQuadPos(QuadrilateralInCamera quad)
  617. {
  618. var qPos1 = quad.Quad.A.pixelToLocalPosition_AnchorCenter(quad.CameraSize, Bg.rectTransform.rect);
  619. var qPos2 = quad.Quad.B.pixelToLocalPosition_AnchorCenter(quad.CameraSize, Bg.rectTransform.rect);
  620. var qPos4 = quad.Quad.C.pixelToLocalPosition_AnchorCenter(quad.CameraSize, Bg.rectTransform.rect);
  621. var qPos3 = quad.Quad.D.pixelToLocalPosition_AnchorCenter(quad.CameraSize, Bg.rectTransform.rect);
  622. pos1.anchoredPosition = qPos1;
  623. pos2.anchoredPosition = qPos2;
  624. pos3.anchoredPosition = qPos3;
  625. pos4.anchoredPosition = qPos4;
  626. SetRectanglePoints(linePosConversion(qPos1, qPos2, qPos3, qPos4));
  627. }
  628. void GetLocalPos()
  629. {
  630. string posListStr = PlayerPrefs.GetString("ScreenPositioningView", "");
  631. if (!string.IsNullOrWhiteSpace(posListStr))
  632. {
  633. List<Vector2> posList = posListStr.Split(';')
  634. .Select(s =>
  635. {
  636. string[] parts = s.Split(',');
  637. return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
  638. })
  639. .ToList();
  640. pos1.anchoredPosition = posList[0];
  641. pos2.anchoredPosition = posList[1];
  642. pos3.anchoredPosition = posList[2];
  643. pos4.anchoredPosition = posList[3];
  644. SetRectanglePoints(linePosConversion(pos1.localPosition, pos2.localPosition, pos3.localPosition, pos4.localPosition));
  645. }
  646. }
  647. #region 按钮颜色切换
  648. private void OnButtonClick(Button button)
  649. {
  650. // 切换按钮颜色
  651. ColorBlock colors = button.colors;
  652. colors.normalColor = highlightedColor;
  653. colors.highlightedColor = highlightedColor;
  654. button.colors = colors;
  655. // 切换字体颜色
  656. Text buttonText = button.GetComponentInChildren<Text>();
  657. buttonText.color = highlightedTextColor;
  658. }
  659. private void ResetButton(Button button)
  660. {
  661. // 重置按钮颜色
  662. ColorBlock colors = button.colors;
  663. colors.normalColor = normalColor;
  664. colors.highlightedColor = normalColor;
  665. button.colors = colors;
  666. // 重置字体颜色
  667. Text buttonText = button.GetComponentInChildren<Text>();
  668. buttonText.color = normalTextColor;
  669. }
  670. #endregion
  671. #region 标定完成之后进入游戏界面
  672. void onEnterInfrared()
  673. {
  674. //添加进入射箭场景
  675. if (PlayerPrefs.GetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 0) == 0)
  676. {
  677. Debug.Log("-----进入射箭场景!");
  678. NewUserGuiderManager newUserGuiderManager = FindObjectOfType<NewUserGuiderManager>();
  679. newUserGuiderManager.curConfigKey = "开始-红外调整";
  680. newUserGuiderManager.isNewModule = AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.HOUYIPRO || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.Gun || AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.ARTEMISPRO;
  681. //进入射箭场景
  682. GlobalData.pkMatchType = PKMatchType.None;
  683. GameMgr.gameType = 1;
  684. //射一箭回到连接页面,Device.view
  685. GameMgr.bNavBack = true;
  686. GameMgr.bShowDistance = false;
  687. AimHandler.ins.bInitOne = false;//true
  688. //关闭计时器
  689. GameMgr.turnOffTimer = true;
  690. //关闭左边靶子和底部速度栏
  691. GameMgr.HideTargetView = true;
  692. GameMgr.HideBillboard = true;
  693. GameMgr.ButtonCount = 0;
  694. UnityEngine.SceneManagement.SceneManager.LoadScene(
  695. "Game", UnityEngine.SceneManagement.LoadSceneMode.Single);
  696. PlayerPrefs.SetInt("entry-guider-infrared-" + LoginMgr.myUserInfo.id, 1);
  697. }
  698. else
  699. {
  700. Debug.Log("-----跳转回连接界面!");
  701. //如不是第一次,则应该跳转回连接界面
  702. ViewMgr.Instance.DestroyView<SmartArcheryView>();
  703. ViewManager2.HideView(ViewManager2.Path_ConnectGuidanceView);
  704. }
  705. }
  706. #endregion
  707. #region 最后选择算法处理UI
  708. //选择全自动结果后进入游戏
  709. void OnFirstResult()
  710. {
  711. ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.Auto);
  712. UpdateQuadUnityVectorList();
  713. onCompelete();
  714. }
  715. //选择半自动结果
  716. void OnSecondResult()
  717. {
  718. ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.SemiAuto);
  719. UpdateQuadUnityVectorList();
  720. onCompelete();
  721. }
  722. public void onCompelete()
  723. {
  724. if (enterFromZimWebCamera)
  725. {
  726. SaveLocalPos();
  727. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  728. return;
  729. }
  730. if (InfraredDemo.running)
  731. {
  732. //跳转入界面
  733. AudioMgr.ins.PlayBtn();
  734. if (!enterFromInfraredDemo)
  735. {
  736. //每次初始化重置一下引导
  737. InfraredDemo._ins.resetInfraredPlayerPrefs();
  738. //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  739. //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  740. if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
  741. {
  742. //如果是红外连接成功,记录一个tag
  743. PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
  744. }
  745. onEnterInfrared();
  746. }
  747. else
  748. {
  749. ViewManager2.HideView(ViewManager2.Path_InfraredScreenPositioningView);
  750. }
  751. }
  752. //存储一次节点
  753. SaveLocalPos();
  754. }
  755. /// <summary>
  756. /// 选择模式后更新 quadUnityVectorList
  757. /// </summary>
  758. void UpdateQuadUnityVectorList() {
  759. ScreenLocate.Main.UpdateQuadUnityVectorList();
  760. SyncQuadUnityVectorListToPos();
  761. InfraredDemo._ins?.SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList, 1, 1);
  762. _locatePointList.Clear();
  763. }
  764. void SyncQuadUnityVectorListToPos() {
  765. Debug.Log("[ScreenLocate] SyncQuadUnityVectorListToPos quadUnityVectorList:" + ScreenLocate.Main.PrintVector2List(ScreenLocate.quadUnityVectorList));
  766. pos1.anchoredPosition = ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  767. pos2.anchoredPosition = ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  768. pos4.anchoredPosition = ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  769. pos3.anchoredPosition = ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect);
  770. }
  771. #endregion
  772. }