InfraredScreenPositioningView.cs 64 KB

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