InfraredScreenPositioningView.cs 65 KB

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