InfraredScreenPositioningView.cs 65 KB

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