InfraredScreenPositioningView.cs 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  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 UnityEngine.SceneManagement;
  11. using o0InfraredLocate.ZIM;
  12. using AdaptUI;
  13. using AppUI.Manager.View;
  14. namespace AppUI.View.Infrared
  15. {
  16. public enum ScreenPositioningStep
  17. {
  18. None, //
  19. Start,//开始界面
  20. AutoEnd,//自动定位结束后界面
  21. AutoEndFail,//自动定位结束后没有数据界面
  22. Manual,//手动界
  23. Successful,//四角消灭界面
  24. }
  25. public class LinePosition
  26. {
  27. public int index;
  28. public List<Vector3> pos;
  29. }
  30. public class InfraredScreenPositioningView : MonoBehaviour
  31. {
  32. [SerializeField]
  33. RectTransform canvasRectTransform;
  34. [SerializeField]
  35. RectTransform draggableParent;
  36. [SerializeField]
  37. RectTransform pointsParent;
  38. [SerializeField]
  39. RectTransform pos1;
  40. [SerializeField]
  41. RectTransform pos2;
  42. [SerializeField]
  43. RectTransform pos3;
  44. [SerializeField]
  45. RectTransform pos4;
  46. //画线时候的点偏移量
  47. float offset = 10;
  48. [Tooltip("勾选后拖拽点与 Line 顶点均按控件中心对齐")]
  49. [SerializeField]
  50. bool useCenterPivot = false;
  51. [SerializeField]
  52. Line line;
  53. List<LinePosition> oldLinePosition;
  54. Vector3 beginPos;
  55. Vector3 endPos;
  56. [SerializeField]
  57. Button btnAuto;
  58. [SerializeField]
  59. Button btnHandMovement;
  60. [SerializeField]
  61. BtnRecordInfrared btnRecordInfrared;
  62. bool bAuto = true;
  63. [SerializeField] Color normalColor = Color.white;
  64. [SerializeField] Color highlightedColor = Color.green;
  65. [SerializeField] Color normalTextColor = Color.black;
  66. [SerializeField] Color highlightedTextColor = Color.white;
  67. [Header("Common Layout Group")]
  68. [SerializeField]
  69. GameObject mask;
  70. [SerializeField]
  71. Line maskLine;
  72. [SerializeField]
  73. Line maskLine_ipad;
  74. [SerializeField]
  75. RawImage rawImage;
  76. [Tooltip("Demo测试时候显示")]
  77. [SerializeField]
  78. RectTransform crosshair;
  79. [SerializeField]
  80. RectTransform crosshairRed;
  81. [Tooltip("存在校准数据时候显示")]
  82. [SerializeField] LineGenerator CurrentUILineGenerator;//开始显示屏幕线条
  83. [Header("Start Layout Group")]
  84. [SerializeField]
  85. GameObject LayoutStart;
  86. [Header("AutoEnd Layout Group")]
  87. [SerializeField]
  88. GameObject LayoutAutoEnd;
  89. [SerializeField] Button AutoEndConfirmBtn;
  90. [SerializeField] Sprite ConfirmBtnBg;
  91. [SerializeField] Color ConfirmBtnNormalColor = Color.white;
  92. [SerializeField] Color ConfirmBtnHideColor = new Color(181f / 255f, 181f / 255f, 181f / 255f, 1f);
  93. [SerializeField]
  94. GameObject TipCalibrationFailedIphone;
  95. [Tooltip("没有存在校准数据时候显示 ipad 提示失败")]
  96. [SerializeField]
  97. GameObject TipCalibrationFailedIpad;
  98. [Header("AutoEndFail Layout Group")]
  99. [SerializeField]
  100. GameObject LayoutAutoFail;
  101. [SerializeField] Button AutoEndFailConfirmBtn;
  102. [Header("Manual Layout Group")]
  103. [SerializeField]
  104. GameObject LayoutManual;
  105. [SerializeField]
  106. Line manualLine;//手动页面的line
  107. [Header("Successful Layout Group")]
  108. [SerializeField]
  109. GameObject LayoutSuccessful;
  110. //[Tooltip("没有存在校准数据时候显示提示失败")]
  111. //[SerializeField]
  112. //GameObject AutoCalibrationFailedTip;
  113. bool bMarkerFinish = false;
  114. ScreenPositioningStep perStepView = ScreenPositioningStep.None;//最后一次进入LayoutSuccessful的页面
  115. //标记当前页面情况
  116. ScreenPositioningStep curStepView = ScreenPositioningStep.Start;//设置一个开始页面
  117. InfraredDemo infraredDemo;
  118. bool doLocateAuto;
  119. int DefaultResolutionIndex;
  120. private void Awake()
  121. {
  122. //设置一次显示相机的image 的颜色
  123. rawImage.color = Color.white;
  124. offset = line.MyThickness;
  125. SyncDraggableCenterMode();
  126. }
  127. void Start()
  128. {
  129. //获取InfraredDemo
  130. infraredDemo = InfraredDemo._ins;
  131. doLocateAuto = false;
  132. Debug.Log("quadUnityVectorList count:" + ScreenLocate.quadUnityVectorList.Count);
  133. if (ScreenLocate.quadUnityVectorList.Count == 0)
  134. {
  135. Debug.Log("GetLocalPos");
  136. GetLocalPos();
  137. }
  138. else
  139. {
  140. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  141. Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
  142. //自动识别时候四个点
  143. SyncQuadUnityVectorListToPos();
  144. }
  145. //更新start相关ui
  146. UpdateStartUI();
  147. //记录操作的位置信息
  148. oldLinePosition = new List<LinePosition>();
  149. SetLinePos();
  150. //相机感光度
  151. if (infraredDemo)
  152. {
  153. ////重置偏移量
  154. //infraredDemo.ResetCenterOffset();
  155. //////重置识别点
  156. ////ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
  157. //////清除一下记录的点
  158. ////ScreenLocate.quadUnityVectorList.Clear();
  159. //infraredDemo.ResetPositioningData();
  160. //offset = line.MyThickness = markerPointsLine.MyThickness = infraredDemo.lineWidth.Get();
  161. //LineGenerator 比 line 宽少了大概2倍
  162. CurrentUILineGenerator.LineThickness = infraredDemo.lineWidth.Get() * 2;
  163. //扇形宽
  164. //markerPointsLine.MyFanWidth = line.MyFanWidth = infraredDemo.fanWidth.Get();
  165. QuadrilateralInCamera screen = ScreenLocate.Main.ScreenIdentification.Screen.QuadInCamera;
  166. CurrentUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screen, ScreenLocate.Main.getUVCCameraInfoSize);
  167. if (CurrentUILineGenerator.Points.Length != 0)
  168. {
  169. //显示maskline
  170. setMaskLineByDeviceType(false);
  171. }
  172. else {
  173. //显示maskline
  174. switchMaskLineByDeviceType(true);
  175. }
  176. }
  177. }
  178. private void OnDestroy()
  179. {
  180. }
  181. public RawImage Bg => rawImage;
  182. void Update()
  183. {
  184. if (enterFromZimWebCamera && ScreenLocate.Main.DebugOnZIMDemo)
  185. {
  186. // ZimWebCamera场景测试
  187. rawImage.texture = ScreenLocate.Main.outputRawImages[7].texture;
  188. if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
  189. {
  190. // 检测到光点
  191. var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
  192. crosshair.gameObject.SetActive(true);
  193. crosshair.anchoredPosition = posInCanvas;
  194. }
  195. else
  196. crosshair.gameObject.SetActive(false);
  197. }
  198. else if (InfraredDemo.running)
  199. {
  200. //渲染相机画面
  201. Texture texture = InfraredDemo.infraredCameraHelper.GetCameraTexture();
  202. if (rawImage.texture == null || texture.GetNativeTexturePtr() != rawImage.texture.GetNativeTexturePtr())
  203. {
  204. rawImage.texture = texture;
  205. }
  206. if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
  207. {
  208. // 检测到光点
  209. var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
  210. //crosshairSmall.gameObject.SetActive(true);
  211. //crosshairSmall.anchoredPosition = posInCanvas;
  212. }
  213. //else
  214. //crosshairSmall.gameObject.SetActive(false);
  215. //rawImage.material = InfraredDemo.infraredCameraHelper.GetCameraMaterial();
  216. //定位最后成功之后出现光标
  217. if (curStepView == ScreenPositioningStep.Successful)
  218. {
  219. if (ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.HasValue)
  220. {
  221. // 检测到光点
  222. var posInCanvas = ScreenLocate.Main.infraredSpotBuffer[0].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(ScreenLocate.Main.CameraSize, rawImage.rectTransform.rect);
  223. crosshairRed.gameObject.SetActive(true);
  224. crosshairRed.anchoredPosition = posInCanvas;
  225. }
  226. else
  227. crosshairRed.gameObject.SetActive(false);
  228. }
  229. else if (crosshairRed.gameObject.activeSelf)
  230. {
  231. crosshairRed.gameObject.SetActive(false);
  232. }
  233. }
  234. }
  235. /// <summary>
  236. /// 根据平台显示不同的maskLine
  237. /// </summary>
  238. /// <param name="bShow"></param>
  239. void switchMaskLineByDeviceType(bool bShow)
  240. {
  241. if (DeviceTypeHelper.DetectDeviceType() == DeviceTypeHelper.DeviceType.iPhone)
  242. {
  243. maskLine.gameObject.SetActive(bShow);
  244. maskLine_ipad.gameObject.SetActive(!bShow);
  245. }
  246. else
  247. {
  248. maskLine.gameObject.SetActive(!bShow);
  249. maskLine_ipad.gameObject.SetActive(bShow);
  250. }
  251. }
  252. void setMaskLineByDeviceType(bool bShow)
  253. {
  254. if (DeviceTypeHelper.DetectDeviceType() == DeviceTypeHelper.DeviceType.iPhone)
  255. {
  256. maskLine.gameObject.SetActive(bShow);
  257. }
  258. else
  259. {
  260. maskLine_ipad.gameObject.SetActive(bShow);
  261. }
  262. }
  263. /// <summary>
  264. /// 根据 平台返回 对应的 ScreenPositions
  265. /// </summary>
  266. /// <returns></returns>
  267. List<Vector2> getMaskLinePositionsByDeviceType()
  268. {
  269. return DeviceTypeHelper.DetectDeviceType() == DeviceTypeHelper.DeviceType.iPhone ? maskLine.ScreenPositions : maskLine_ipad.ScreenPositions;
  270. }
  271. /// <summary>
  272. /// 设置校准结束时候的定位按钮
  273. /// </summary>
  274. /// <param name="active"></param>
  275. public void SetAutoConfirmBtnActive(bool active)
  276. {
  277. if (AutoEndConfirmBtn == null) return;
  278. Image btnImage = AutoEndConfirmBtn.GetComponent<Image>();
  279. if (btnImage == null) return;
  280. // 根据 active 状态切换按钮外观
  281. if (active)
  282. {
  283. btnImage.color = ConfirmBtnNormalColor;
  284. if (ConfirmBtnBg != null)
  285. {
  286. btnImage.sprite = ConfirmBtnBg;
  287. }
  288. AutoEndConfirmBtn.interactable = true;
  289. }
  290. else
  291. {
  292. btnImage.color = ConfirmBtnHideColor;
  293. btnImage.sprite = null; // 去掉背景图
  294. AutoEndConfirmBtn.interactable = false;
  295. }
  296. }
  297. /// <summary>
  298. /// 根据enum操作ui
  299. /// </summary>
  300. /// <param name="step"></param>
  301. public void SetScreenPositioningStepState(ScreenPositioningStep step = ScreenPositioningStep.None)
  302. {
  303. curStepView = step;
  304. AllScreenPositioningStepFalse();
  305. TipCalibrationFailedIphone.SetActive(false);
  306. TipCalibrationFailedIpad.SetActive(false);
  307. //AutoCalibrationFailedTip.SetActive(false);
  308. QuadrilateralInCamera screenAuto = ScreenLocate.Main.ScreenIdentification.QuadAuto;
  309. switch (step)
  310. {
  311. case ScreenPositioningStep.Start:
  312. //maskLine.SetDrawMask(true);
  313. switchMaskLineByDeviceType(true);
  314. LayoutStart.SetActive(true);
  315. UpdateStartUI();
  316. break;
  317. case ScreenPositioningStep.AutoEnd:
  318. //switchMaskLineByDeviceType(true);
  319. setMaskLineByDeviceType(false);
  320. LayoutAutoEnd.SetActive(true);
  321. pointsParent.gameObject.SetActive(false);
  322. CurrentUILineGenerator.enabled = true;
  323. if (screenAuto == null)
  324. {
  325. //不存在数据,走流程2
  326. SetAutoConfirmBtnActive(false);
  327. if (DeviceTypeHelper.DetectDeviceType() == DeviceTypeHelper.DeviceType.iPhone)
  328. {
  329. TipCalibrationFailedIphone.SetActive(true);
  330. }
  331. else
  332. {
  333. TipCalibrationFailedIpad.SetActive(true);
  334. }
  335. }
  336. else
  337. {
  338. //存在数据,显示确认按钮
  339. SetAutoConfirmBtnActive(true);
  340. }
  341. break;
  342. case ScreenPositioningStep.AutoEndFail:
  343. switchMaskLineByDeviceType(true);
  344. LayoutAutoFail.SetActive(true);
  345. CurrentUILineGenerator.enabled = true;
  346. break;
  347. case ScreenPositioningStep.Manual:
  348. setMaskLineByDeviceType(false);
  349. SaveStartOldLinePosition();
  350. perStepView = ScreenPositioningStep.Manual;
  351. bMarkerFinish = false;
  352. LayoutManual.SetActive(true);
  353. bAuto = false;
  354. doLocateAuto = false;
  355. draggableParent.gameObject.SetActive(true);
  356. pointsParent.gameObject.SetActive(false);
  357. //隐藏识别后的线
  358. CurrentUILineGenerator.enabled = false;
  359. if (screenAuto == null)
  360. {
  361. //不存在数据
  362. onResetByMaskLine();
  363. }
  364. break;
  365. case ScreenPositioningStep.Successful:
  366. setMaskLineByDeviceType(false);
  367. LayoutSuccessful.SetActive(true);
  368. pointsParent.gameObject.SetActive(false);
  369. CurrentUILineGenerator.enabled = false;
  370. if (SB_EventSystem.ins && !SB_EventSystem.ins.simulateMouseIsAwaked)
  371. {
  372. SB_EventSystem.ins.AwakenSimulateMouse();
  373. }
  374. break;
  375. }
  376. }
  377. void AllScreenPositioningStepFalse()
  378. {
  379. LayoutStart.SetActive(false);
  380. //LayoutMarker.SetActive(false);
  381. LayoutManual.SetActive(false);
  382. LayoutAutoEnd.SetActive(false);
  383. LayoutAutoFail.SetActive(false);
  384. //LayoutLightCtrl.SetActive(false);
  385. LayoutSuccessful.SetActive(false);
  386. onClearSuccessfullLineGenerator();
  387. }
  388. /// <summary>
  389. /// 开始页面时候ui
  390. /// </summary>
  391. void UpdateStartUI()
  392. {
  393. pointsParent.gameObject.SetActive(false);
  394. CurrentUILineGenerator.enabled = true;
  395. if (ScreenLocate.quadUnityVectorList.Count == 0)
  396. {
  397. }
  398. else
  399. {
  400. }
  401. }
  402. public void OnClick_Back()
  403. {
  404. AudioMgr.ins.PlayBtn();
  405. ViewManager.HideView(UIViewType.InfraredScreenPositioningView);
  406. }
  407. public void OnClick_PositioningFailedDemonstration() {
  408. Debug.Log("失败演示Todo.");
  409. }
  410. /// <summary>
  411. /// 返回最开始页面
  412. /// </summary>
  413. public void OnClick_BackLayoutStart()
  414. {
  415. ClearPointsData();
  416. SetScreenPositioningStepState(ScreenPositioningStep.Start);
  417. }
  418. void ClearPointsData()
  419. {
  420. //重置中心点0
  421. infraredDemo?.ResetCenterOffset();
  422. //清除白线数据
  423. infraredDemo?.ResetPositioningData();
  424. //上面应该清空了 ScreenLocate.Main.ScreenIdentification.QuadAuto
  425. onClearCurrentUILineGenerator();
  426. }
  427. /// <summary>
  428. /// successful 返回页面时候,根据ScreenPositioningStep区分
  429. /// </summary>
  430. public void OnClick_BackByScreenPositioningStep()
  431. {
  432. //SetScreenPositioningStepState(ScreenPositioningStep.LightCtrl);
  433. //清空两个绘制线
  434. onClearSuccessfullLineGenerator();
  435. //if(perStepView == ScreenPositioningStep.Manual)
  436. // SetScreenPositioningStepState(ScreenPositioningStep.Manual);
  437. //else
  438. // SetScreenPositioningStepState(ScreenPositioningStep.Marker);
  439. }
  440. /// <summary>
  441. /// 直接进入游戏流程
  442. /// </summary>
  443. public void OnClick_EnterGame()
  444. {
  445. //对比确认效果。直接进入游戏引导页面
  446. EnterGame();
  447. }
  448. /// <summary>
  449. /// 进入自动流程校准操作
  450. /// </summary>
  451. public void OnClick_EnterAuto()
  452. {
  453. Debug.Log("*********** OnClick_EnterAuto ************");
  454. //自动按钮时候进入之前的校准流程
  455. ClearPointsData();
  456. OnClick_Auto();
  457. }
  458. /// <summary>
  459. /// 自动识别
  460. /// </summary>
  461. public void OnClick_Auto()
  462. {
  463. bAuto = true;
  464. doLocateAuto = true;
  465. if (enterFromZimWebCamera)
  466. {
  467. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  468. _screenLocate.EnterScreenLocateManualAuto();
  469. return;
  470. }
  471. if (InfraredDemo.running)
  472. {
  473. InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
  474. }
  475. }
  476. /// <summary>
  477. /// 进入手动界面
  478. /// </summary>
  479. public void OnClick_EnterManual()
  480. {
  481. SetScreenPositioningStepState(ScreenPositioningStep.Manual);
  482. }
  483. /// <summary>
  484. /// 进入最后四角消灭页面
  485. /// </summary>
  486. public void OnClick_Successful()
  487. {
  488. SetScreenPositioningStepState(ScreenPositioningStep.Successful);
  489. }
  490. /// <summary>
  491. /// 切换成手动方式
  492. /// </summary>
  493. public void onHandMovement()
  494. {
  495. bAuto = false;
  496. doLocateAuto = false;
  497. draggableParent.gameObject.SetActive(true);
  498. pointsParent.gameObject.SetActive(false);
  499. mask.SetActive(false);
  500. }
  501. /// <summary>
  502. /// 自动识别后同步
  503. /// </summary>
  504. public void SyncScreenPosition()
  505. {
  506. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  507. Debug.Log("texSize:" + texSize + " = " + canvasRectTransform.rect);
  508. SyncQuadUnityVectorListToPos();
  509. RefreshLineFromHandles();
  510. //SyncQuadUnityVectorListToMarkerPointesPos();
  511. //显示提示
  512. //pointMarker.ShowHintImageParent(true);
  513. //AutoEndConfirmBtn.interactable = true;
  514. SetAutoConfirmBtnActive(true);
  515. //屏幕变化情况下。同步修改对应的line
  516. //设置两个线段
  517. QuadrilateralInCamera screenAuto = ScreenLocate.Main.ScreenIdentification.QuadAuto;
  518. QuadrilateralInCamera screenSemiAuto = ScreenLocate.Main.ScreenIdentification.QuadSemiAuto;
  519. if (screenAuto != null)
  520. {
  521. //Debug.Log("[校准流程]Successful自动识别screenAuto信息 ------------ ");
  522. //if (AutoCalibrationFailedTip != null)
  523. // AutoCalibrationFailedTip.SetActive(false);
  524. CurrentUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenAuto, texSize);
  525. //FirstUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenAuto, texSize);
  526. //如果是开始页面进行自动定位的
  527. if (curStepView == ScreenPositioningStep.Start)
  528. {
  529. if (curStepView != ScreenPositioningStep.AutoEnd)
  530. {
  531. SetScreenPositioningStepState(ScreenPositioningStep.AutoEnd);
  532. }
  533. }
  534. }
  535. else
  536. {
  537. //if (AutoCalibrationFailedTip != null)
  538. // AutoCalibrationFailedTip.SetActive(true);
  539. Debug.LogError("screenAuto 不存在!");
  540. //如果是开始页面进行自动定位的
  541. }
  542. if (screenSemiAuto != null)
  543. {
  544. //Debug.Log("[校准流程]Successful半自动识别screenSemiAuto信息 ------------ ");
  545. //SecondUILineGenerator.Points = infraredDemo.ConvertQuadToPoints(screenSemiAuto, texSize);
  546. }
  547. //else
  548. //{
  549. // Debug.LogError("screenSemiAuto 不存在!");
  550. //}
  551. }
  552. /// <summary>
  553. /// 清空白线数据
  554. /// </summary>
  555. void onClearCurrentUILineGenerator()
  556. {
  557. if (CurrentUILineGenerator.Points.Length != 0) CurrentUILineGenerator.Points = new Vector2[0];//System.Array.Clear(CurrentUILineGenerator.Points, 0, CurrentUILineGenerator.Points.Length);
  558. }
  559. /// <summary>
  560. /// 清空最后识别的两个自动和半自动数据线
  561. /// </summary>
  562. void onClearSuccessfullLineGenerator()
  563. {
  564. //if (FirstUILineGenerator.Points.Length != 0) FirstUILineGenerator.Points = new Vector2[0]; //System.Array.Clear(FirstUILineGenerator.Points, 0, FirstUILineGenerator.Points.Length);
  565. //if (SecondUILineGenerator.Points.Length != 0) SecondUILineGenerator.Points = new Vector2[0]; //System.Array.Clear(SecondUILineGenerator.Points, 0, SecondUILineGenerator.Points.Length);
  566. }
  567. #region 绘制线段部分
  568. //点击拖拽的开始位置
  569. public void onBeginPos(int index, Vector3 pos)
  570. {
  571. Debug.Log("pos begin: " + pos);
  572. beginPos = pos;
  573. }
  574. public void onDragPos(int index, Vector3 pos)
  575. {
  576. RefreshLineFromHandles();
  577. }
  578. //点击拖拽的结束位置
  579. public void onEndPos(int index, Vector3 pos)
  580. {
  581. Debug.Log("pos end: " + pos);
  582. endPos = pos;
  583. if (beginPos == endPos) return;
  584. //Debug.Log(index+",最后的点:" + pos);
  585. //再记录一次最后的点
  586. SetLinePos();
  587. }
  588. //同步设置图片对应的位置到line
  589. public void SetLinePos()
  590. {
  591. //记录一个操作点的操作位置
  592. AddOldLinePosition();
  593. RefreshLineFromHandles();
  594. }
  595. void SyncDraggableCenterMode()
  596. {
  597. SyncDraggableCenterMode(pos1);
  598. SyncDraggableCenterMode(pos2);
  599. SyncDraggableCenterMode(pos3);
  600. SyncDraggableCenterMode(pos4);
  601. }
  602. void SyncDraggableCenterMode(RectTransform handle)
  603. {
  604. if (handle == null)
  605. return;
  606. DraggablePolygonPoint draggable = handle.GetComponent<DraggablePolygonPoint>();
  607. if (draggable != null)
  608. draggable.SetUseCenterDrag(useCenterPivot);
  609. }
  610. Vector3 GetHandleLocalPosition(RectTransform handle)
  611. {
  612. if (!useCenterPivot)
  613. return handle.localPosition;
  614. return handle.localPosition + (Vector3)handle.rect.center;
  615. }
  616. void SetHandleLocalPosition(RectTransform handle, Vector3 localPos)
  617. {
  618. handle.localPosition = useCenterPivot ? localPos - (Vector3)handle.rect.center : localPos;
  619. }
  620. Vector2 GetHandleAnchoredPosition(RectTransform handle)
  621. {
  622. if (!useCenterPivot)
  623. return handle.anchoredPosition;
  624. return handle.anchoredPosition + (Vector2)handle.rect.center;
  625. }
  626. void SetHandleAnchoredPosition(RectTransform handle, Vector2 anchoredPos)
  627. {
  628. handle.anchoredPosition = useCenterPivot ? anchoredPos - (Vector2)handle.rect.center : anchoredPos;
  629. }
  630. void RefreshLineFromHandles()
  631. {
  632. SetRectanglePoints(linePosConversion(
  633. GetHandleLocalPosition(pos1),
  634. GetHandleLocalPosition(pos2),
  635. GetHandleLocalPosition(pos3),
  636. GetHandleLocalPosition(pos4)));
  637. }
  638. static Vector2 GetCornerRecordPivot(int index)
  639. {
  640. switch (index)
  641. {
  642. case 0: return new Vector2(0, 0);
  643. case 1: return new Vector2(1, 0);
  644. case 2: return new Vector2(1, 1);
  645. case 3: return new Vector2(0, 1);
  646. default: return new Vector2(0.5f, 0.5f);
  647. }
  648. }
  649. void RecordHandleLocatePoint(RectTransform handle, int index)
  650. {
  651. Vector2 pivot = useCenterPivot ? new Vector2(0.5f, 0.5f) : GetCornerRecordPivot(index);
  652. RecordLocatePoint(handle, pivot);
  653. }
  654. void AddOldLinePosition()
  655. {
  656. Vector3[] v = new Vector3[4];
  657. pos1.GetWorldCorners(v);
  658. Vector3[] v1 = new Vector3[4];
  659. pos1.GetLocalCorners(v1);
  660. //for (int i = 0; i < 4; i++)
  661. //{
  662. // Debug.Log(i + " =1= " + v[i]);
  663. // Debug.Log(i + " =2= " + v1[i]);
  664. //}
  665. //记录一个操作点的操作位置
  666. List<Vector3> screenPositions = new List<Vector3>();
  667. screenPositions.Add(GetHandleLocalPosition(pos1));
  668. screenPositions.Add(GetHandleLocalPosition(pos2));
  669. screenPositions.Add(GetHandleLocalPosition(pos3));
  670. screenPositions.Add(GetHandleLocalPosition(pos4));
  671. LinePosition linePosition = new LinePosition();
  672. linePosition.index = oldLinePosition.Count;
  673. linePosition.pos = screenPositions;
  674. oldLinePosition.Add(linePosition);
  675. }
  676. /// <summary>
  677. /// 记录一个开始位置
  678. /// </summary>
  679. void SaveStartOldLinePosition()
  680. {
  681. oldLinePosition.Clear();
  682. //记录一个操作点的操作位置
  683. List<Vector3> screenPositions = new List<Vector3>();
  684. screenPositions.Add(GetHandleLocalPosition(pos1));
  685. screenPositions.Add(GetHandleLocalPosition(pos2));
  686. screenPositions.Add(GetHandleLocalPosition(pos3));
  687. screenPositions.Add(GetHandleLocalPosition(pos4));
  688. LinePosition linePosition = new LinePosition();
  689. linePosition.index = 1;
  690. linePosition.pos = screenPositions;
  691. oldLinePosition.Add(linePosition);
  692. }
  693. //转换绘制线段的点
  694. List<Vector2> linePosConversion(Vector3 _pos1, Vector3 _pos2, Vector3 _pos3, Vector3 _pos4)
  695. {
  696. List<Vector2> _screenPositions = new List<Vector2>();
  697. if (useCenterPivot)
  698. {
  699. _screenPositions.Add(_pos1);
  700. _screenPositions.Add(_pos2);
  701. _screenPositions.Add(_pos3);
  702. _screenPositions.Add(_pos4);
  703. return _screenPositions;
  704. }
  705. //点为负数的增大 offset,正数减少 offset
  706. //Vector2 startPos1 = new Vector2(_pos1.x - pos1.rect.width * 0.5f + offset, _pos1.y - pos1.rect.height * 0.5f + offset);
  707. //Vector2 startPos2 = new Vector2(_pos2.x + pos2.rect.width * 0.5f - offset, _pos2.y - pos2.rect.height * 0.5f + offset);
  708. //Vector2 startPos3 = new Vector2(_pos3.x + pos3.rect.width * 0.5f - offset, _pos3.y + pos3.rect.height * 0.5f - offset);
  709. //Vector2 startPos4 = new Vector2(_pos4.x - pos4.rect.width * 0.5f + offset, _pos4.y + pos4.rect.height * 0.5f - offset);
  710. Vector2 startPos1 = new Vector2(_pos1.x + offset, _pos1.y + offset);
  711. Vector2 startPos2 = new Vector2(_pos2.x - offset, _pos2.y + offset);
  712. Vector2 startPos3 = new Vector2(_pos3.x - offset, _pos3.y - offset);
  713. Vector2 startPos4 = new Vector2(_pos4.x + offset, _pos4.y - offset);
  714. _screenPositions.Add(startPos1);
  715. _screenPositions.Add(startPos2);
  716. _screenPositions.Add(startPos3);
  717. _screenPositions.Add(startPos4);
  718. return _screenPositions;
  719. }
  720. void SetRectanglePoints(List<Vector2> screenPositions)
  721. {
  722. line.SetLine(screenPositions);
  723. }
  724. //撤回上一个元素
  725. public void onRecall()
  726. {
  727. // 获取并删除最后一个元素,并且保留一个元素
  728. if (oldLinePosition.Count > 1) // 确保列表不为空
  729. {
  730. // 获取回退的那个元素点
  731. LinePosition lastElement_second = oldLinePosition[oldLinePosition.Count - 2];
  732. // 获取最后一个元素
  733. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  734. //Debug.Log(JsonUtility.ToJson(lastElement) + " = " + oldLinePosition.Count);
  735. oldLinePosition.RemoveAt(oldLinePosition.Count - 1); // 删除最后一个元素
  736. SetHandleLocalPosition(pos1, lastElement_second.pos[0]);
  737. SetHandleLocalPosition(pos2, lastElement_second.pos[1]);
  738. SetHandleLocalPosition(pos3, lastElement_second.pos[2]);
  739. SetHandleLocalPosition(pos4, lastElement_second.pos[3]);
  740. //设置线段的点
  741. RefreshLineFromHandles();
  742. }
  743. }
  744. //是不是从Demo界面进入该页面的
  745. public bool enterFromInfraredDemo { get; set; } = false;
  746. //是否从测试场景进入
  747. public bool enterFromZimWebCamera { get; set; } = false;
  748. //确认修改
  749. public void onConfirmation()
  750. {
  751. if (enterFromZimWebCamera)
  752. {
  753. ConfirmScreenLocateManualTest();
  754. if (oldLinePosition.Count > 1) // 确保列表不为空
  755. {
  756. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  757. oldLinePosition.Clear();
  758. oldLinePosition.Add(lastElement);
  759. }
  760. SaveLocalPos();
  761. AudioMgr.ins.PlayBtn();
  762. ViewManager.HideView(UIViewType.InfraredScreenPositioningView);
  763. return;
  764. }
  765. if (!ConfirmScreenLocateManual()) return;
  766. if (oldLinePosition.Count > 1) // 确保列表不为空
  767. {
  768. //LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  769. oldLinePosition.Clear();
  770. //oldLinePosition.Add(lastElement);
  771. }
  772. if (InfraredDemo.running)
  773. {
  774. //跳转入界面
  775. AudioMgr.ins.PlayBtn();
  776. if (!enterFromInfraredDemo)
  777. {
  778. //每次初始化重置一下引导
  779. infraredDemo.resetInfraredPlayerPrefs();
  780. //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  781. //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  782. if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
  783. {
  784. //如果是红外连接成功,记录一个tag
  785. PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
  786. }
  787. //onEnterInfrared();
  788. }
  789. else
  790. {
  791. ViewManager.HideView(UIViewType.InfraredScreenPositioningView);
  792. }
  793. }
  794. //存储一次节点
  795. SaveLocalPos();
  796. }
  797. //重置位置
  798. public void onReset()
  799. {
  800. oldLinePosition.Clear();
  801. // 获取屏幕的四个角的像素坐标
  802. Vector2 bottomLeft = new Vector2(0, 0);
  803. // 将屏幕像素坐标转换为 Canvas 的局部坐标
  804. Vector2 localBottomLeft;
  805. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, bottomLeft, null, out localBottomLeft);
  806. // 打印结果
  807. Debug.Log("Local Bottom Left: " + localBottomLeft);
  808. //int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x) - 120), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y) - 77);
  809. int _x = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.x)), _y = Mathf.FloorToInt(Mathf.Abs(localBottomLeft.y));
  810. SetHandleAnchoredPosition(pos1, new Vector2(-_x, -_y)); //Bottom Left
  811. SetHandleAnchoredPosition(pos2, new Vector2(_x, -_y)); //Bottom Right
  812. SetHandleAnchoredPosition(pos3, new Vector2(_x, _y)); //Top Right
  813. SetHandleAnchoredPosition(pos4, new Vector2(-_x, _y)); //Top Left
  814. //pos1.anchoredPosition = new Vector3(_x, _y, 0);
  815. //pos2.anchoredPosition = new Vector3(-_x, _y, 0);
  816. //pos3.anchoredPosition = new Vector3(-_x, -_y, 0);
  817. //pos4.anchoredPosition = new Vector3(_x, -_y, 0);
  818. //设置一次位置
  819. SetLinePos();
  820. btnRecordInfrared.Reset();
  821. ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
  822. if (enterFromZimWebCamera)
  823. {
  824. ScreenLocate.Main.ScreenQuad.gameObject.SetActive(false);
  825. ScreenLocate.Main.UILineGenerator.Points = new Vector2[0];
  826. }
  827. if (curStepView == ScreenPositioningStep.Start)
  828. {
  829. //如果是开始页面进行自动定位的失败的
  830. if (curStepView != ScreenPositioningStep.AutoEndFail)
  831. {
  832. SetScreenPositioningStepState(ScreenPositioningStep.AutoEndFail);
  833. }
  834. //AutoEndConfirmBtn.interactable = false;
  835. SetAutoConfirmBtnActive(false);
  836. }
  837. }
  838. /// <summary>
  839. /// 根据MaskLine 重置 可拖拽的 line 部分数据
  840. /// </summary>
  841. public void onResetByMaskLine()
  842. {
  843. oldLinePosition.Clear();
  844. List<Vector2> _markLine = getMaskLinePositionsByDeviceType();// maskLine.ScreenPositions
  845. SetHandleAnchoredPosition(pos1, _markLine[0] + new Vector2(20, 15));//Bottom Left
  846. SetHandleAnchoredPosition(pos2, _markLine[1] + new Vector2(-20, 15)); //Bottom Right
  847. SetHandleAnchoredPosition(pos3, _markLine[2] + new Vector2(-10, -15)); //Top Right
  848. SetHandleAnchoredPosition(pos4, _markLine[3] + new Vector2(10, -15));//Top Left
  849. //设置一次位置
  850. SetLinePos();
  851. }
  852. /// <summary>
  853. /// 标准四个点
  854. /// </summary>
  855. /// <param name="index"></param>
  856. /// <param name="pos"></param>
  857. public void onManualNewPos(int index, Vector3 pos)
  858. {
  859. Debug.Log("pos end: " + pos);
  860. if (index == 0)
  861. SetHandleLocalPosition(pos1, pos);
  862. else if (index == 1)
  863. SetHandleLocalPosition(pos2, pos);
  864. else if (index == 2)
  865. SetHandleLocalPosition(pos3, pos);
  866. else if (index == 3)
  867. SetHandleLocalPosition(pos4, pos);
  868. //再记录一次最后的点
  869. SetLinePos();
  870. }
  871. /// <summary>
  872. /// 处理新流程,先记录手动,然后到自动识别处理
  873. /// </summary>
  874. public void onManualToAutomatic()
  875. {
  876. if (enterFromZimWebCamera)
  877. {
  878. ConfirmScreenLocateManualTest();
  879. if (oldLinePosition.Count > 1) // 确保列表不为空
  880. {
  881. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  882. oldLinePosition.Clear();
  883. oldLinePosition.Add(lastElement);
  884. }
  885. SaveLocalPos();
  886. //到自动
  887. OnClick_Auto();
  888. return;
  889. }
  890. if (!ConfirmScreenLocateManual())
  891. {
  892. Debug.LogError("ConfirmScreenLocateManual is false!");
  893. return;
  894. }
  895. if (oldLinePosition.Count > 1) // 确保列表不为空
  896. {
  897. LinePosition lastElement = oldLinePosition[oldLinePosition.Count - 1];
  898. oldLinePosition.Clear();
  899. oldLinePosition.Add(lastElement);
  900. }
  901. //存储一次节点
  902. SaveLocalPos();
  903. //直接设置后进入下一步
  904. OnSecondResult();
  905. //Debug.Log("[校准流程]进入自动校准数据,调用EnterScreenLocateManualAuto");
  906. //bAuto = true;
  907. //doLocateAuto = true;
  908. ////自动校准
  909. //InfraredDemo.infraredCameraHelper.EnterScreenLocateManualAuto();
  910. }
  911. /// <summary>
  912. /// PointMarker 调用
  913. /// </summary>
  914. public void onFinishManualToAutomatic()
  915. {
  916. bMarkerFinish = true;
  917. onManualToAutomatic();
  918. }
  919. #endregion
  920. List<Vector2> _locatePointList = new();
  921. float _texWidth;
  922. float _texHeight;
  923. void RecordLocatePoint(RectTransform p, Vector2 pivot)
  924. {
  925. Vector2 pos = JCUnityLib.RectTransformUtils.GetPositionByPivot(p, pivot);
  926. pos.x = Mathf.Clamp01(pos.x / Screen.width);
  927. pos.y = Mathf.Clamp01(pos.y / Screen.height);
  928. _locatePointList.Add(pos);
  929. }
  930. bool ConfirmScreenLocateManual()
  931. {
  932. if (InfraredDemo.running)
  933. {
  934. //渲染截图
  935. Texture2D texture2D = InfraredDemo.infraredCameraHelper.EnterScreenLocateManual();
  936. if (texture2D == null)
  937. {
  938. Debug.Log("EnterScreenLocateManual = null");
  939. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(null);
  940. return false;
  941. }
  942. _locatePointList.Clear();
  943. _texWidth = texture2D.width;
  944. _texHeight = texture2D.height;
  945. RecordHandleLocatePoint(pos1, 0);
  946. RecordHandleLocatePoint(pos2, 1);
  947. RecordHandleLocatePoint(pos3, 2);
  948. RecordHandleLocatePoint(pos4, 3);
  949. InfraredDemo.infraredCameraHelper.QuitScreenLocateManual(_locatePointList);
  950. FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, 1, 1);
  951. //同步数据
  952. ScreenLocate.quadUnityVectorList.Clear();
  953. ScreenLocate.quadUnityVectorList.Add(_locatePointList[0]);
  954. ScreenLocate.quadUnityVectorList.Add(_locatePointList[1]);
  955. //两个点切换,顺序不一样
  956. ScreenLocate.quadUnityVectorList.Add(_locatePointList[3]);
  957. ScreenLocate.quadUnityVectorList.Add(_locatePointList[2]);
  958. ScreenLocate.SaveScreenLocateVectorList();
  959. Debug.Log("[校准流程]设置QuitScreenLocateManual()手动数据,保存quadUnityVectorList");
  960. return true;
  961. }
  962. return false;
  963. }
  964. void ConfirmScreenLocateManualTest()
  965. {
  966. //渲染截图
  967. ScreenLocate _screenLocate = FindAnyObjectByType<ScreenLocate>();
  968. Texture2D texture2D = _screenLocate.EnterScreenLocateManual();
  969. if (texture2D == null)
  970. {
  971. _screenLocate.QuitScreenLocateManual(null);
  972. return;
  973. }
  974. _locatePointList.Clear();
  975. _texWidth = texture2D.width;
  976. _texHeight = texture2D.height;
  977. RecordHandleLocatePoint(pos1, 0);
  978. RecordHandleLocatePoint(pos2, 1);
  979. RecordHandleLocatePoint(pos3, 2);
  980. RecordHandleLocatePoint(pos4, 3);
  981. _screenLocate.QuitScreenLocateManual(_locatePointList);
  982. //FindObjectOfType<InfraredDemo>().SetLocatePointsToCameraRender(_locatePointList, _texWidth, _texHeight);
  983. setPointsLocation(_locatePointList, pointsParent.gameObject, !bAuto);
  984. if (!doLocateAuto) // 设置手动定位数据
  985. setPointsManual(_locatePointList, GameObject.Find("WebCameraView/CameraImage0/ScreenQuad"));
  986. //同步数据
  987. ScreenLocate.quadUnityVectorList.Clear();
  988. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[0].x, _locatePointList[0].y));
  989. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[1].x, _locatePointList[1].y));
  990. //两个点切换,顺序不一样
  991. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[3].x, _locatePointList[3].y));
  992. ScreenLocate.quadUnityVectorList.Add(new Vector2(_locatePointList[2].x, _locatePointList[2].y));
  993. ScreenLocate.SaveScreenLocateVectorList();
  994. }
  995. void setPointsLocation(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
  996. {
  997. //GameObject pointsTF2 = GameObject.Find("WebCameraView/CameraImage0/ScreenQuad");
  998. pointsTF2.SetActive(active);
  999. if (pointsTF2.transform.childCount == targetList.Count)
  1000. {
  1001. for (int i = 0; i < pointsTF2.transform.childCount; i++)
  1002. {
  1003. Transform pointTF = pointsTF2.transform.GetChild(i);
  1004. Vector2 pos = targetList[i];
  1005. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(Vector2.one, pointsTF2.GetComponent<RectTransform>().rect);
  1006. pointTF.gameObject.SetActive(true);
  1007. }
  1008. }
  1009. }
  1010. void setPointsManual(List<Vector2> targetList, GameObject pointsTF2, bool active = true)
  1011. {
  1012. setPointsLocation(targetList, pointsTF2, active);
  1013. var lo = new Vector2(-0.5f, -0.5f);
  1014. ScreenLocate.Main.UILineGenerator.Points = new Vector2[4] {
  1015. 2 * (targetList[0] + lo),
  1016. 2 * (targetList[1] + lo),
  1017. 2 * (targetList[2] + lo),
  1018. 2 * (targetList[3] + lo) };
  1019. }
  1020. void SaveLocalPos()
  1021. {
  1022. List<Vector3> screenPositions = new List<Vector3>();
  1023. screenPositions.Add(GetHandleAnchoredPosition(pos1));
  1024. screenPositions.Add(GetHandleAnchoredPosition(pos2));
  1025. screenPositions.Add(GetHandleAnchoredPosition(pos3));
  1026. screenPositions.Add(GetHandleAnchoredPosition(pos4));
  1027. string saveStr = string.Join(';', screenPositions.Select(v => $"{v.x},{v.y}")); //,{v.z}
  1028. Debug.Log("Local UI Position: " + saveStr);
  1029. PlayerPrefs.SetString("ScreenPositioningView", saveStr);
  1030. }
  1031. void GetLocalPos()
  1032. {
  1033. string posListStr = PlayerPrefs.GetString("ScreenPositioningView", "");
  1034. if (!string.IsNullOrWhiteSpace(posListStr))
  1035. {
  1036. List<Vector2> posList = posListStr.Split(';')
  1037. .Select(s =>
  1038. {
  1039. string[] parts = s.Split(',');
  1040. return new Vector2(float.Parse(parts[0]), float.Parse(parts[1]));
  1041. })
  1042. .ToList();
  1043. SetHandleAnchoredPosition(pos1, posList[0]);
  1044. SetHandleAnchoredPosition(pos2, posList[1]);
  1045. SetHandleAnchoredPosition(pos3, posList[2]);
  1046. SetHandleAnchoredPosition(pos4, posList[3]);
  1047. RefreshLineFromHandles();
  1048. }
  1049. }
  1050. #region 按钮颜色切换
  1051. private void OnButtonClick(Button button)
  1052. {
  1053. // 切换按钮颜色
  1054. ColorBlock colors = button.colors;
  1055. colors.normalColor = highlightedColor;
  1056. colors.highlightedColor = highlightedColor;
  1057. button.colors = colors;
  1058. // 切换字体颜色
  1059. Text buttonText = button.GetComponentInChildren<Text>();
  1060. buttonText.color = highlightedTextColor;
  1061. }
  1062. private void ResetButton(Button button)
  1063. {
  1064. // 重置按钮颜色
  1065. ColorBlock colors = button.colors;
  1066. colors.normalColor = normalColor;
  1067. colors.highlightedColor = normalColor;
  1068. button.colors = colors;
  1069. // 重置字体颜色
  1070. Text buttonText = button.GetComponentInChildren<Text>();
  1071. buttonText.color = normalTextColor;
  1072. }
  1073. #endregion
  1074. #region 最后选择算法处理UI
  1075. //选择全自动结果后进入游戏
  1076. void OnFirstResult()
  1077. {
  1078. QuadrilateralInCamera screenAuto = ScreenLocate.Main.ScreenIdentification.QuadAuto;
  1079. if (screenAuto == null)
  1080. {
  1081. Debug.Log("校准结果为空!");
  1082. return;
  1083. }
  1084. ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.Auto);
  1085. UpdateQuadUnityVectorList();
  1086. onCompelete();
  1087. }
  1088. //选择半自动结果
  1089. //void OnSecondResult()
  1090. //{
  1091. // ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.SemiAuto);
  1092. // UpdateQuadUnityVectorList();
  1093. // onCompelete();
  1094. //}
  1095. //选择手动定位结果
  1096. void OnSecondResult()
  1097. {
  1098. ScreenLocate.Main.SelectScreenAfterLocate(ScreenLocate.ScreenIdentificationTag.Manual);
  1099. UpdateQuadUnityVectorList();
  1100. onCompelete();
  1101. }
  1102. public void onCompelete()
  1103. {
  1104. if (enterFromZimWebCamera)
  1105. {
  1106. SaveLocalPos();
  1107. ViewManager.HideView(UIViewType.InfraredScreenPositioningView);
  1108. return;
  1109. }
  1110. EnterGame();
  1111. //存储一次节点
  1112. SaveLocalPos();
  1113. }
  1114. void EnterGame()
  1115. {
  1116. //跳转入界面
  1117. AudioMgr.ins.PlayBtn();
  1118. OnClick_Successful();
  1119. //if (InfraredDemo.running)
  1120. //{
  1121. // //跳转入界面
  1122. // AudioMgr.ins.PlayBtn();
  1123. // if (!enterFromInfraredDemo)
  1124. // {
  1125. // //每次初始化重置一下引导
  1126. // infraredDemo.resetInfraredPlayerPrefs();
  1127. // //GameObject connectGuidanceView = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_ConnectGuidanceView);
  1128. // //connectGuidanceView.GetComponent<ConnectGuidanceView>().showTextipInfrared();
  1129. // if (!PlayerPrefs.HasKey("hideInfraredBowAndArrow"))
  1130. // {
  1131. // //如果是红外连接成功,记录一个tag
  1132. // PlayerPrefs.SetInt("hideInfraredBowAndArrow", 1);
  1133. // }
  1134. // onEnterInfrared();
  1135. // }
  1136. // else
  1137. // {
  1138. // ViewManager.HideView(UIViewType.InfraredScreenPositioningView);
  1139. // }
  1140. //}
  1141. }
  1142. /// <summary>
  1143. /// 选择模式后更新 quadUnityVectorList
  1144. /// </summary>
  1145. void UpdateQuadUnityVectorList()
  1146. {
  1147. ScreenLocate.Main.UpdateQuadUnityVectorList();
  1148. SyncQuadUnityVectorListToPos();
  1149. infraredDemo?.SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList, 1, 1);
  1150. _locatePointList.Clear();
  1151. }
  1152. void SyncQuadUnityVectorListToPos()
  1153. {
  1154. Debug.Log("[ScreenLocate] SyncQuadUnityVectorListToPos quadUnityVectorList:" + ScreenLocate.Main.PrintVector2List(ScreenLocate.quadUnityVectorList));
  1155. SetHandleAnchoredPosition(pos1, ScreenLocate.quadUnityVectorList[0].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect));
  1156. SetHandleAnchoredPosition(pos2, ScreenLocate.quadUnityVectorList[1].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect));
  1157. SetHandleAnchoredPosition(pos4, ScreenLocate.quadUnityVectorList[2].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect));
  1158. SetHandleAnchoredPosition(pos3, ScreenLocate.quadUnityVectorList[3].pixelToLocalPosition_AnchorCenter(Vector2.one, canvasRectTransform.rect));
  1159. }
  1160. //这个脚本存在时候。任何切换操作都直接处理删除
  1161. //void OnSceneUnloaded(Scene scene)
  1162. //{
  1163. // ViewManager.HideView(ViewManager2.Path_ConnectGuidanceView);
  1164. // ViewManager.HideView(UIViewType.InfraredScreenPositioningView);
  1165. //}
  1166. //void OnEnable()
  1167. //{
  1168. // SceneManager.sceneUnloaded += OnSceneUnloaded;
  1169. //}
  1170. //void OnDisable()
  1171. //{
  1172. // SceneManager.sceneUnloaded -= OnSceneUnloaded;
  1173. //}
  1174. #endregion
  1175. #region 判断点是否在 quad 内部
  1176. //判断maskline
  1177. public bool IsPointInMaskLine(Vector2 point)
  1178. {
  1179. return IsPointInQuad(point, getMaskLinePositionsByDeviceType());// maskLine.ScreenPositions
  1180. }
  1181. //实现基于射线法,通过数交点的奇偶性来判断点是否在多边形内
  1182. bool IsPointInQuad(Vector2 point, List<Vector2> quadVertices)
  1183. {
  1184. if (quadVertices.Count != 4)
  1185. {
  1186. Debug.LogError("Quad must have exactly 4 vertices.");
  1187. return false;
  1188. }
  1189. bool isInside = false;
  1190. int vertexCount = quadVertices.Count;
  1191. for (int i = 0, j = vertexCount - 1; i < vertexCount; j = i++)
  1192. {
  1193. Vector2 vi = quadVertices[i];
  1194. Vector2 vj = quadVertices[j];
  1195. // 检查射线是否与边相交
  1196. if (((vi.y > point.y) != (vj.y > point.y)) &&
  1197. (point.x < (vj.x - vi.x) * (point.y - vi.y) / (vj.y - vi.y) + vi.x))
  1198. {
  1199. isInside = !isInside;
  1200. }
  1201. }
  1202. return isInside;
  1203. }
  1204. /// <summary>
  1205. /// 判断四个点是否能够形成有效的四边形,并根据面积阈值过滤小四边形
  1206. /// </summary>
  1207. /// <param name="points">四个点</param>
  1208. /// <param name="minArea">面积阈值,用于过滤面积太小的四边形</param>
  1209. /// <returns>是否是有效的四边形</returns>
  1210. public bool IsValidQuadrilateral(Vector2[] points, float minArea = 100000.0f)
  1211. {
  1212. if (points.Length != 4)
  1213. {
  1214. Debug.Log("四个点数不足,无法形成四边形。");
  1215. return false; // 如果点数不是4,直接返回false
  1216. }
  1217. // 计算四边形的面积
  1218. float area = CalculateArea(points);
  1219. Debug.Log($"四边形面积: {area}");
  1220. if (area < minArea)
  1221. {
  1222. Debug.Log($"面积小于阈值: {minArea},返回 false。");
  1223. return false; // 面积太小,返回 false
  1224. }
  1225. // 检查是否有三点共线
  1226. for (int i = 0; i < points.Length; i++)
  1227. {
  1228. Vector2 a = points[i];
  1229. Vector2 b = points[(i + 1) % points.Length];
  1230. Vector2 c = points[(i + 2) % points.Length];
  1231. if (ArePointsCollinear(a, b, c))
  1232. {
  1233. Debug.Log($"点 {a}, {b}, {c} 共线,返回 false。");
  1234. return false; // 存在共线点,返回 false
  1235. }
  1236. }
  1237. // 检查是否为矩形或接近矩形
  1238. if (!IsRectangle(points))
  1239. {
  1240. // 如果不是矩形,检查是否是梯形
  1241. if (!IsTrapezoid(points))
  1242. {
  1243. Debug.Log("既不是矩形,也不是梯形,返回 false。");
  1244. return false; // 不是矩形也不是梯形,返回 false
  1245. }
  1246. else
  1247. {
  1248. Debug.Log("是梯形。");
  1249. }
  1250. }
  1251. else
  1252. {
  1253. Debug.Log("是矩形。");
  1254. }
  1255. // 检查对角线是否相交
  1256. if (DoLinesIntersect(points[0], points[1], points[2], points[3]) ||
  1257. DoLinesIntersect(points[1], points[2], points[3], points[0]))
  1258. {
  1259. Debug.Log("对角线相交,返回 false。");
  1260. return false; // 对角线相交,返回 false
  1261. }
  1262. // 检查点的排列顺序,确保是顺时针或逆时针
  1263. if (!ArePointsClockwise(points) && !ArePointsClockwise(points.Reverse().ToArray()))
  1264. {
  1265. Debug.Log("点的排列顺序不正确,返回 false。");
  1266. return false; // 点的排列顺序不正确
  1267. }
  1268. Debug.Log("四边形有效,返回 true。");
  1269. return true; // 通过所有检查,返回 true
  1270. }
  1271. /// <summary>
  1272. /// 判断是否是矩形(近似90度角)
  1273. /// </summary>
  1274. private bool IsRectangle(Vector2[] points)
  1275. {
  1276. // 角度容差范围,允许夹角有小的偏差
  1277. const float dotProductThreshold = 0.1f;
  1278. for (int i = 0; i < points.Length; i++)
  1279. {
  1280. Vector2 a = points[i];
  1281. Vector2 b = points[(i + 1) % points.Length];
  1282. Vector2 c = points[(i + 2) % points.Length];
  1283. // 计算向量 AB 和 BC 的内积
  1284. Vector2 ab = b - a;
  1285. Vector2 bc = c - b;
  1286. float dotProduct = Vector2.Dot(ab.normalized, bc.normalized);
  1287. // 如果内积接近 0,表示夹角接近 90 度
  1288. if (Mathf.Abs(dotProduct) > dotProductThreshold)
  1289. {
  1290. Debug.Log($"点 {a}, {b}, {c} 的夹角不接近 90 度,返回 false。");
  1291. return false; // 角度不接近 90 度
  1292. }
  1293. }
  1294. return true;
  1295. }
  1296. /// <summary>
  1297. /// 判断是否是梯形
  1298. /// </summary>
  1299. private bool IsTrapezoid(Vector2[] points)
  1300. {
  1301. // 斜率容差范围,允许斜率差异较小
  1302. const float slopeThreshold = 0.2f;
  1303. // 计算对角线的斜率
  1304. float slope1 = (points[1].y - points[0].y) / (points[1].x - points[0].x);
  1305. float slope2 = (points[3].y - points[2].y) / (points[3].x - points[2].x);
  1306. // 如果对角线斜率差异小于容差范围,则认为是梯形
  1307. if (Mathf.Abs(slope1 - slope2) < slopeThreshold)
  1308. {
  1309. Debug.Log("对角线平行,判断为梯形。");
  1310. return true; // 对角线平行,返回 true
  1311. }
  1312. else
  1313. {
  1314. Debug.Log($"对角线斜率差异过大: {slope1} vs {slope2},不是梯形,返回 false。");
  1315. return false; // 斜率差异过大,不是梯形
  1316. }
  1317. }
  1318. //计算四边形面积
  1319. float CalculateArea(Vector2[] points)
  1320. {
  1321. float area = 0f;
  1322. int n = points.Length;
  1323. for (int i = 0; i < n; i++)
  1324. {
  1325. Vector2 current = points[i];
  1326. Vector2 next = points[(i + 1) % n];
  1327. area += current.x * next.y - current.y * next.x;
  1328. }
  1329. return Mathf.Abs(area) / 2f;
  1330. }
  1331. //确保点不共线
  1332. //判断点是否在同一条直线上(即共线),如果共线则无法形成四边形。可以通过计算任意三点的面积是否为 0 来验证是否共线:
  1333. bool ArePointsCollinear(Vector2 a, Vector2 b, Vector2 c)
  1334. {
  1335. float area = Mathf.Abs(a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)) / 2.0f;
  1336. return Mathf.Approximately(area, 0f);
  1337. }
  1338. //确保边不相交
  1339. bool DoLinesIntersect(Vector2 a, Vector2 b, Vector2 c, Vector2 d)
  1340. {
  1341. float cross(Vector2 p1, Vector2 p2, Vector2 p3)
  1342. {
  1343. return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
  1344. }
  1345. float d1 = cross(a, b, c);
  1346. float d2 = cross(a, b, d);
  1347. float d3 = cross(c, d, a);
  1348. float d4 = cross(c, d, b);
  1349. return d1 * d2 < 0 && d3 * d4 < 0;
  1350. }
  1351. //顺时针或逆时针排列顶点
  1352. bool ArePointsClockwise(Vector2[] points)
  1353. {
  1354. float sum = 0;
  1355. for (int i = 0; i < points.Length; i++)
  1356. {
  1357. Vector2 current = points[i];
  1358. Vector2 next = points[(i + 1) % points.Length];
  1359. sum += (next.x - current.x) * (next.y + current.y);
  1360. }
  1361. return sum < 0; // 小于0为顺时针,大于0为逆时针
  1362. }
  1363. #endregion
  1364. #region 四角消灭处理部分
  1365. public int collisionCount = 0;
  1366. public void SetCollisionEvent()
  1367. {
  1368. if (collisionCount < 3)
  1369. {
  1370. collisionCount++;
  1371. }
  1372. else
  1373. {
  1374. StartCoroutine(TransitionToCalibrationAfterCollision());
  1375. }
  1376. }
  1377. IEnumerator TransitionToCalibrationAfterCollision()
  1378. {
  1379. if (SB_EventSystem.ins)
  1380. {
  1381. //切换设备时候清除一下选中状态,避免鼠标还在UI上有选中状态
  1382. SB_EventSystem.ins.simulateMouse?.ClearSelection();
  1383. //SB_EventSystem.ins.mouseConfirm?.Hide();
  1384. //if (SB_EventSystem.ins.simulateMouseIsAwaked)
  1385. //{
  1386. // SB_EventSystem.ins.AwakenSimulateMouse();
  1387. //}
  1388. }
  1389. yield return null;
  1390. ViewManager.HideView(UIViewType.DeviceArcheryView);
  1391. ViewManager.HideView(UIViewType.DeviceGunView);
  1392. ViewManager.HideView(UIViewType.InfraredScreenPositioningView);
  1393. ViewManager.ShowView(UIViewType.CalibrationView);
  1394. }
  1395. #endregion
  1396. }
  1397. }