InfraredScreenPositioningView.cs 28 KB

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