InfraredScreenPositioningView.cs 65 KB

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