InfraredScreenPositioningView.cs 64 KB

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