InfraredScreenPositioningView.cs 64 KB

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