InfraredScreenPositioningView.cs 69 KB

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