InfraredScreenPositioningView.cs 36 KB

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