InfraredScreenPositioningView.cs 64 KB

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