InfraredScreenPositioningView.cs 66 KB

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