InfraredScreenPositioningView.cs 65 KB

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